Exemple #1
0
 public function unmapValue(Reflection\Property $property, $value)
 {
     if ($property->getType() === Reflection\Property::TYPE_DATE) {
         return new Date($value);
     }
     return $value;
 }
 public function unmapValue(Reflection\Property $property, $value)
 {
     if ($value === null) {
         return "";
     } elseif ($value instanceof \DateTime && isset(self::$format[$property->getType()])) {
         $value = $value->format(self::$format[$property->getType()]);
         if ($value === false) {
             throw new \Exception("Can not convert DateTime automatically!");
         }
     }
     return $value;
 }
Exemple #3
0
 /**
  * Insert/update entity
  *
  * @param Entity $entity
  *
  * @return Entity
  *
  * @throws Exception\RepositoryException
  */
 public function save(Entity $entity)
 {
     $requiredClass = UNC::nameToClass($this->getEntityName(), UNC::ENTITY_MASK);
     if (!$entity instanceof $requiredClass) {
         throw new Exception\RepositoryException("Entity must be instance of " . $requiredClass . "!");
     }
     $reflection = Entity\Reflection::load($entity);
     if (!$reflection->hasPrimary()) {
         throw new Exception\RepositoryException("Can not save entity without primary property!");
     }
     $primaryName = $reflection->getPrimaryProperty()->getName();
     $primaryValue = $entity->{$primaryName};
     if ($primaryValue === null) {
         $primaryValue = $this->create($entity);
         if (Property::isPrimaryEmpty($primaryValue)) {
             throw new RepositoryException("Entity was successfully created but returned primary is empty!");
         }
         $entity->{$primaryName} = $primaryValue;
     } else {
         $this->update($entity, $primaryValue);
     }
     return $entity;
 }
Exemple #4
0
 /**
  * Parse properties from annotations
  *
  * @param string $docComment
  *
  * @throws Exception\EntityException
  */
 private function _parseProperties($docComment)
 {
     $properties = [];
     foreach (Reflection\Annotation::parseProperties($docComment) as $definition) {
         try {
             $property = new Reflection\Property($definition[2], $definition[3], $this, !$definition[1], $definition[4]);
         } catch (Exception\PropertyException $e) {
             throw new Exception\EntityException($e->getMessage(), $this->className, $definition[0]);
         }
         // Prevent duplications
         if (isset($properties[$property->getName()])) {
             throw new Exception\EntityException("Duplicate property with name '" . $property->getName() . "'!", $this->className, $definition[0]);
         }
         if (in_array($property->getName(), $this->publicProperties)) {
             throw new Exception\EntityException("Property '" . $property->getName() . "' already defined as public property!", $this->className, $definition[0]);
         }
         // Primary property
         if ($property->hasOption(Reflection\Property::OPTION_PRIMARY)) {
             if ($this->hasPrimary()) {
                 throw new Exception\EntityException("Primary already defined!", $this->className, $definition[0]);
             }
             $this->primaryName = $property->getName();
         }
         if ($property->hasOption(Reflection\Property::OPTION_ASSOC) && $this->primaryName === null) {
             throw new Exception\EntityException("You must define primary property before the association!", $this->className, $definition[0]);
         }
         $this->properties[$property->getName()] = $property;
     }
 }
Exemple #5
0
 public function unmapValue(Entity\Reflection\Property $property, $value)
 {
     // Call map filter from property option
     if ($property->hasOption(Entity\Reflection\Property::OPTION_MAP_FILTER)) {
         $value = call_user_func($property->getOption(Entity\Reflection\Property::OPTION_MAP_FILTER)[1], $value);
     }
     if ($value instanceof Entity\Collection) {
         return $this->unmapCollection($value);
     } elseif ($value instanceof Entity) {
         return $this->unmapEntity($value);
     }
     // Call adapter's mapping if needed
     if (!$property->getEntityReflection()->hasAdapter()) {
         throw new Exception\InvalidArgumentException("Entity " . $property->getEntityReflection()->getClassName() . " has no adapter defined!");
     }
     if (isset($this->adapterMappings[$property->getEntityReflection()->getAdapterName()])) {
         return $this->adapterMappings[$property->getEntityReflection()->getAdapterName()]->unmapValue($property, $value);
     }
     return $value;
 }