PHP code example of lesphp / property-info-typed-array

1. Go to this page and download the library: Download lesphp/property-info-typed-array library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lesphp / property-info-typed-array example snippets


use LesPhp\PropertyInfo\TypedArray;
use LesPhp\PropertyInfo\TypedArrayAttributeExtractor;

class FooBar {
    #[TypedArray(type: Baz::class, nullable: false, keyType: 'int')]
    private array $baz;
    
    #[TypedArray(type: Baz::class, nullable: true)]
    #[TypedArray(type: 'string', nullable: true)]
    private array $bazOrString;
}

$typedArrayExtractor = new TypedArrayAttributeExtractor();

$typedArrayExtractor->getTypes(Foo_Bar::class, 'baz');
$typedArrayExtractor->getTypes(Foo_Bar::class, 'bazOrString');

use LesPhp\PropertyInfo\TypedArrayAttributeExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Serializer;

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader());
$propertyTypeExtractor = new PropertyInfoExtractor([new ReflectionExtractor()], [new TypedArrayAttributeExtractor()]);
$propertyNormalizer = new PropertyNormalizer($classMetadataFactory, null, $propertyTypeExtractor);

$serializer = new Serializer(
    [$propertyNormalizer, new ArrayDenormalizer()],
    [new JsonEncoder()]
);