Example #1
0
 public function saveChanges($primaryValue, Connection $connection, Entity $entity)
 {
     $reflection = Entity\Reflection::load($entity);
     if (!$reflection->hasPrimary()) {
         throw new Exception\InvalidArgumentException("Only entity with primary can save changes!");
     }
     $sourceAdapter = $connection->getAdapter($this->sourceReflection->getAdapterName());
     $primaryName = $reflection->getPrimaryProperty()->getName();
     switch ($entity->getChangeType()) {
         case Entity::CHANGE_ATTACH:
             $adapterQuery = $sourceAdapter->createUpdateOne($this->getSourceResource(), $this->getPrimaryKey(), $primaryValue, [$this->getReferencingKey() => $entity->{$primaryName}]);
             $sourceAdapter->execute($adapterQuery);
             break;
         default:
             break;
     }
 }
Example #2
0
 /**
  * Find records by set of primary values
  *
  * @param array $primaryValues
  * @param array $associate
  *
  * @return Entity\Collection
  *
  * @throws Exception\RepositoryException
  */
 public function findPrimaries(array $primaryValues, array $associate = [])
 {
     $entityReflection = Entity\Reflection::load($this->getEntityName());
     if (!$entityReflection->hasPrimary()) {
         throw new Exception\RepositoryException("Method can not be used because entity " . $this->getEntityName() . " has no primary property defined!");
     }
     if (empty($primaryValues)) {
         throw new Exception\RepositoryException("Values can not be empty!");
     }
     try {
         return $this->query()->select()->setFilter([$entityReflection->getPrimaryProperty()->getName() => [Filter::EQUAL => $primaryValues]])->associate($associate)->run($this->connection);
     } catch (Exception\QueryException $e) {
         throw new Exception\RepositoryException($e->getMessage());
     }
 }
Example #3
0
 /**
  * Gets data which should be serialized to JSON
  *
  * @return array
  */
 public function jsonSerialize()
 {
     $output = [];
     foreach (Entity\Reflection::load(get_called_class())->getProperties() as $propertyName => $property) {
         $value = $this->{$propertyName};
         if ($value instanceof Entity\Collection || $value instanceof Entity) {
             $output[$propertyName] = $value->jsonSerialize();
         } elseif ($value instanceof \DateTime && $property->getType() === Entity\Reflection\Property::TYPE_DATE) {
             $output[$propertyName] = (array) $value;
             $output[$propertyName]["date"] = $value->format(self::$dateFormat);
         } else {
             $output[$propertyName] = $value;
         }
     }
     return array_merge($output, $this->_getPublicPropertyValues());
 }
Example #4
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 #5
0
 /**
  * Convert entity to simple array
  *
  *  @param Entity $entity
  *
  *  @return array
  */
 public function unmapEntity(Entity $entity)
 {
     $output = [];
     foreach ($entity->getData() as $propertyName => $value) {
         $property = Entity\Reflection::load($entity)->getProperty($propertyName);
         // Skip associations & readonly
         if ($property->hasOption(Entity\Reflection\Property::OPTION_ASSOC) || !$property->isWritable()) {
             continue;
         }
         $output[$property->getName(true)] = $this->unmapValue($property, $value);
     }
     return $output;
 }
Example #6
0
 protected function getEntityReflection()
 {
     return Reflection::load($this->getRepository()->getEntityName());
 }
Example #7
0
 /**
  * Get entity by primary value
  *
  * @param mixed $value
  *
  * @return Entity|false
  */
 public function getByPrimary($value)
 {
     foreach ($this->data as $entity) {
         $primaryPropertyName = Entity\Reflection::load($entity)->getPrimaryProperty()->getName();
         $primaryValue = $entity->{$primaryPropertyName};
         if ($primaryValue === $value && $primaryValue !== null) {
             return $entity;
         }
     }
     return false;
 }