コード例 #1
0
 /**
  * @param \StdClass   $source
  * @param object|null $entity
  *
  * @return object Hydrated object
  * @throws HydrationException
  */
 public function hydarate($source, $entity = null)
 {
     if (null === $entity) {
         $entity = $this->metadata->getReflectionClass()->newInstance();
     }
     $acessor = new PropertyAccessor();
     foreach ($this->metadata->getFieldNames() as $fieldName) {
         $property = $this->metadata->getReflectionProperty($fieldName);
         $apiField = $this->metadata->getApiFieldName($fieldName);
         try {
             $value = $acessor->getValue($source, $apiField);
         } catch (NoSuchPropertyException $exception) {
             if (!$this->metadata->getFieldMapping($fieldName)['nullable']) {
                 throw new HydrationException(sprintf('Api field %s for property %s does not present in response', $apiField, $fieldName));
             }
             $property->setValue($entity, null);
             continue;
         }
         $type = $this->manager->getConfiguration()->getTypeRegistry()->get($this->metadata->getTypeOfField($fieldName));
         $value = $type->fromApiValue($value);
         $property->setValue($entity, $value);
     }
     foreach ($this->metadata->getAssociationNames() as $fieldName) {
         $value = $this->hydrateAssociation($fieldName, $entity, $source);
         $property = $this->metadata->getReflectionProperty($fieldName);
         $property->setValue($entity, $value);
     }
     return $entity;
 }
コード例 #2
0
 /**
  * Loads a collection of entities in a one-to-many association.
  *
  * @param array                  $assoc
  * @param object                 $sourceEntity
  * @param AbstractLazyCollection $collection The collection to load/fill.
  *
  * @return Collection
  */
 public function loadOneToManyCollection(array $assoc, $sourceEntity, AbstractLazyCollection $collection)
 {
     if ($collection instanceof ApiCollection) {
         foreach ($collection->getIterator() as $entity) {
             $this->metadata->getReflectionProperty($assoc['mappedBy'])->setValue($entity, $sourceEntity);
         }
     }
     return $collection;
 }