Exemplo n.º 1
0
 /**
  * Hydrate an entity
  * @param array $data
  * @return mixed Entity
  * @throws \OpenLdapObject\Exception\InvalidHydrateException
  */
 public function hydrate(array &$data)
 {
     $entity = new $this->className();
     // To fix a bug: Ldap column name is always to lower case
     $column = array();
     foreach ($this->analyzer->listColumns() as $name => $info) {
         $column[strtolower($name)] = array_merge($info, array('realname' => $name));
     }
     foreach ($data as $key => $value) {
         $keyLow = strtolower($key);
         if (!array_key_exists($keyLow, $column)) {
             continue;
         }
         if (is_array($value) && $column[$keyLow]['type'] === 'string') {
             throw new InvalidHydrateException('Column ' . $key . ' define as a string but data is array');
         }
         if ($column[$keyLow]['type'] === 'array') {
             // $method = 'add' . Utils::capitalize($column[$keyLow]['realname']);
             if (!is_array($value)) {
                 $data[$key] = array($value);
                 $value = array($value);
             }
             $property = $this->analyzer->getReflection()->getProperty($column[$keyLow]['realname']);
             $isAccessible = $property->isPublic();
             $property->setAccessible(true);
             $property->setValue($entity, new ArrayCollection($value));
             if (!$isAccessible) {
                 $property->setAccessible(false);
             }
         } elseif ($column[$keyLow]['type'] === 'entity') {
             $multi = $this->analyzer->isEntityRelationMultiple($column[$keyLow]['realname']);
             if ($multi) {
                 $property = $this->analyzer->getReflection()->getProperty($column[$keyLow]['realname']);
                 $isAccessible = $property->isPublic();
                 $property->setAccessible(true);
                 // Manage multi entity but only one
                 if (!is_array($value)) {
                     $data[$key] = array($value);
                     $value = array($value);
                 }
                 $property->setValue($entity, new EntityCollection(EntityCollection::DN, $this->em->getRepository($column[$keyLow]['relation']['classname']), $value));
                 if (!$isAccessible) {
                     $property->setAccessible(false);
                 }
             } else {
                 $method = 'set' . Utils::capitalize($column[$keyLow]['realname']);
                 $entity->{$method}($this->repository->read($value));
             }
         } else {
             $method = 'set' . Utils::capitalize($column[$keyLow]['realname']);
             $entity->{$method}($value);
         }
     }
     return $entity;
 }
 public function testIsMultiEntityRelation()
 {
     $this->assertEquals($this->entityAnalyzer->isEntityRelationMultiple('member'), true);
     $this->assertEquals($this->entityAnalyzer->isEntityRelationMultiple('cn'), false);
 }