Example #1
0
 public function set($name, $value)
 {
     if (!isset($this->mapping[$name])) {
         throw new WrongArgumentException("knows nothing about property '{$name}'");
     }
     $primitive = $this->mapping[$name];
     $setter = 'set' . ucfirst($primitive->getName());
     if (!method_exists($this->object, $setter)) {
         throw new WrongArgumentException("cannot find mutator for '{$name}' in class " . get_class($this->object));
     }
     if (is_object($value)) {
         if ($primitive instanceof PrimitiveAnyType && $value instanceof PrototypedEntity) {
             $value = ObjectToDTOConverter::create($value->entityProto())->make($value);
         } else {
             $value = $this->dtoValue($value, $primitive);
         }
     } elseif (is_array($value) && is_object(current($value))) {
         $dtoValue = [];
         foreach ($value as $oneValue) {
             Assert::isTrue(is_object($oneValue), 'array must contain only objects');
             $dtoValue[] = $this->dtoValue($oneValue, $primitive);
         }
         $value = $dtoValue;
     }
     return $this->object->{$setter}($value);
 }
Example #2
0
 /**
  * @return GmpBigInteger
  **/
 public static function make($number, $base = 10)
 {
     Assert::isTrue(is_numeric($number));
     $result = new self();
     $result->resource = gmp_init($number, $base);
     return $result;
 }
 /**
  * @throws WrongArgumentException
  * @return IdentifiablePrimitive
  **/
 public function setValue($value)
 {
     $className = $this->className;
     Assert::isNotNull($this->className);
     Assert::isTrue($value instanceof $className);
     return parent::setValue($value);
 }
Example #4
0
 /**
  * @return PrototypedBuilder
  **/
 public function cloneBuilder(EntityProto $proto)
 {
     Assert::isTrue($this->proto->isInstanceOf($proto) || $proto->isInstanceOf($this->proto), Assert::dumpArgument($proto));
     $result = new $this($proto);
     $result->limitedPropertiesList = $this->limitedPropertiesList;
     return $result;
 }
Example #5
0
 public static function swap(DAOConnected $first, DAOConnected $second, $property = 'position')
 {
     Assert::isTrue(get_class($first) === get_class($second));
     $setMethod = 'set' . ucfirst($property);
     $getMethod = 'get' . ucfirst($property);
     Assert::isTrue(method_exists($first, $setMethod) && method_exists($first, $getMethod));
     /** @var StorableDAO $dao */
     $dao = $first->dao();
     $db = DBPool::me()->getByDao($dao);
     $oldPosition = $first->{$getMethod}();
     $newPosition = $second->{$getMethod}();
     $db->begin();
     $e = null;
     try {
         $dao->save($first->{$setMethod}(self::$nullValue));
         $dao->save($second->{$setMethod}($oldPosition));
         $dao->save($first->{$setMethod}($newPosition));
         $db->commit();
     } catch (DatabaseException $e) {
         $db->rollback();
     }
     $dao->uncacheByIds([$first->getId(), $second->getId()]);
     if ($e) {
         throw $e;
     }
 }
Example #6
0
 /**
  * @throws WrongArgumentException
  * @return PrimitiveIdentifier
  **/
 public function of($class)
 {
     $className = $this->guessClassName($class);
     Assert::isTrue(class_exists($className, true) || interface_exists($className, true), "knows nothing about '{$className}' class/interface");
     $this->ofClassName = $className;
     return $this;
 }
 /**
  * @return PrimitiveMultiList
  **/
 public function setDefault($default)
 {
     Assert::isArray($default);
     foreach ($default as $index) {
         Assert::isTrue(array_key_exists($index, $this->list));
     }
     return parent::setDefault($default);
 }
Example #8
0
 public function __construct($left, $right, $logic)
 {
     Assert::isTrue($right instanceof Query || $right instanceof Criteria || $right instanceof MappableObject || is_array($right));
     Assert::isTrue($logic == self::IN || $logic == self::NOT_IN);
     $this->left = $left;
     $this->right = $right;
     $this->logic = $logic;
 }
Example #9
0
 public function __construct($class)
 {
     Assert::isTrue(ClassUtils::isInstanceOf($class, Prototyped::class));
     if (is_object($class)) {
         $this->className = get_class($class);
     } else {
         $this->className = $class;
     }
 }
Example #10
0
 protected function cacheListByQuery(SelectQuery $query, $array)
 {
     if ($array !== Cache::NOT_FOUND) {
         Assert::isArray($array);
         Assert::isTrue(current($array) instanceof Identifiable);
     }
     Cache::me()->mark($this->className)->add($this->makeQueryKey($query, self::SUFFIX_LIST), $array, Cache::EXPIRES_FOREVER);
     return $array;
 }
