/** * Create inspection for model field. * * @param string $field * @return FieldInspection */ private function inspectField($field) { $filters = $this->reflection->getSetters() + $this->reflection->getAccessors(); $fillable = true; if ($this->reflection->getSecured() === '*' || in_array($field, $this->reflection->getSecured())) { $fillable = false; } if ($this->reflection->getFillable() != []) { $fillable = in_array($field, $this->reflection->getFillable()); } return new FieldInspection($this->inspector, $field, $this->reflection->getFields()[$field], $fillable, in_array($field, $this->reflection->getHidden()), isset($filters[$field]), array_key_exists($field, $this->reflection->getValidates())); }
/** * {@inheritdoc} * * Schema can generate accessors and filters based on field type. */ public function getMutators() { $mutators = parent::getMutators(); //Trying to resolve mutators based on field type foreach ($this->tableSchema->getColumns() as $column) { //Resolved filters $resolved = []; if (!empty($filter = $this->builder->getMutators($column->abstractType()))) { //Mutator associated with type directly $resolved += $filter; } elseif (!empty($filter = $this->builder->getMutators('php:' . $column->phpType()))) { //Mutator associated with php type $resolved += $filter; } //Merging mutators and default mutators foreach ($resolved as $mutator => $filter) { if (!array_key_exists($column->getName(), $mutators[$mutator])) { $mutators[$mutator][$column->getName()] = $filter; } } } foreach ($mutators as $mutator => &$filters) { foreach ($filters as $field => $filter) { //Some mutators may be described using aliases (for shortness) $filters[$field] = $this->builder->mutatorAlias($filter); } unset($filters); } return $mutators; }
/** * Generate virtual entity representation. * * @param ReflectionEntity $entity * @return ClassElement */ protected function renderEntity(ReflectionEntity $entity) { $element = new ClassElement($entity->getShortName()); //We are going to render every entity property as real property due child implementation //may clarify it's type foreach ($entity->getFields() as $field => $type) { if (substr($field, 0, 1) == '_') { //Hidden fields continue; } $classType = ''; $arrayType = false; if (is_array($type)) { $arrayType = true; $type = $type[0]; } $type = str_replace('[]', '', $type); if (lcfirst($type) != $type && class_exists($type)) { $type = $classType = '\\' . $type; } if ($arrayType) { $type .= '[]'; } $element->property($field, "@var {$type}")->setAccess(AbstractElement::ACCESS_PUBLIC); //Let's pre-generate getters and setters $setter = $element->method('set' . Inflector::classify($field)); $getter = $element->method('get' . Inflector::classify($field)); $setter->parameter($field, $type); if (!empty($arrayType)) { $setter->parameter($field)->setType('array'); } elseif (!empty($classType)) { $setter->parameter($field)->setType($classType); } $setter->setComment("@return \$this", true); $getter->setComment("@return {$type}"); } foreach ($entity->getAccessors() as $name => $accessor) { if (is_array($accessor)) { $accessor = $accessor[0]; } $element->property($name, '@var \\' . $accessor); $element->method('get' . Inflector::classify($name))->setComment("@return \\" . $accessor); } return $element; }
/** * {@inheritdoc} * * Schema can generate accessors and filters based on field type. */ public function getMutators() { $mutators = parent::getMutators(); //Trying to resolve mutators based on field type foreach ($this->getFields() as $field => $type) { //Resolved filters $resolved = []; if (is_array($type) && is_scalar($type[0]) && ($filter = $this->builder->getMutators('array::' . $type[0]))) { //Mutator associated to array with specified type $resolved += $filter; } elseif (is_array($type) && ($filter = $this->builder->getMutators('array'))) { //Default array mutator $resolved += $filter; } elseif (!is_array($type) && ($filter = $this->builder->getMutators($type))) { //Mutator associated with type directly $resolved += $filter; } if (isset($resolved[self::MUTATOR_ACCESSOR])) { //Accessor options include field type, this is ODM specific behaviour $resolved[self::MUTATOR_ACCESSOR] = [$resolved[self::MUTATOR_ACCESSOR], is_array($type) ? $type[0] : $type]; } //Merging mutators and default mutators foreach ($resolved as $mutator => $filter) { if (!array_key_exists($field, $mutators[$mutator])) { $mutators[$mutator][$field] = $filter; } } } //Some mutators may be described using aliases (for shortness) $mutators = $this->normalizeMutators($mutators); //Every composition is counted as field accessor :) foreach ($this->getCompositions() as $field => $composition) { $mutators[self::MUTATOR_ACCESSOR][$field] = [$composition['type'] == ODM::CMP_MANY ? Compositor::class : ODM::CMP_ONE, $composition['class']]; } return $mutators; }
/** * Generate set of fields for given entity, only fillable fields will be added. * * @param ReflectionEntity $entity */ public function followEntity(ReflectionEntity $entity) { foreach ($entity->getFillable() as $field) { $type = $entity->getFields()[$field]; //Let's use data by default $this->addField($field, $type, 'data'); if (!empty($entity->getValidates()[$field])) { //Let's use validation declared in entity if ($entity->getValidates()[$field] != ['notEmpty']) { //We are only going to use default entity validation if $this->validates[$field] = $entity->getValidates()[$field]; } } } }