function GetSetMethodNormalizer::isAllowedAttribute
Overrides AbstractNormalizer::isAllowedAttribute
1 call to GetSetMethodNormalizer::isAllowedAttribute()
- GetSetMethodNormalizer::extractAttributes in vendor/
symfony/ serializer/ Normalizer/ GetSetMethodNormalizer.php - Extracts attributes to normalize from the class of the given object, format and context.
File
-
vendor/
symfony/ serializer/ Normalizer/ GetSetMethodNormalizer.php, line 163
Class
- GetSetMethodNormalizer
- Converts between objects with getter and setter methods and arrays.
Namespace
Symfony\Component\Serializer\NormalizerCode
protected function isAllowedAttribute($classOrObject, string $attribute, ?string $format = null, array $context = []) : bool {
if (!parent::isAllowedAttribute($classOrObject, $attribute, $format, $context)) {
return false;
}
$class = \is_object($classOrObject) ? $classOrObject::class : $classOrObject;
if (!isset(self::$reflectionCache[$class])) {
self::$reflectionCache[$class] = new \ReflectionClass($class);
}
$reflection = self::$reflectionCache[$class];
if ($context['_read_attributes'] ?? true) {
foreach ([
'get',
'is',
'has',
] as $getterPrefix) {
$getter = $getterPrefix . $attribute;
$reflectionMethod = $reflection->hasMethod($getter) ? $reflection->getMethod($getter) : null;
if ($reflectionMethod && $this->isGetMethod($reflectionMethod)) {
return true;
}
}
return false;
}
$setter = 'set' . $attribute;
if ($reflection->hasMethod($setter) && $this->isSetMethod($reflection->getMethod($setter))) {
return true;
}
$constructor = $reflection->getConstructor();
if ($constructor && $constructor->isPublic()) {
foreach ($constructor->getParameters() as $parameter) {
if ($parameter->getName() === $attribute) {
return true;
}
}
}
return false;
}