/** * Construct * * @param string $entity entity class name */ public function __construct($entity) { $ref = new ClassReflection($entity); // TODO: verify entity class $this->entity = $entity; if ($pos = strrpos($entity, '\\')) { $pos++; } $this->name = substr($entity, $pos); if ($ref->hasAnnotation('tableName')) { $this->tableName = $ref->getAnnotation('tableName'); } else { $this->tableName = Tools::underscore(Tools::pluralize($this->name)); } foreach ($ref->getProperties() as $property) { if ($property->hasAnnotation('column')) { $annotation = (array) $property->getAnnotation('column'); $datatype = 'ActiveMapper\\DataTypes\\' . $annotation[0]; unset($annotation[0]); $params = array_merge(array($property->name, $property->hasAnnotation('null')), $annotation); if (!\class_exists($datatype)) { throw new \ActiveMapper\InvalidDataTypeException("Data type '{$datatype}' not exist"); } $this->columns[$property->name] = callback(ClassReflection::from($datatype), 'newInstance')->invokeArgs($params); } if ($property->hasAnnotation('primary')) { if ($property->hasAnnotation('column')) { if (empty($this->primaryKey)) { $this->primaryKey = $property->name; } else { throw new \NotImplementedException("Multiple column primary key not implemented '{$entity}'"); } } else { throw new \NotImplementedException("Primary key must be column {$entity}::\${$property->name}"); } } if ($property->hasAnnotation('autoincrement')) { if (!$this->columns[$property->name] instanceof \ActiveMapper\DataTypes\Int) { throw new \ActiveMapper\InvalidDataTypeException("Autoincrement avaiable only for Int data type column {$entity}::\${$property->name}"); } elseif ($property->name == $this->primaryKey) { $this->primaryKeyAutoincrement = TRUE; } else { throw new \NotImplementedException("Auto increment for non primary key column not implemented"); } } } if (!$this->primaryKey) { throw new \LogicException("Entity without primary key not supported '{$this->entity}'"); } if (!$this->hasProxy() && ($ref->hasAnnotation('OneToOne') || $ref->hasAnnotation('OneToOne') || $ref->hasAnnotation('OneToOne') || $ref->hasAnnotation('OneToOne'))) { throw new \LogicException("Entity associations support only on proxy entity"); } }