function ObjectNormalizer::extractAttributes
Overrides AbstractObjectNormalizer::extractAttributes
File
-
vendor/
symfony/ serializer/ Normalizer/ ObjectNormalizer.php, line 65
Class
- ObjectNormalizer
- Converts between objects and arrays using the PropertyAccess component.
Namespace
Symfony\Component\Serializer\NormalizerCode
protected function extractAttributes(object $object, ?string $format = null, array $context = []) : array {
if (\stdClass::class === $object::class) {
return array_keys((array) $object);
}
// If not using groups, detect manually
$attributes = [];
// methods
$class = ($this->objectClassResolver)($object);
$reflClass = new \ReflectionClass($class);
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
if (0 !== $reflMethod->getNumberOfRequiredParameters() || $reflMethod->isStatic() || $reflMethod->isConstructor() || $reflMethod->isDestructor()) {
continue;
}
$name = $reflMethod->name;
$attributeName = null;
// ctype_lower check to find out if method looks like accessor but actually is not, e.g. hash, cancel
if (3 < \strlen($name) && !ctype_lower($name[3]) && match ($name[0]) { 'g' => str_starts_with($name, 'get'),
'h' => str_starts_with($name, 'has'),
'c' => str_starts_with($name, 'can'),
default => false,
}) {
// getters, hassers and canners
$attributeName = substr($name, 3);
if (!$reflClass->hasProperty($attributeName)) {
$attributeName = lcfirst($attributeName);
}
}
elseif ('is' !== $name && str_starts_with($name, 'is') && !ctype_lower($name[2])) {
// issers
$attributeName = substr($name, 2);
if (!$reflClass->hasProperty($attributeName)) {
$attributeName = lcfirst($attributeName);
}
}
if (null !== $attributeName && $this->isAllowedAttribute($object, $attributeName, $format, $context)) {
$attributes[$attributeName] = true;
}
}
// properties
foreach ($reflClass->getProperties() as $reflProperty) {
if (!$reflProperty->isPublic()) {
continue;
}
if ($reflProperty->isStatic() || !$this->isAllowedAttribute($object, $reflProperty->name, $format, $context)) {
continue;
}
$attributes[$reflProperty->name] = true;
}
return array_keys($attributes);
}