/** * Convert an entity to array using annotation reader * * @param $instance * * @return array */ private function entityToEntry($instance) { $instanceClassName = get_class($instance); $arrayInstance = array(); $r = new ReflectionClass($instanceClassName); $instanceMetadataCollection = $this->getClassMetadata($instance); $classAnnotations = $this->reader->getClassAnnotations($r); $arrayInstance['objectClass'] = array('top'); foreach ($classAnnotations as $classAnnotation) { if ($classAnnotation instanceof ObjectClass && ($classAnnotationValue = $classAnnotation->getValue()) !== '') { array_push($arrayInstance['objectClass'], $classAnnotationValue); } } foreach ($instanceMetadataCollection->getMetadatas() as $varname) { $getter = 'get' . ucfirst($instanceMetadataCollection->getKey($varname)); $setter = 'set' . ucfirst($instanceMetadataCollection->getKey($varname)); $value = $instance->{$getter}(); if ($value == null) { if ($instanceMetadataCollection->isSequence($instanceMetadataCollection->getKey($varname))) { $sequence = $instanceMetadataCollection->getSequence($instanceMetadataCollection->getKey($varname)); $value = (int) $this->generateSequenceValue($sequence); $instance->{$setter}($value); } } // Specificity of ldap (incopatibility with ldap boolean) if (is_bool($value)) { if ($value) { $value = "TRUE"; } else { $value = "FALSE"; } } if (is_object($value)) { if ($value instanceof DateTime) { $arrayInstance[$varname] = Converter::toLdapDateTime($value, false); } elseif ($value instanceof DateTimeDecorator) { $arrayInstance[$varname] = (string) $value; } else { $arrayInstance[$varname] = $this->buildEntityDn($value); } } elseif (is_array($value) && !empty($value) && isset($value[0]) && is_object($value[0])) { $valueArray = array(); foreach ($value as $val) { $valueArray[] = $this->buildEntityDn($val); } $arrayInstance[$varname] = $valueArray; } elseif (strtolower($varname) == "userpassword") { if (is_array($value)) { foreach ($value as $val) { $needle = '{CLEAR}'; if (strpos($val, $needle) === 0) { $arrayInstance[$varname] = substr($val, strlen($needle)); } } } else { $arrayInstance[$varname] = $value; } } else { $arrayInstance[$varname] = $value; } } return $arrayInstance; }
public function testToLdapDateTime_UTC() { $date = 1234556; $returned = Converter::toLdapDateTime($date, true); $this->assertEquals('19700115065556+0000', (string) $returned); }