Ejemplo n.º 1
0
 /**
  * Vytvori / nacte / vrati entitu.
  * @param IEntity|scalar|array
  * @param bool
  * @return IEntity|NULL null only if not invasive
  * @throws EntityNotFoundException
  */
 protected final function createEntity($entity, $invasive = true)
 {
     if ($entity instanceof IEntity) {
         $model = $entity->getModel(false);
         if ($model and $this->parent->getModel(false) !== $model) {
             $this->parent->fireEvent('onAttachModel', NULL, $model);
         }
     }
     $repository = $this->getChildRepository($invasive);
     // todo neni mozne kdyz parent neni pripojen na repo
     if (!$repository) {
         return NULL;
     }
     if (!$entity instanceof IEntity and (is_array($entity) or $entity instanceof Traversable)) {
         $array = $entity instanceof Traversable ? iterator_to_array($entity) : $entity;
         $entity = NULL;
         if (isset($array['id'])) {
             $entity = $repository->getById($array['id']);
         }
         if (!$entity) {
             if (!$invasive) {
                 return NULL;
             }
             $entityName = $repository->getEntityClassName($array);
             $entity = new $entityName();
             // todo construct pak nesmy mit povine parametry
             $repository->attach($entity);
         }
         if ($invasive) {
             $entity->setValues($array);
         }
     }
     if (!$entity instanceof IEntity) {
         $id = $entity;
         $entity = $repository->getById($id);
         if (!$entity) {
             if (!$invasive) {
                 return NULL;
             }
             throw new EntityNotFoundException("Entity '{$id}' not found in `" . get_class($repository) . "`");
         }
     }
     if ($invasive) {
         $repository->attach($entity);
     } else {
         if (!$repository->isAttachableEntity($entity)) {
             return NULL;
         }
     }
     return $entity;
 }
 /**
  * usort comparison function
  * @see self::getResult()
  * @see self::$_sort
  * @param IEntity
  * @param IEntity
  * @return int -1 or 1
  */
 private final function _sort(IEntity $aRow, IEntity $bRow)
 {
     foreach ($this->_sort as $tmp) {
         $key = $tmp[0];
         $direction = $tmp[1];
         if (strpos($key, '->') !== false) {
             $a = $aRow;
             $b = $bRow;
             foreach (explode('->', $key) as $k) {
                 if (!$a instanceof IEntity) {
                     $a = NULL;
                 } else {
                     if (!$a->hasParam($k)) {
                         throw new InvalidArgumentException("'{$k}' is not key in '{$key}'");
                     } else {
                         $a = $a->{$k};
                         if ($a instanceof IRelationship) {
                             $a = $a->get()->fetch();
                         }
                     }
                 }
                 if (!$b instanceof IEntity) {
                     $b = NULL;
                 } else {
                     if (!$b->hasParam($k)) {
                         throw new InvalidArgumentException("'{$k}' is not key in '{$key}'");
                     } else {
                         $b = $b->{$k};
                         if ($b instanceof IRelationship) {
                             $b = $b->get()->fetch();
                         }
                     }
                 }
             }
         } else {
             if (!$aRow->hasParam($key) or !$bRow->hasParam($key)) {
                 if (!isset($aRow->{$key}) or !isset($bRow->{$key})) {
                     throw new InvalidArgumentException("'{$key}' is not key");
                 }
             }
             $a = $aRow->{$key};
             $b = $bRow->{$key};
         }
         if (is_scalar($a) and is_scalar($b)) {
             $r = strnatcasecmp($a, $b);
         } else {
             if ($a instanceof DateTime and $b instanceof DateTime) {
                 $r = $a < $b ? -1 : 1;
             } else {
                 if ($b === NULL) {
                     $r = 1;
                 } else {
                     if ($a === NULL) {
                         $r = -1;
                     } else {
                         $tmp = 'unknown';
                         foreach (array($a, $b) as $ab) {
                             if (!is_scalar($ab) and !$ab instanceof DateTime and $ab !== NULL) {
                                 $tmp = is_object($ab) ? get_class($ab) : gettype($ab);
                                 break;
                             }
                         }
                         throw new InvalidArgumentException(get_class($aRow) . "::\${$key} contains non-sortable value, {$tmp}");
                     }
                 }
             }
         }
         if ($r !== 0) {
             break;
         }
     }
     if ($direction === self::DESC) {
         return -$r;
     }
     return $r;
 }
Ejemplo n.º 3
0
 /**
  * @param mixed
  * @param string
  * @param IEntity
  * @return scalar|NULL|DateTime
  * @throws MapperPersistenceException
  */
 protected function scalarizeValue($value, $key, IEntity $entity)
 {
     if ($value instanceof IEntityInjection) {
         $value = $value->getInjectedValue();
     }
     if ($value instanceof IEntity) {
         $value = $value->id;
     } else {
         if (is_array($value) or $value instanceof ArrayObject and get_class($value) == 'ArrayObject') {
             $value = serialize($value);
             // todo zkontrolovat jestli je jednodimenzni a neobrahuje zadne nesmysly
         } else {
             if (is_object($value) and method_exists($value, '__toString')) {
                 $value = $value->__toString();
             } else {
                 if ($value !== NULL and !$value instanceof DateTime and !is_scalar($value)) {
                     $mapper = $entity->getRepository(false) ? $entity->getRepository()->getMapper() : $this;
                     throw new MapperPersistenceException(array($mapper, $entity, $key, $value));
                 }
             }
         }
     }
     return $value;
 }