public function ObjectNormalizer::normalize in Zircon Profile 8
Same name and namespace in other branches
- 8.0 vendor/symfony/serializer/Normalizer/ObjectNormalizer.php \Symfony\Component\Serializer\Normalizer\ObjectNormalizer::normalize()
Throws
Overrides NormalizerInterface::normalize
File
- vendor/
symfony/ serializer/ Normalizer/ ObjectNormalizer.php, line 54
Class
- ObjectNormalizer
- Converts between objects and arrays using the PropertyAccess component.
Namespace
Symfony\Component\Serializer\NormalizerCode
public function normalize($object, $format = null, array $context = array()) {
if ($this
->isCircularReference($object, $context)) {
return $this
->handleCircularReference($object);
}
$data = array();
$attributes = $this
->getAllowedAttributes($object, $context, true);
// If not using groups, detect manually
if (false === $attributes) {
$attributes = array();
// methods
$reflClass = new \ReflectionClass($object);
foreach ($reflClass
->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
if (!$reflMethod
->isConstructor() && !$reflMethod
->isDestructor() && 0 === $reflMethod
->getNumberOfRequiredParameters()) {
$name = $reflMethod
->getName();
if (strpos($name, 'get') === 0 || strpos($name, 'has') === 0) {
// getters and hassers
$attributes[lcfirst(substr($name, 3))] = true;
}
elseif (strpos($name, 'is') === 0) {
// issers
$attributes[lcfirst(substr($name, 2))] = true;
}
}
}
// properties
foreach ($reflClass
->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
$attributes[$reflProperty
->getName()] = true;
}
$attributes = array_keys($attributes);
}
foreach ($attributes as $attribute) {
if (in_array($attribute, $this->ignoredAttributes)) {
continue;
}
$attributeValue = $this->propertyAccessor
->getValue($object, $attribute);
if (isset($this->callbacks[$attribute])) {
$attributeValue = call_user_func($this->callbacks[$attribute], $attributeValue);
}
if (null !== $attributeValue && !is_scalar($attributeValue)) {
if (!$this->serializer instanceof NormalizerInterface) {
throw new LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $attribute));
}
$attributeValue = $this->serializer
->normalize($attributeValue, $format, $context);
}
if ($this->nameConverter) {
$attribute = $this->nameConverter
->normalize($attribute);
}
$data[$attribute] = $attributeValue;
}
return $data;
}