Example #1
0
 /**
  * @throws WrongArgumentException
  * @return PrimitiveForm
  * @deprecated You should use ofProto() instead
  **/
 public function of($className)
 {
     Assert::classExists($className);
     $protoClass = EntityProto::PROTO_CLASS_PREFIX . $className;
     Assert::classExists($protoClass);
     return $this->ofProto(Singleton::getInstance($protoClass));
 }
Example #2
0
 public function __construct($root, $path)
 {
     Assert::isString($path, 'non-string path given');
     if (is_object($root)) {
         $className = get_class($root);
     } else {
         Assert::classExists($root);
         $className = $root;
     }
     $this->root = $className;
     $this->path = $path;
     $this->fetchHelpers($className);
     $proto = self::$protos[$className];
     $path = explode('.', $path);
     for ($i = 0, $size = count($path); $i < $size; ++$i) {
         $this->properties[$i] = $property = $proto->getPropertyByName($path[$i]);
         if ($className = $property->getClassName()) {
             $this->fetchHelpers($className);
             $proto = self::$protos[$className];
         } elseif ($i < $size) {
             continue;
         } else {
             throw new WrongArgumentException('corrupted path');
         }
     }
 }
 /**
  * @throws WrongArgumentException
  * @return PrimitivePolymorphicIdentifier
  **/
 public function ofBase($className)
 {
     Assert::classExists($className);
     Assert::isInstance($className, DAOConnected::class, "class '{$className}' must implement DAOConnected interface");
     $this->baseClassName = $className;
     return $this;
 }
Example #4
0
 /**
  * @throws WrongArgumentException
  * @return PrimitiveRegistry
  **/
 public function of($class)
 {
     $className = $this->guessClassName($class);
     Assert::classExists($className);
     Assert::isInstance($className, Registry::class);
     $this->className = $className;
     return $this;
 }
Example #5
0
 public static final function create(StorageEngineType $type = null, $linkId = null)
 {
     if (!$type) {
         return new self($linkId);
     }
     $className = $type->toString();
     Assert::classExists($className);
     return new $className($linkId);
 }
 public function dropWith($worker)
 {
     Assert::classExists($worker);
     if ($this->modifiedIds) {
         $workerObject = new $worker($this->dao);
         $workerObject->uncacheByIds($this->modifiedIds);
         $this->modifiedIds = [];
     }
     return $this;
 }
Example #7
0
 public function __construct($controllerName)
 {
     Assert::classExists($controllerName);
     $this->url = $controllerName;
 }
Example #8
0
 public static function setDefaultHandler($handler)
 {
     Assert::classExists($handler);
     self::$defaultHandler = $handler;
 }
