Exemple #1
0
 /**
  * Create query
  *
  * @return QueryBuilder
  */
 protected function query()
 {
     $class = UNC::nameToClass($this->getEntityName(), UNC::ENTITY_MASK);
     return $class::query();
 }
Exemple #2
0
 /**
  * Load and register reflection
  *
  * @param string|Entity|Collection|Reflection $arg
  *
  * @throws Exception\InvalidArgumentException
  *
  * @return Reflection
  */
 public static function load($arg)
 {
     if (is_object($arg) && $arg instanceof Entity) {
         $class = get_class($arg);
     } elseif (is_object($arg) && $arg instanceof Collection) {
         $class = $arg->getEntityClass();
     } elseif (is_object($arg) && $arg instanceof Reflection) {
         $class = $arg->getClassName();
         if (!isset(self::$registered[$class])) {
             return self::$registered[$class] = $arg;
         }
         return $arg;
     } elseif (is_string($arg)) {
         $class = $arg;
     } else {
         throw new Exception\InvalidArgumentException("Entity identifier must be object, collection, class or name!", $arg);
     }
     if (!is_subclass_of($class, "UniMapper\\Entity")) {
         $class = UNC::nameToClass($arg, UNC::ENTITY_MASK);
     }
     if (!class_exists($class)) {
         throw new Exception\InvalidArgumentException("Entity class " . $class . " not found!");
     }
     if (isset(self::$registered[$class])) {
         return self::$registered[$class];
     }
     return self::$registered[$class] = new Reflection($class);
 }
Exemple #3
0
 /**
  * Validate value type
  *
  * @param mixed $value Given value
  *
  * @throws Exception\InvalidArgumentException
  * @throws \Exception
  */
 public function validateValueType($value)
 {
     if ($this->hasOption(self::OPTION_PRIMARY) && self::isPrimaryEmpty($value)) {
         throw new Exception\InvalidArgumentException("Primary value can not be empty string or null!", $value);
     }
     if ($value === null) {
         return;
     }
     // Enumeration
     if ($this->hasOption(self::OPTION_ENUM) && !$this->getOption(self::OPTION_ENUM)->isValid($value)) {
         throw new Exception\InvalidArgumentException("Value " . $value . " is not from defined entity enumeration " . "range on property " . $this->name . "!", $value);
     }
     // Scalar or array type
     if (self::isScalarType($this->type) || $this->type === self::TYPE_ARRAY) {
         if (gettype($value) === $this->type) {
             return;
         }
         throw new Exception\InvalidArgumentException("Expected " . $this->type . " but " . gettype($value) . " given on property " . $this->name . "!", $value);
     }
     // Object validation
     $givenType = gettype($value);
     if ($givenType === "object") {
         $givenType = get_class($value);
     }
     if ($this->type === self::TYPE_ENTITY) {
         // Entity
         $expectedEntityClass = UNC::nameToClass($this->typeOption, UNC::ENTITY_MASK);
         if ($value instanceof $expectedEntityClass) {
             return;
         } else {
             throw new Exception\InvalidArgumentException("Expected entity " . $this->typeOption . " but " . $givenType . " given on property " . $this->name . "!", $value);
         }
     } elseif ($this->type === self::TYPE_COLLECTION) {
         // Collection
         $entityClass = UNC::nameToClass($this->typeOption, UNC::ENTITY_MASK);
         if (!$value instanceof Entity\Collection) {
             throw new Exception\InvalidArgumentException("Expected entity collection but " . $givenType . " given on" . " property " . $this->name . "!", $value);
         } elseif ($value->getEntityClass() !== $entityClass) {
             throw new Exception\InvalidArgumentException("Expected collection of entity " . $entityClass . " but collection of entity " . $value->getEntityClass() . " given on property " . $this->name . "!", $value);
         } else {
             return;
         }
     } elseif ($this->type === self::TYPE_DATETIME) {
         // DateTime
         if ($value instanceof \DateTime) {
             return;
         } else {
             throw new Exception\InvalidArgumentException("Expected DateTime but " . $givenType . " given on" . " property " . $this->name . "!", $value);
         }
     } elseif ($this->type === self::TYPE_DATE) {
         // Date
         if ($value instanceof \DateTime) {
             return;
         } else {
             throw new Exception\InvalidArgumentException("Expected date as DateTime object but " . $givenType . " given on property " . $this->name . "!", $value);
         }
     } else {
         // Unexpected
         throw new \Exception("Unsupported type " . $this->type . "!");
     }
 }