PHP code example of exeu / objectmerger

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

    

exeu / objectmerger example snippets



namespace Acme;

use Exeu\ObjectMerger\Annotation as Exeu;

class Foo
{
    /**
     * @Exeu\Mergeable(type="string")
     */
    private $bar;
    
    public function setBar($bar) { $this->bar = $bar; }
    public function getBar() { return $this->bar; }
}




use Doctrine\Common\Annotations\AnnotationReader;
use Exeu\ObjectMerger\Accessor\PropertyAccessorRegistry;
use Exeu\ObjectMerger\EventDispatcher\EventDispatcher;
use Exeu\ObjectMerger\MergeHandler\MergeHandlerRegistry;
use Exeu\ObjectMerger\Metadata\Driver\AnnotationDriver;
use Exeu\ObjectMerger\ObjectMerger;
use Metadata\MetadataFactory;

// ...

$reader  = new AnnotationReader();
$driver  = new AnnotationDriver($reader);

$metadataFactory            = new MetadataFactory($driver);
$eventDispatcher            = new EventDispatcher();
$propertyAccessorRegistry   = new PropertyAccessorRegistry();
$mergeHanlderRegistry       = new MergeHandlerRegistry();

$objectMerger = new ObjectMerger($metadataFactory, $propertyAccessorRegistry, $mergeHanlderRegistry, $eventDispatcher);

// ...

$objectA = new \Acme\Foo();
$objectA->setBar('baz');

$objectB = new \Acme\Foo();
$objectB->setBar('overwritten-baz');

$objectMerger->merge($objectA, $objectB);

echo $objectB->getBar(); // will return 'baz'



namespace Acme\Demo\Handler;

use Exeu\ObjectMerger\MergeHandlerInterface;
use Exeu\ObjectMerger\Metadata\PropertyMetadata;
use Exeu\ObjectMerger\MergeContext;

class CustomHandler implements MergeHandlerInterface
{
    public function merge(PropertyMetadata $propertyMetadata, MergeContext $context)
    {
       // DO YOUR AWESOME STUFF HERE
    }


    public function getType()
    {
        return 'MyCustomHandler';
    }
}


// bootrap as shown above.

$customMergeHandler   = new \Acme\Demo\Handler\CustomHandler();
$mergeHanlderRegistry = new MergeHandlerRegistry();
$mergeHandlerRegistry->addMergeHandler($customMergeHandler);


$objectMerger = new ObjectMerger($metadataFactory, $propertyAccessorRegistry, $mergeHanlderRegistry, $eventDispatcher);

// bootstrap as shown above.



namespace Acme;

use Exeu\ObjectMerger\Annotation as Exeu;

class Foo
{
    /**
     * @Exeu\Mergeable(type="MyCustomHandler")
     */
    private $bar;
    
    
    // ...
}

 bash
$ php composer.phar install