2017 © Pedro Peláez
 

library objectmerger

Object Merger for PHP Objects.

image

exeu/objectmerger

Object Merger for PHP Objects.

  • Monday, September 8, 2014
  • by Exeu
  • Repository
  • 1 Watchers
  • 2 Stars
  • 3,009 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 4 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

Objectmerger

With this library you have the abillity to merge two objects of the same type., (*1)

This library is under construction. Things will change., (*2)

Build status

Build Status, (*3)

Installation

Composer

Add the objectmerger in your existing composer.json or create a new composer.json:, (*4)

{
    "require": {
        "exeu/objectmerger": "dev-master"
    }
}

Now tell composer to download the library by running the command:, (*5)

``` bash $ php composer.phar install, (*6)


Composer will generate the autoloader file automaticly. So you only have to include this. Typically its located in the vendor dir and its called autoload.php ##Basic Usage: ###Adding mergeable metadata Before you can merge objects you have to add some metadata about which property should be mergeable. You can achieve this out of the box by three different ways: Annotations, YAML and XML. ####Annotation ```php <?php 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; } }

YAML

Not implemented yet. If you want to contribute -> Feel free and fork this library., (*7)

XML

Not implemented yet. If you want to contribute -> Feel free and fork this library., (*8)

Using the merger

<?php

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'

Registering CustomHandler:

A Customhandler is an own created merge handler. Creating a MergeHandler gives you the power of controlling HOW a property is beeing merged. Built in are some default mergehandler like (string, int, object, etc.). Now we are at the point to create a new merge handler. First of all you have to implement the MergeHandlerInterface., (*9)

<?php

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';
    }
}

After youve Written your handler you can simply register it in the MergeHandlerRegistry:, (*10)

<?php
// 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.

After attaching the MergeHandler to the Registry it is ready to use. Just add the MergeAnnotation to your property:, (*11)

<?php
namespace Acme;

use Exeu\ObjectMerger\Annotation as Exeu;

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


    // ...
}

The Versions

08/09 2014

dev-master

9999999-dev https://github.com/Exeu/objectmerger

Object Merger for PHP Objects.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

by Jan Eichhorn

18/07 2014

0.3.0

0.3.0.0 https://github.com/Exeu/objectmerger

Object Merger for PHP Objects.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

by Jan Eichhorn

25/03 2014

0.2.0

0.2.0.0 https://github.com/Exeu/objectmerger

Object Merger for PHP Objects.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

by Jan Eichhorn

23/03 2014

0.1.0

0.1.0.0 https://github.com/Exeu/objectmerger

Object Merger for PHP Objects.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

by Jan Eichhorn

22/03 2014

0.0.3

0.0.3.0 https://github.com/Exeu/objectmerger

Object Merger for PHP Objects.

  Sources   Download

Apache-2.0

The Requires

 

by Jan Eichhorn

21/03 2014

0.0.2

0.0.2.0 https://github.com/Exeu/objectmerger

Object Merger for PHP Objects.

  Sources   Download

Apache-2.0

The Requires

 

by Jan Eichhorn

21/03 2014

0.0.1

0.0.1.0 https://github.com/Exeu/objectmerger

Object Merger for PHP Objects.

  Sources   Download

Apache-2.0

The Requires

 

by Jan Eichhorn

20/03 2014

dev-develop

dev-develop https://github.com/Exeu/objectmerger

Object Merger for PHP Objects.

  Sources   Download

Apache-2.0

The Requires

 

by Jan Eichhorn