Example #1
0
 /**
  * Get entity validator
  *
  * @return \UniMapper\Validator
  */
 public function getValidator()
 {
     if (!$this->validator) {
         $this->validator = new Validator($this);
     }
     return $this->validator->onEntity();
 }
Example #2
0
 private function _setProperties($values, $autoConvert = true, $skipUndefined = false, $setReadonly = false, $skipUnwritable = false)
 {
     if (!Validator::isTraversable($values)) {
         throw new Exception\InvalidArgumentException("Values must be traversable data!", $values);
     }
     $reflection = Entity\Reflection::load(get_called_class());
     foreach ($values as $name => $value) {
         // Public
         if (in_array($name, $reflection->getPublicProperties())) {
             $this->{$name} = $value;
             continue;
         }
         // Undefined
         if (!$reflection->hasProperty($name)) {
             if ($skipUndefined) {
                 continue;
             }
             throw new Exception\InvalidArgumentException("Undefined property '" . $name . "'!");
         }
         $property = $reflection->getProperty($name);
         // Computed
         if ($property->hasOption(Entity\Reflection\Property::OPTION_COMPUTED)) {
             if ($skipUnwritable) {
                 continue;
             }
             throw new Exception\InvalidArgumentException("Computed property is read-only!");
         }
         // Readonly
         if (!$property->isWritable() && !$setReadonly) {
             if ($skipUnwritable) {
                 continue;
             }
             throw new Exception\InvalidArgumentException("Property '" . $name . "' is read-only!");
         }
         // Validate type
         try {
             $property->validateValueType($value);
         } catch (Exception\InvalidArgumentException $e) {
             if ($autoConvert) {
                 $value = $property->convertValue($value);
             } else {
                 throw $e;
             }
         }
         // Set value
         $this->data[$name] = $value;
     }
 }
Example #3
0
 /**
  * @param string|Entity $name   Entity name, class or entity object
  * @param mixed         $values
  */
 public function __construct($name, $values = null)
 {
     $reflection = Entity\Reflection::load($name);
     $this->entityClass = $reflection->getClassName();
     if ($values) {
         if (Validator::isTraversable($values)) {
             foreach ($values as $index => $value) {
                 if ($value instanceof Entity) {
                     $this->offsetSet($index, $value);
                 } else {
                     $this->data[$index] = $reflection->createEntity($value);
                 }
             }
         } else {
             throw new Exception\InvalidArgumentException("Values must be traversable data!", $values);
         }
     }
 }
Example #4
0
 public function __construct(callable $validation, \UniMapper\Validator $parent)
 {
     $this->validation = $validation;
     $this->validator = new \UniMapper\Validator($parent->getEntity(), $parent);
     $this->validator->on($parent->getProperty()->getName());
 }
Example #5
0
 /**
  * Try to convert value on required type automatically
  *
  * @param mixed $value
  *
  * @return mixed
  *
  * @throws Exception\InvalidArgumentException
  */
 public function convertValue($value)
 {
     if ($value === null || $value === "" && $this->type !== self::TYPE_STRING) {
         return;
     }
     if (self::isScalarType($this->type) || $this->type === self::TYPE_ARRAY) {
         // Scalar and array
         if ($this->type === self::TYPE_BOOLEAN && strtolower($value) === "false") {
             return false;
         }
         if (is_scalar($value) || $this->type === self::TYPE_ARRAY) {
             if (settype($value, $this->type)) {
                 return $value;
             }
         }
     } elseif ($this->type === self::TYPE_DATETIME || $this->type === self::TYPE_DATE) {
         // DateTime
         if ($value instanceof \DateTime) {
             return $value;
         } elseif (is_array($value) && isset($value["date"])) {
             $date = $value["date"];
         } elseif (is_object($value) && isset($value->date)) {
             $date = $value->date;
         } else {
             $date = $value;
         }
         if (isset($date)) {
             try {
                 return new \DateTime($date);
             } catch (\Exception $e) {
             }
         }
     } elseif ($this->type === self::TYPE_COLLECTION && Validator::isTraversable($value)) {
         // Collection
         return new Entity\Collection($this->typeOption, $value);
     } elseif ($this->type === self::TYPE_ENTITY && Validator::isTraversable($value)) {
         // Entity
         return Entity\Reflection::load($this->typeOption)->createEntity($value);
     }
     throw new Exception\InvalidArgumentException("Can not convert value on property '" . $this->name . "' automatically!", $value);
 }
Example #6
0
 public function mapEntity($name, $data)
 {
     if (!Validator::isTraversable($data)) {
         throw new Exception\InvalidArgumentException("Input data must be traversable!", $data);
     }
     $entityReflection = Entity\Reflection::load($name);
     $values = [];
     foreach ($data as $index => $value) {
         $propertyName = $index;
         // Map property name if needed
         foreach ($entityReflection->getProperties() as $propertyReflection) {
             if ($propertyReflection->getName(true) === $index) {
                 $propertyName = $propertyReflection->getName();
                 break;
             }
         }
         // Skip undefined properties
         if (!$entityReflection->hasProperty($propertyName)) {
             continue;
         }
         // Map value
         $values[$propertyName] = $this->mapValue($entityReflection->getProperty($propertyName), $value);
     }
     return $entityReflection->createEntity($values);
 }