PHP code example of guennichi / property-loader

1. Go to this page and download the library: Download guennichi/property-loader 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/ */

    

guennichi / property-loader example snippets




$mappingLoader = new Guennichi\PropertyLoader\Mapping\Loader\AnnotationLoader(
    new Doctrine\Common\Annotations\AnnotationReader()
);

$propertyLoader = new Guennichi\PropertyLoader\PropertyLoader($mappingLoader);


/**
 * @Annotation
 * @Target("PROPERTY")
 */
class Email extends Guennichi\PropertyLoader\Loader {
    // The source property name
    // Which we will use to generate the email
    // based on it's value.
    public string $source;
}

// in the base Guennichi\PropertyLoader\Loader class
public function handledBy(): string
{
    return static::class.'Handler';
}

namespace App\DTO;

use App\Loaders as AcmeLoad;

class Person
{
    // ...

    public string $name;
    
    /**
     * @AcmeLoad\Email(source="name") 
     */
    public string $email;
    // ...
}

$person = new Person();
$person->name = 'radhi';

// Load properties based on mappings and stored handlers
$propertyLoader->load($person, function (Email $emailLoader, ExecutionContextInterface $context {
        // Get the sourceProperty reflection object
        // based on "source" (name of property)
        $sourceProperty = $context->getClassMetadata()
        ->getReflectionClass()
        ->getProperty($emailLoader->source);
        
        $object = $context->getObject();

        $value = $sourceProperty->getValue($object) . '@guennichi.com';

        $context->getPropertyMetadata()->setPropertyValue($value, $object);
});


echo $person->email; // [email protected]