-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matthias Devlamynck
committed
Mar 11, 2024
1 parent
8528e11
commit 0e091d3
Showing
13 changed files
with
1,781 additions
and
1,950 deletions.
There are no files selected for viewing
13 changes: 6 additions & 7 deletions
13
Annotation/CsvContentTranslationPrefix.php → Attribute/CsvContentTranslationPrefix.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,19 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace RichId\CsvGeneratorBundle\Annotation; | ||
namespace RichId\CsvGeneratorBundle\Attribute; | ||
|
||
/** | ||
* Class CsvContentTranslationPrefix. | ||
* | ||
* @package RichId\CsvGeneratorBundle\Annotation | ||
* @package RichId\CsvGeneratorBundle\Attribute | ||
* @author Hugo Dumazeau <[email protected]> | ||
* @copyright 2014 - 2021 RichId (https://www.rich-id.fr) | ||
* | ||
* @Annotation | ||
* @Target({"PROPERTY"}) | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY)] | ||
class CsvContentTranslationPrefix | ||
{ | ||
/** @var string */ | ||
public $translationPrefix; | ||
public function __construct( | ||
public string $translationPrefix | ||
) {} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
<?php | ||
|
||
namespace RichId\CsvGeneratorBundle\Utility; | ||
|
||
use Symfony\Component\PropertyAccess\PropertyAccess; | ||
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; | ||
use Symfony\Component\Serializer\Exception\LogicException; | ||
use Symfony\Component\Serializer\Mapping\AttributeMetadata; | ||
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface; | ||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; | ||
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; | ||
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer; | ||
|
||
/** Copy of symfony ObjectNormalizer to override getAttributeValue() */ | ||
class ObjectNormalizer extends AbstractObjectNormalizer | ||
{ | ||
public const ALL_NULL_VALUES = 'all_null_values'; | ||
|
||
private readonly \Closure $objectClassResolver; | ||
|
||
public function __construct(?ClassMetadataFactoryInterface $classMetadataFactory = null, ?NameConverterInterface $nameConverter = null, ?PropertyTypeExtractorInterface $propertyTypeExtractor = null, ?ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, ?callable $objectClassResolver = null, array $defaultContext = []) | ||
{ | ||
if (!class_exists(PropertyAccess::class)) { | ||
throw new LogicException('The ObjectNormalizer class requires the "PropertyAccess" component. Try running "composer require symfony/property-access".'); | ||
} | ||
|
||
parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver, $objectClassResolver, $defaultContext); | ||
|
||
$this->objectClassResolver = ($objectClassResolver ?? static fn ($class) => \is_object($class) ? $class::class : $class)(...); | ||
} | ||
|
||
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool | ||
{ | ||
return parent::supportsNormalization($data, $format, $context) | ||
&& ($context[self::ALL_NULL_VALUES] ?? false); | ||
} | ||
|
||
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function getSupportedTypes(?string $format): array | ||
{ | ||
return ['object' => false]; | ||
} | ||
|
||
protected function getAttributeValue(object $object, string $attribute, ?string $format = null, array $context = []): mixed | ||
{ | ||
return null; | ||
} | ||
|
||
protected function setAttributeValue(object $object, string $attribute, mixed $value, ?string $format = null, array $context = []) | ||
{ | ||
} | ||
|
||
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; | ||
|
||
if (str_starts_with($name, 'get') || str_starts_with($name, 'has') || str_starts_with($name, 'can')) { | ||
// getters, hassers and canners | ||
$attributeName = substr($name, 3); | ||
|
||
if (!$reflClass->hasProperty($attributeName)) { | ||
$attributeName = lcfirst($attributeName); | ||
} | ||
} elseif (str_starts_with($name, 'is')) { | ||
// 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); | ||
} | ||
|
||
protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false): array|bool | ||
{ | ||
if (false === $allowedAttributes = parent::getAllowedAttributes($classOrObject, $context, $attributesAsString)) { | ||
return false; | ||
} | ||
|
||
if (null !== $this->classDiscriminatorResolver) { | ||
$class = \is_object($classOrObject) ? $classOrObject::class : $classOrObject; | ||
if (null !== $discriminatorMapping = $this->classDiscriminatorResolver->getMappingForMappedObject($classOrObject)) { | ||
$allowedAttributes[] = $attributesAsString ? $discriminatorMapping->getTypeProperty() : new AttributeMetadata($discriminatorMapping->getTypeProperty()); | ||
} | ||
|
||
if (null !== $discriminatorMapping = $this->classDiscriminatorResolver->getMappingForClass($class)) { | ||
$attributes = []; | ||
foreach ($discriminatorMapping->getTypesMapping() as $mappedClass) { | ||
$attributes[] = parent::getAllowedAttributes($mappedClass, $context, $attributesAsString); | ||
} | ||
$allowedAttributes = array_merge($allowedAttributes, ...$attributes); | ||
} | ||
} | ||
|
||
return $allowedAttributes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.