public function testWithDisableGlobalStrict() { OpenLdapObject::disableStrictMode(); $user = $this->em->getRepository('\\OpenLdapObject\\Tests\\Manager\\People')->find('jdoe'); $this->assertEquals($user->getGivenName(), 'John'); OpenLdapObject::enableStrictMode(); }
/** * 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)) { if ($keyLow === 'objectclass') { foreach ($value as $objectClass) { if (!$entity->getObjectClass()->contains($objectClass)) { $entity->addObjectClass($objectClass); } } } continue; } if (is_array($value) && $column[$keyLow]['type'] === 'string') { if (!$column[$keyLow]['strict'] || !OpenLdapObject::isStrict()) { // If is not strict, given the first element of the array $value = reset($value); } else { 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, array('ignore_errors' => $column[$keyLow]['relation']['ignore_errors']))); if (!$isAccessible) { $property->setAccessible(false); } } else { $method = 'set' . Utils::capitalize($column[$keyLow]['realname']); $entity->{$method}($this->em->getRepository($column[$keyLow]['relation']['classname'])->read($value)); } } else { $method = 'set' . Utils::capitalize($column[$keyLow]['realname']); $entity->{$method}($value); } } return $entity; }