Giter VIP home page Giter VIP logo

objectmerger's Introduction

#Objectmerger

With this library you have the abillity to merge two objects of the same type.

This library is under construction. Things will change.

Build status

Build Status

Installation

Composer

Add the objectmerger in your existing composer.json or create a new composer.json:

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

Now tell composer to download the library by running the command:

$ php composer.phar install

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
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.

####XML Not implemented yet. If you want to contribute -> Feel free and fork this library.

###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.

<?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:

<?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:

<?php
namespace Acme;

use Exeu\ObjectMerger\Annotation as Exeu;

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

objectmerger's People

Contributors

exeu avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

apa-opensource

objectmerger's Issues

[Feature Request] - Introduce Collection Validation

Currently there is no validation for a collection type within the CollectionHandler.
This could be dangerous!

There should be some validation is the propertyvalues traversable or are arrays.

Same for the addMissing handler.

[Feature Request] - Add a more flexible collection mergestrategy set

Currently there is only one merge strategy -> addMissing.
This mergestrategy describes that missing objects (target collection) should be added from the source collection.

$sourceCollection = array(
    $objectA,
    $objectB,
    $objectC
);

$targetCollection = array(
    $objectA,
    $objectB
);

// Mergeresult with addMissing

$targetCollection = array(
    $objectA, // merged
    $objectB, // merged
    $objectC // added
);

There should be also a mergestrategy which removes elements from the targetCollection if there is no correspondending object within the sourceCollection:

$sourceCollection = array(
    $objectA,
    $objectB
);

$targetCollection = array(
    $objectA,
    $objectB,
    $objectC
);

// Mergeresult with remove

$targetCollection = array(
    $objectA, // merged
    $objectB, // merged
);

The goal is that it is possible to combine these two strategies.

    /**
     * @Mergeable(type="Collection<Test\ObjectB>", collectionMergeStrategy={"addMissing", "remove"})
     */
    private $friends;

[Feature Request] - Add nullvalue handling

It should be possible to define how null-values are handled.

For example if the sourcevalue is null -> it should not be merged into the target property.

// Sourceobject
private $propA = null;

// Targetobject
private $propA = 'Foo';

// Mergeresult
private $propA = 'Foo'; // DON'T MERGE

For this scenario there should be some metainformation:

/**
 * @Mergeable(type="string")
 * @IgnoreNullValue
 */
private $propA;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.