2024-09-07 09:27:00 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers\CBAPowerBoard\Models;
|
|
|
|
|
|
|
|
use Symfony\Component\Serializer\Serializer;
|
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
|
|
|
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
|
|
|
|
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
|
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
|
|
|
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
|
|
|
|
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
|
|
|
|
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
|
|
|
|
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
|
|
|
|
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
|
|
|
|
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
|
|
|
|
|
|
|
|
class Parse
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-09-07 09:28:48 +02:00
|
|
|
public function encode($object_type, $document)
|
2024-09-07 09:27:00 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
$phpDocExtractor = new PhpDocExtractor();
|
|
|
|
$reflectionExtractor = new ReflectionExtractor();
|
|
|
|
|
|
|
|
// list of PropertyTypeExtractorInterface (any iterable)
|
|
|
|
$typeExtractors = [$reflectionExtractor,$phpDocExtractor];
|
|
|
|
|
|
|
|
// list of PropertyDescriptionExtractorInterface (any iterable)
|
|
|
|
$descriptionExtractors = [$phpDocExtractor];
|
|
|
|
|
|
|
|
// list of PropertyInitializableExtractorInterface (any iterable)
|
|
|
|
$propertyInitializableExtractors = [$reflectionExtractor];
|
|
|
|
|
|
|
|
$propertyInfo = new PropertyInfoExtractor(
|
|
|
|
$propertyInitializableExtractors,
|
|
|
|
$descriptionExtractors,
|
|
|
|
$typeExtractors,
|
|
|
|
);
|
|
|
|
|
|
|
|
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
|
|
|
|
|
|
|
|
$metadataAwareNameConverter = new MetadataAwareNameConverter($classMetadataFactory);
|
|
|
|
|
|
|
|
$normalizer = new ObjectNormalizer($classMetadataFactory, $metadataAwareNameConverter, null, $propertyInfo);
|
|
|
|
|
|
|
|
$normalizers = [new DateTimeNormalizer(), $normalizer, new ArrayDenormalizer()];
|
|
|
|
|
|
|
|
$encoders = [new JsonEncoder()];
|
|
|
|
|
|
|
|
$serializer = new Serializer($normalizers, $encoders);
|
2024-09-07 09:28:48 +02:00
|
|
|
|
2024-09-07 09:27:00 +02:00
|
|
|
$data = $serializer->deserialize(json_encode($document), $object_type, 'json', [\Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer::SKIP_NULL_VALUES => true]);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|