Example #9
0
 private function processPath(AbstractProtoClass $proto, $probablyPath, JoinCapableQuery $query, $table, $parentRequired = true, $prefix = null)
 {
     $path = explode('.', $probablyPath);
     try {
         $property = $proto->getPropertyByName($path[0]);
     } catch (MissingElementException $e) {
         // oh, it's a value, not a property
         return new DBValue($probablyPath);
     }
     unset($path[0]);
     Assert::isTrue($property->getRelationId() != null && !$property->isGenericType());
     Assert::classExists($property->getClassName());
     // checking whether we're playing with value object
     if (!method_exists($property->getClassName(), 'dao')) {
         if (method_exists($property->getClassName(), 'proto') && count($path) > 1) {
             return $this->processPath($property->getProto(), implode('.', $path), $query, $table);
         } else {
             return $this->guessAtom(implode('.', $path), $query, $table, $prefix);
         }
     } else {
         $propertyDao = call_user_func([$property->getClassName(), 'dao']);
         Assert::isNotNull($propertyDao, 'can not find target dao for "' . $property->getName() . '" property at "' . get_class($proto) . '"');
     }
     $alias = $propertyDao->getJoinName($property->getColumnName(), $prefix);
     if ($property->getRelationId() == MetaRelation::ONE_TO_MANY || $property->getRelationId() == MetaRelation::MANY_TO_MANY) {
         $remoteName = $property->getClassName();
         $selfName = $this->getObjectName();
         $self = new $selfName();
         $getter = $property->getGetter();
         $dao = call_user_func([$remoteName, 'dao']);
         if ($property->getRelationId() == MetaRelation::MANY_TO_MANY) {
             $helperTable = $self->{$getter}()->getHelperTable();
             $helperAlias = $helperTable;
             if (!$query->hasJoinedTable($helperAlias)) {
                 $logic = Expression::eq(DBField::create($this->getIdName(), $table), DBField::create($self->{$getter}()->getParentIdField(), $helperAlias));
                 if ($property->isRequired()) {
                     $query->join($helperTable, $logic, $helperAlias);
                 } else {
                     $query->leftJoin($helperTable, $logic, $helperAlias);
                 }
             }
             $logic = Expression::eq(DBField::create($propertyDao->getIdName(), $alias), DBField::create($self->{$getter}()->getChildIdField(), $helperAlias));
         } else {
             $logic = Expression::eq(DBField::create($self->{$getter}()->getParentIdField(), $alias), DBField::create($this->getIdName(), $table));
         }
         if (!$query->hasJoinedTable($alias)) {
             if ($property->isRequired() && $parentRequired) {
                 $query->join($dao->getTable(), $logic, $alias);
             } else {
                 $query->leftJoin($dao->getTable(), $logic, $alias);
             }
         }
     } else {
         // OneToOne, lazy OneToOne
         // prevents useless joins
         if (isset($path[1]) && count($path) == 1 && $path[1] == $propertyDao->getIdName()) {
             return new DBField($property->getColumnName(), $table);
         }
         if (!$query->hasJoinedTable($alias)) {
             $logic = Expression::eq(DBField::create($property->getColumnName(), $table), DBField::create($propertyDao->getIdName(), $alias));
             if ($property->isRequired() && $parentRequired) {
                 $query->join($propertyDao->getTable(), $logic, $alias);
             } else {
                 $query->leftJoin($propertyDao->getTable(), $logic, $alias);
             }
         }
     }
     if ($path) {
         return $propertyDao->guessAtom(implode('.', $path), $query, $alias, $property->isRequired() && $parentRequired, $propertyDao->getJoinPrefix($property->getColumnName(), $prefix));
     }
     // ok, we're done
 }
Example #10
0
 /**
  * @return BasePrimitive
  **/
 public static function spawn($primitive, $name)
 {
     Assert::classExists($primitive);
     return new $primitive($name);
 }
Example #11
0
 public function toValue(ProtoDAO $dao = null, $array, $prefix = null)
 {
     $raw = $array[$prefix . $this->columnName];
     if ($this->type == 'binary') {
         return DBPool::getByDao($dao)->getDialect()->unquoteBinary($raw);
     }
     if ($this->className == 'HttpUrl') {
         return HttpUrl::create()->parse($raw);
     }
     if ($this->type === 'json' || $this->type === 'jsonb') {
         return json_decode($raw, true);
         //associative array instead of object
     }
     if (!$this->identifier && $this->generic && $this->className) {
         return call_user_func([$this->className, 'create'], $raw);
     } elseif (!$this->identifier && $this->className) {
         // BOVM: prevents segfault on >=php-5.2.5
         Assert::classExists($this->className);
         if (!is_subclass_of($this->className, Enumeration::class) && !is_subclass_of($this->className, Enum::class) && !is_subclass_of($this->className, Registry::class)) {
             $remoteDao = call_user_func([$this->className, 'dao']);
             $joinPrefix = $remoteDao->getJoinPrefix($this->columnName, $prefix);
             $joined = $this->strategyId == FetchStrategy::JOIN || isset($array[$joinPrefix . $remoteDao->getIdName()]);
             if ($joined) {
                 return $remoteDao->makeObject($array, $joinPrefix);
             } else {
                 // will be fetched later
                 // by AbstractProtoClass::fetchEncapsulants
                 $object = new $this->className();
                 $object->setId($raw);
                 return $object;
             }
         } else {
             return new $this->className($raw);
         }
     }
     // veeeeery "special" handling, by tradition.
     // MySQL returns 0/1, others - t/f
     if ($this->type == 'boolean') {
         return (bool) strtr($raw, ['f' => null]);
     }
     return $raw;
 }
Example #12
0
 public static function setHandler($handler)
 {
     Assert::classExists($handler);
     self::$handler = new $handler();
 }
Example #13
0
 public static function setDefaultLocker($name)
 {
     Assert::classExists($name);
     self::$lockerName = $name;
     self::$locker = Singleton::getInstance($name);
 }