Example #11
0
 public function set($name, $value)
 {
     if (!isset($this->mapping[$name])) {
         throw new WrongArgumentException("knows nothing about property '{$name}'");
     }
     Assert::isTrue(!is_object($value), 'cannot put objects into scope');
     $primitive = $this->mapping[$name];
     $this->object[$primitive->getName()] = $value;
     return $this;
 }
Example #12
0
 /**
  * @return SQLChain
  **/
 public function toMapped(ProtoDAO $dao, JoinCapableQuery $query)
 {
     $size = count($this->chain);
     Assert::isTrue($size > 0, 'empty chain');
     $chain = new $this();
     for ($i = 0; $i < $size; ++$i) {
         $chain->exp($dao->guessAtom($this->chain[$i], $query), $this->logic[$i]);
     }
     return $chain;
 }
Example #13
0
 /**
  * @return IndexedList
  **/
 public function offsetSet($offset, $value)
 {
     Assert::isTrue($value instanceof Identifiable);
     $offset = $value->getId();
     if ($this->offsetExists($offset)) {
         throw new WrongArgumentException("object with id == '{$offset}' already exists");
     }
     $this->list[$offset] = $value;
     return $this;
 }
Example #14
0
 protected function cacheListByQuery(SelectQuery $query, $array, $expires = Cache::EXPIRES_FOREVER)
 {
     if ($array !== Cache::NOT_FOUND) {
         Assert::isArray($array);
         Assert::isTrue(current($array) instanceof Identifiable);
     }
     $key = $this->makeQueryKey($query, self::SUFFIX_LIST);
     Cache::me()->mark($this->className)->set($key, ['tags' => $this->getTagsForQuery($query), 'data' => $array], $expires);
     //			SemaphorePool::me()->free(self::LOCK_PREFIX.$key);
     return $array;
 }
 /**
  * @return GmpBigInteger
  **/
 public function makeRandom($stop, RandomSource $source)
 {
     if (is_string($stop)) {
         $stop = $this->makeNumber($stop);
     } elseif ($stop instanceof BigInteger && !$stop instanceof GmpBigInteger) {
         $stop = $this->makeNumber($stop->toString());
     }
     Assert::isTrue($stop instanceof GmpBigInteger);
     $numBytes = ceil(log($stop->floatValue(), 2) / 8);
     return $this->makeFromBinary("" . $source->getBytes($numBytes))->mod($stop);
 }
Example #16
0
 public final function query(Query $query)
 {
     $result = $this->queryRaw($query->toDialectString($this->getDialect()));
     if ($query instanceof InsertQuery && !empty($this->sequencePool[$name = $query->getTable() . '_id'])) {
         $id = current($this->sequencePool[$name]);
         Assert::isTrue($id instanceof Identifier, 'identifier was lost in the way');
         $id->setId($this->getInsertId())->finalize();
         unset($this->sequencePool[$name][key($this->sequencePool[$name])]);
     }
     return $result;
 }
 public function toBoolean(Form $form)
 {
     Assert::isTrue($this->brackets, 'brackets must be enabled');
     $subject = $form->toFormValue($this->subject);
     switch ($this->logic) {
         case self::NOT:
             return false === $subject;
         default:
             throw new UnsupportedMethodException("'{$this->logic}' doesn't supported yet");
     }
 }
Example #18
0
 public function __construct(ProtoDAO $dao, SelectQuery $query = null)
 {
     if ($query) {
         Assert::isTrue($query instanceof SelectQuery);
     }
     $this->dao = $dao;
     $this->db = DBPool::getByDao($this->dao);
     $this->selectQuery = $query;
     $this->openTransaction();
     $this->declareCursor();
 }
Example #19
0
 /**
  * @return LogicalChain
  **/
 public static function block($args, $logic)
 {
     Assert::isTrue($logic == BinaryExpression::EXPRESSION_AND || $logic == BinaryExpression::EXPRESSION_OR, "unknown logic '{$logic}'");
     $logicalChain = new self();
     foreach ($args as $arg) {
         if (!$arg instanceof LogicalObject && !$arg instanceof SelectQuery) {
             throw new WrongArgumentException('unsupported object type: ' . get_class($arg));
         }
         $logicalChain->exp($arg, $logic);
     }
     return $logicalChain;
 }
Example #20
0
 /**
  * @return SimplePhpView
  **/
 public function render(Model $model = null)
 {
     Assert::isTrue($model === null || $model instanceof Model);
     if ($model) {
         extract($model->getList());
     }
     $partViewer = new PartViewer($this->partViewResolver, $model);
     $this->preRender();
     include $this->templatePath;
     $this->postRender();
     return $this;
 }
