public function hydrate(string $klass, array $data) : EntityInterface { $entity = parent::hydrate($klass, $data); $refl = new \ReflectionClass($entity); $md = EntityMetadataRegistry::get($klass); $columns = []; foreach ($md->getColumns() as $column) { if ($md->isRelation($column->getField())) { continue; } $name = $column->getField(); $field = StringUtil::camelize($name); if (!array_key_exists($name, $data)) { $columns[$field] = new PartialEntityException($klass, $field); continue; } $columns[$field] = $column->getType()->castForEntity($data[$name]); } parent::setPrivateProperty($refl, 'columns', $entity, new AttributeCollection($entity, $columns)); $relations = []; foreach ($md->getRelations() as $name => $relation) { $relations[$name] = $relation; } parent::setPrivateProperty($refl, 'relations', $entity, new RelationCollection($entity, $relations)); return $entity; }
public function __construct(string $klass) { $this->klass = $klass; $this->entity = substr($klass, strrpos($klass, '\\') + 1); $this->table = strtolower(StringUtil::pluralize($this->entity)); if (EntitiesConfiguration::instance()->getConnection() === null) { throw new \BadMethodCallException('Could not establish database connection'); } $this->reader = new SqlReader($this->table, EntitiesConfiguration::getConnection()); }
private function findInReflectionClass(\ReflectionClass $refl, string $type, string $name) { $_ = StringUtil::classify($type); $has = 'has' . $_; $get = 'get' . $_; if (!method_exists($refl, $has) || !method_exists($refl, $get)) { throw new \InvalidArgumentException(sprintf('Cannot find by %s in ReflectionClass', $type)); } $ref = new \ReflectionClass($refl->getName()); while (!call_user_func([$ref, $has], $name)) { if ($ref = $ref->getParentClass()) { continue; } throw new \InvalidArgumentException(sprintf('Property %s does not exist in %s', $name, $refl->getName())); } return call_user_func([$ref, $get], $name); }
public function getChanges() : array { $md = EntityMetadataRegistry::get(get_class(parent::getEntity())); $changes = []; foreach (parent::getChanges() as $key => $value) { $column = StringUtil::underscore($key); $relation = $md->getRelation($column); // If the column isn't a relation, or the relation isn't defined by a column on the current entity, we // can skip it if ($relation === null || $md->getColumn($column) === null) { continue; } if ($value instanceof EntityInterface) { $value = $value->getId(); } $changes[$key] = $value; } return $changes; }
/** * @param ColumnInterface $column * @param $retType * * @return string */ private function buildGetterName(ColumnInterface $column, $retType) : string { return ($retType === ColumnTypeHelper::TYPE_BOOLEAN ? 'is' : 'get') . StringUtil::classify($column->getField()); }
private static function addManyRelation(string $name, string $entity, bool $owned, array $conds = [], string $target = null) { if (!$target) { $target = StringUtil::underscore(ClassUtil::getClassName(static::class)); } EntityMetadataRegistry::get(static::class)->addRelation($name, new ManyRelation($entity, $conds, $owned, null, $target)); }