mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 13:24:25 +00:00
96 lines
2.9 KiB
PHP
96 lines
2.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\DocGeneratorBundle\Serializer\Helper;
|
|
|
|
use Symfony\Component\Serializer\Mapping\ClassMetadata;
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
|
|
|
use function array_merge;
|
|
use function is_array;
|
|
|
|
class NormalizeNullValueHelper
|
|
{
|
|
private ?string $discriminatorType = null;
|
|
|
|
private ?string $discriminatorValue = null;
|
|
|
|
private NormalizerInterface $normalizer;
|
|
|
|
public function __construct(NormalizerInterface $normalizer, $discriminatorType = null, $discriminatorValue = null)
|
|
{
|
|
$this->normalizer = $normalizer;
|
|
$this->discriminatorType = $discriminatorType;
|
|
$this->discriminatorValue = $discriminatorValue;
|
|
}
|
|
|
|
public function normalize(array $attributes, string $format = 'docgen', ?array $context = [], ?ClassMetadata $classMetadata = null)
|
|
{
|
|
$data = [];
|
|
$data['isNull'] = true;
|
|
|
|
if (null !== $this->discriminatorType) {
|
|
$data[$this->discriminatorType] = $this->discriminatorValue;
|
|
}
|
|
|
|
foreach ($attributes as $key => $class) {
|
|
if (is_numeric($key)) {
|
|
$data[$class] = '';
|
|
} else {
|
|
switch ($class) {
|
|
case 'array':
|
|
case 'bool':
|
|
case 'double':
|
|
case 'float':
|
|
case 'int':
|
|
case 'resource':
|
|
case 'string':
|
|
case 'null':
|
|
$data[$key] = '';
|
|
|
|
break;
|
|
|
|
default:
|
|
$data[$key] = $this->normalizer->normalize(null, $format, array_merge(
|
|
$this->getContextForAttribute($key, $context, $classMetadata),
|
|
['docgen:expects' => $class]
|
|
));
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
private function getContextForAttribute(string $key, array $initialContext, ?ClassMetadata $classMetadata): array
|
|
{
|
|
if (null === $classMetadata) {
|
|
return $initialContext;
|
|
}
|
|
|
|
$attributeMetadata = $classMetadata->getAttributesMetadata()[$key] ?? null;
|
|
|
|
if (null !== $attributeMetadata) {
|
|
/** @var \Symfony\Component\Serializer\Mapping\AttributeMetadata $attributeMetadata */
|
|
$initialContext = array_merge(
|
|
$initialContext,
|
|
$attributeMetadata->getNormalizationContextForGroups(
|
|
is_array($initialContext['groups']) ? $initialContext['groups'] : [$initialContext['groups']]
|
|
)
|
|
);
|
|
}
|
|
|
|
return $initialContext;
|
|
}
|
|
}
|