/** * @todo vyjímku pro dva sloupce s stejným názvem, vč PK! */ public function __construct($argument) { $this->_class = $argument; if (!$this->__call('isEntity')) { throw new Exception("Cannot create reflection: '{$argument}' is not an entity."); } if (($prefix = $this->getAnnotation('prefix')) && \is_string($prefix)) { $this->prefix = $prefix; } else { $this->prefix = \strtolower(\preg_replace('/[^A-Z0-9]*/', '', $this->getName())); } $default_primary_key = $this->prefix . '_' . self::ID; $this->_properties = $this->getAnnotations('property'); foreach ($this->_properties as &$pa) { if ($pa->primary_key) { $this->primaryKey = $pa->name; $this->columns[$pa->name] = \is_string($pa->column) ? $pa->column : $default_primary_key; $this->types[$this->columns[$pa->name]] = $pa->type; } } if ($this->primaryKey === NULL) { $this->primaryKey = self::ID; $this->columns[self::ID] = $default_primary_key; $this->types[$this->columns[self::ID]] = 'int'; } foreach ($this->_properties as &$pa) { if ($pa->primary_key) { continue; } elseif ($pa->relation === 'belongs_to') { $parentClass = $pa->type; if ($parentClass == $this->getName()) { $pr = $this; } else { $pr = $parentClass::getReflection(); } \is_string($pa->column) or $pa->column = $pr->getPrimaryKeyColumn(); $this->parents[$pa->name] = $parentClass; $this->columns[$pa->name] = $pa->column; $this->types[$this->columns[$pa->name]] = $pr->types[$pr->getPrimaryKeyColumn()]; } elseif ($pa->relation === 'has_many') { $this->children[$pa->name] = $pa->type; $this->foreignKeys[$pa->name] = \is_string($pa->column) ? $pa->column : $this->columns[$this->primaryKey]; } elseif ($pa->relation === 'has_one') { $this->singles[$pa->name] = $pa->type; $this->foreignKeys[$pa->name] = \is_string($pa->column) ? $pa->column : $this->columns[$this->primaryKey]; } elseif ($pa->relation === FALSE) { $this->columns[$pa->name] = \is_string($pa->column) ? $pa->column : ($pa->column = $this->prefix . '_' . $pa->name); $this->types[$this->columns[$pa->name]] = $pa->type; } } if ($this->__call('isExtendingEntity')) { $parentEntity = $this->__call('getParentEntity'); $pr = $parentEntity::getReflection(); $this->parents[Entity::EXTENDED] = $parentEntity; $this->columns[Entity::EXTENDED] = $this->getPrimaryKeyColumn(); $this->types[$this->columns[Entity::EXTENDED]] = $pr->types[$pr->getPrimaryKeyColumn()]; } if (($tn = $this->getAnnotation('table')) && \is_string($tn)) { $this->tableName = $tn; } else { $this->tableName = Helpers::fromCamelCase(strrpos($this->getName(), '\\') !== FALSE ? \substr($this->getName(), \strrpos($this->getName(), '\\') + 1) : $this->getName()); } }
/** * Should not by called directly * @param string $name * @param mixed $value * @return void */ public function __set($name, $value) { //name starts with underscore -> no magic functionality if (\strpos($name, '_') === 0) { $_name = \substr($name, 1); } else { $_name = FALSE; } //primary key if ($name == 'id' || $name == $this->_reflection->primaryKey || $name == $this->_reflection->getPrimaryKeyColumn()) { $this->_setId($value); return; } //parents if (isset($this->_reflection->parents[$name])) { if ($value === NULL || \is_object($value) && $value instanceof $this->_reflection->parents[$name]) { $this->_parents[$name] = $value; return; } throw new Exception("Property {$name} must be entity of class " . $this->_reflection->parents[$name]); } //singles if (isset($this->_reflection->singles[$name])) { if ($value === NULL || \is_object($value) && $value instanceof $this->_reflection->singles[$name]) { $this->_singles[$name] = $value; return; } throw new Exception("Property {$name} must be entity of class " . $this->_reflection->singles[$name]); } //children if (isset($this->_reflection->children[$name])) { if ($value === NULL || \is_object($value) && $value instanceof \IDataSource) { $this->_children[$name] = $value; return; } throw new Exception("Property {$name} must implement interface IDataSource."); } /***** values *****/ if ($_name && isset($this->_reflection->columns[$_name]) && !isset($this->_reflection->parents[$_name])) { return $this->_setValue($this->_reflection->columns[$_name], $value); } if (\method_exists($this, $m_name = Helpers::setter($name))) { //set{Name} return $this->{$m_name}($value); } if (isset($this->_reflection->columns[$name]) && !isset($this->_reflection->parents[$name])) { return $this->_setValue($this->_reflection->columns[$name], $value); } //inheritance if ($this->_reflection->isExtendingEntity()) { $this->_parents[self::EXTENDED]->{$name} = $value; return; } $class = \get_called_class(); throw new Exception("Undeclared property {$class}->{$name}."); }