コード例 #1
0
ファイル: Hydrator.php プロジェクト: VincentBee/vbee_site
 /**
  * {@inheritdoc}
  */
 public function hydrate(array $data, $targets, $from)
 {
     $mappingPosition = $this->provider->getReversedMapping($targets);
     $mapping = $this->provider->getMapping($targets);
     $entityPool = [];
     $idsPool = [];
     // Extract objects
     foreach ($mappingPosition as $mappedValue => $position) {
         $entity = explode('.', $mappedValue)[0];
         $attribute = explode('.', $mappedValue)[1];
         // If the entity doesn't exist in the pool.
         if (!isset($entityPool[$entity])) {
             $entityPool[$entity] = [];
             $idsPool[$entity] = [];
         }
         foreach ($data as $index => $row) {
             $value = $this->extractValue($row, $mappingPosition, $mappedValue, $mapping[$mappedValue]['type']);
             if ($mapping[$mappedValue]['type'] === 'id') {
                 // If this object id doesn't exist in the entity pool.
                 if (!isset($entityPool[$entity][$value])) {
                     $entityPool[$entity][$value] = [];
                 }
                 // Save the id at the current cursor position
                 $idsPool[$entity][$index] = $value;
             }
             $entityPool[$entity][$idsPool[$entity][$index]][$attribute] = $value;
         }
     }
     // Build relations
     foreach ($mapping as $property => $propertyMapping) {
         $entity = explode('.', $property)[0];
         $attribute = explode('.', $property)[1];
         if ($propertyMapping['type'] !== 'relation') {
             continue;
         }
         foreach ($entityPool[$entity] as $id => $data) {
             if ($propertyMapping['owner']) {
                 $entityPool[$entity][$id][$propertyMapping['field']] =& $entityPool[$propertyMapping['entity']][$data[$attribute]];
             } else {
                 if (!isset($entityPool[$propertyMapping['entity']][$data[$attribute]][$propertyMapping['field']])) {
                     $entityPool[$propertyMapping['entity']][$data[$attribute]][$propertyMapping['field']] = [];
                 }
                 $entityPool[$propertyMapping['entity']][$data[$attribute]][$propertyMapping['field']][] =& $entityPool[$entity][$id];
             }
         }
     }
     foreach ($entityPool[$from] as $id => $entity) {
         $entityPool[$from][$id] = $this->getHydrator($from)->hydrate($entity);
     }
     return $entityPool[$from];
 }