PHP code example of brendt / php-make-object

1. Go to this page and download the library: Download brendt/php-make-object 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/ */

    

brendt / php-make-object example snippets


$post = make(Post::class)->from($postData);

$reflectionExtractor = new ReflectionExtractor();

$phpDocExtractor = new PhpDocExtractor();

$propertyTypeExtractor = new PropertyInfoExtractor(
    listExtractors: [$reflectionExtractor],
    typeExtractors: [$phpDocExtractor, $reflectionExtractor],
    descriptionExtractors: [$phpDocExtractor],
    accessExtractors: [$reflectionExtractor],
    initializableExtractors: [$reflectionExtractor]
);

$normalizer = new ObjectNormalizer(
    propertyTypeExtractor: $propertyTypeExtractor
);

$arrayNormalizer = new ArrayDenormalizer();

$serializer = new SymfonySerializer(
    normalizers: [
        $arrayNormalizer,
        $normalizer,
    ],
    encoders: [
        new XmlEncoder(),
        new JsonEncoder(),
    ],
);

$post = $serializer->denormalize($postData, Post::class)

$post = make(Post::class)->from($postData);

$post = make(Post::class)->from([
    'title' => 'test',
]);

$post = make(Post::class)->from(<<<JSON
    {
        "title": "test"
    }
JSON);

$post = make(Post::class)->from(<<<XML
    <post>
        <title>test</title>
    </post>
XML);

$post = make(Post::class)->from(__DIR__ . '/post.json');

$post = make(Post::class)->from(new PostRequest());

final class PostRequest implements Makes
{
    public function data(): array
    {
        return [
            'title' => 'test',
        ];
    }
}

$posts = make(Post::class)->fromCollection([
    ['title' => 'a'],
    ['title' => 'b'],
    ['title' => 'c'],
]);