/** * 初始化对象 */ protected function initialize() { $exploded = explode(':', $this->name(), 2); lego_assert(count($exploded) === 2, 'JSON field name example: `array:key:sub-key:...`'); $this->column = $exploded[0]; $this->jsonKey = str_replace(':', '.', $exploded[1]); }
protected function mode($mode, $condition = true) { lego_assert(in_array($mode, [self::MODE_EDITABLE, self::MODE_READONLY, self::MODE_DISABLED]), 'illegal mode'); if (value($condition)) { $this->mode = $mode; // trigger event $this->afterModeChanged($mode); } return $this; }
protected function add($fieldType, $fieldName, $fieldDescription) : Field { // 为避免人肉拼接namespace, 所以写了下面一坨 $field = class_namespace(Text::class, $fieldType); lego_assert(class_exists($field), 'Undefined Field ' . $field); /** @var Field $field */ $field = new $field($fieldName, $fieldDescription, $this->data()); $this->fields[$fieldName] = $field; $this->fieldAdded($field); return $field; }
public function afterRegistered() { $globals = Register::get(self::class, self::class); foreach ($this->data() as $name => &$callable) { if ($name === self::class) { continue; } if (is_string($callable) && array_key_exists($callable, $globals)) { $callable = $globals[$callable]; lego_assert(is_callable($callable), '$callable is not callable.'); } // Register to ResponseData ResponseData::add($name, $callable); } }
private function calculateRelated(Model $model = null, $relationName = null) { $model = $model ?: $this->getModel(); if (is_null($relationName)) { $copy = $model; foreach (explode('.', $this->relation) as $name) { $copy = $this->calculateRelated($copy, $name); } return $copy; } lego_assert(method_exists($model, $relationName), get_class($model) . " . not exists method {$relationName}()"); /** @var Relation $relation */ $relation = $model->{$relationName}(); lego_assert($relation instanceof Relation, "{$relationName}() result must be instance of Relation"); return $relation->getRelated(); }
/** * key 转换为 RegisterData 类 * * - field.data => \Lego\Register\Data\FieldData * - \Lego\Register\Data\FieldData => \Lego\Register\Data\FieldData * * @param $key * @return mixed|string */ private static function translateClass($key) { if (is_subclass_of($key, RegisterData::class)) { return $key; } static $cache = []; // 避免多次进行父类判定 if (isset($cache[$key])) { return $cache[$key]; } // abc.def.ghi => AbcDefGhi $classBaseName = ucfirst(camel_case(str_replace('.', '_', $key))); $class = class_namespace(RegisterData::class, $classBaseName); lego_assert(is_subclass_of($class, RegisterData::class), "Unsupported Register data {$class}(key: {$key})"); $cache[$key] = $class; return $class; }
/** * 校验注册的数据是否合法, 不合法时抛出异常 * @param array $data */ protected function validate(array $data = []) { lego_assert($this->path() === self::class, 'ResponseData Path must be self.'); }
public function response() { $arguments = value($this->arguments); lego_assert(is_array($arguments), '$data[1] should be array or return array.'); return call_user_func_array($this->response, $arguments); }
/** * 初始化操作, 在类构造函数中调用 */ protected function initialize() { lego_assert($this->data() instanceof Row, 'Unsupported data.'); }
public function getButtons($location) { lego_assert(in_array($location, $this->buttonLocations()), "{$location} does not exists."); return $this->buttons[$location]; }
private function assertIsEloquentRow() { lego_assert($this->source() instanceof EloquentRow, 'Unsupported Rule on ' . class_basename($this->source())); }
/** * 校验注册的数据是否合法, 不合法时抛出异常 * @param $data */ protected function validate($data) { lego_assert($data instanceof \Closure, '$data should be Closure.'); }
/** * 校验注册的数据是否合法, 不合法时抛出异常 * @param $data */ protected function validate($data) { lego_assert(is_subclass_of($data, \Lego\Field\Field::class), '$data should be subclass of ' . \Lego\Field\Field::class); }