Example #21
0
 /**
  * @return DBTable
  **/
 public function addUniques()
 {
     Assert::isTrue(func_num_args() > 0);
     $uniques = [];
     foreach (func_get_args() as $name) {
         // check existence
         $this->getColumnByName($name);
         $uniques[] = $name;
     }
     $this->uniques[] = $uniques;
     return $this;
 }
 /**
  * @return PrimitiveIdentifier
  **/
 public function setMethodName($methodName)
 {
     if (is_callable($methodName)) {
         /* all ok, call what you want */
     } elseif (strpos($methodName, '::') === false) {
         $dao = $this->dao();
         Assert::isTrue(method_exists($dao, $methodName), "knows nothing about '" . get_class($dao) . "::{$methodName}' method");
     } else {
         ClassUtils::checkStaticMethod($methodName);
     }
     $this->methodName = $methodName;
     return $this;
 }
Example #23
0
 public function __construct($what, $from)
 {
     if ($from instanceof DialectString) {
         Assert::isTrue($from instanceof DBValue || $from instanceof DBField);
     } else {
         $from = new DBField($from);
     }
     if (!$what instanceof DatePart) {
         $what = new DatePart($what);
     }
     $this->what = $what;
     $this->from = $from;
 }
Example #24
0
 protected function cacheListByQuery(SelectQuery $query, $array)
 {
     if ($array !== Cache::NOT_FOUND) {
         Assert::isArray($array);
         Assert::isTrue(current($array) instanceof Identifiable);
     }
     $cache = Cache::me();
     $key = $this->makeQueryKey($query, self::SUFFIX_LIST);
     if ($this->handler->touch($this->keyToInt($key))) {
         $cache->mark($this->className)->add($key, $array, Cache::EXPIRES_FOREVER);
     }
     return $array;
 }
Example #25
0
 /**
  * @throws WrongArgumentException
  * @return boolean
  **/
 public function importValue($value)
 {
     if ($value === null) {
         return parent::importValue(null);
     }
     Assert::isTrue($value instanceof Hstore, 'importValue');
     if (!$this->value instanceof Form) {
         $this->value = $this->makeForm();
     }
     $this->value->import($value->getList());
     $this->imported = true;
     return $this->value->getErrors() ? false : true;
 }
Example #26
0
 public static function down(DAOConnected $object, LogicalObject $exp = null)
 {
     $getMethod = 'get' . ucfirst(self::$property);
     Assert::isTrue(method_exists($object, $getMethod));
     $oldPosition = $object->{$getMethod}();
     $criteria = Criteria::create($object->dao())->add(Expression::gt(self::$property, $oldPosition))->addOrder(OrderBy::create(self::$property)->asc())->setLimit(1);
     if ($exp) {
         $criteria->add($exp);
     }
     if ($lowerObject = $criteria->get()) {
         DaoUtils::setNullValue(self::$nullValue);
         DaoUtils::swap($lowerObject, $object, self::$property);
     }
 }
Example #27
0
 public function __construct($rgb)
 {
     $length = strlen($rgb);
     Assert::isTrue($length <= 7, 'color must be #XXXXXX');
     if ($rgb[0] == '#') {
         $rgb = substr($rgb, 1);
     }
     if ($length < 6) {
         $rgb = str_pad($rgb, 6, '0', STR_PAD_LEFT);
     }
     $this->red = hexdec($rgb[0] . $rgb[1]);
     $this->green = hexdec($rgb[2] . $rgb[3]);
     $this->blue = hexdec($rgb[4] . $rgb[5]);
 }
 /**
  * @throws BaseException
  * @return ModelAndView
  **/
 public function run(Prototyped $subject, Form $form, HttpRequest $request)
 {
     Assert::isFalse($this->running, 'command already running');
     Assert::isTrue($subject instanceof DAOConnected);
     $this->transaction = InnerTransaction::begin($subject->dao());
     try {
         $mav = $this->command->run($subject, $form, $request);
         $this->running = true;
         return $mav;
     } catch (BaseException $e) {
         $this->transaction->rollback();
         throw $e;
     }
 }
 public function make($object, $recursive = true)
 {
     Assert::isTrue(is_readable($object), "required object `{$object}` must exist");
     $realObject = $this->getRealObject($object);
     $result = $this->identityMap->lookup($realObject);
     if ($result) {
         return $result;
     }
     $result = parent::make($realObject, $recursive);
     if ($result instanceof Identifiable) {
         $result->setId(basename($realObject));
     }
     return $result;
 }
Example #30
0
 public function toDialectString(Dialect $dialect)
 {
     Assert::isTrue($this->targets !== [], 'do not know who should i truncate');
     if ($dialect->hasTruncate()) {
         $head = 'TRUNCATE TABLE ';
     } else {
         $head = 'DELETE FROM ';
     }
     if ($dialect->hasMultipleTruncate()) {
         $query = $head . $this->dumpTargets($dialect, null, ',');
     } else {
         $query = $this->dumpTargets($dialect, $head, ';');
     }
     return $query . ';';
 }