2021-12-21 10:59:23 +01:00

73 lines
1.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\Normalizer\NormalizerInterface;
use function array_merge;
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 = [])
{
$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(
$context,
['docgen:expects' => $class]
));
break;
}
}
}
return $data;
}
}