Chris@2: # DeepCopy Chris@2: Chris@2: DeepCopy helps you create deep copies (clones) of your objects. It is designed to handle cycles in the association graph. Chris@2: Chris@2: [![Build Status](https://travis-ci.org/myclabs/DeepCopy.png?branch=master)](https://travis-ci.org/myclabs/DeepCopy) Chris@2: [![Coverage Status](https://coveralls.io/repos/myclabs/DeepCopy/badge.png?branch=master)](https://coveralls.io/r/myclabs/DeepCopy?branch=master) Chris@2: [![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/myclabs/DeepCopy/badges/quality-score.png?s=2747100c19b275f93a777e3297c6c12d1b68b934)](https://scrutinizer-ci.com/g/myclabs/DeepCopy/) Chris@2: [![Total Downloads](https://poser.pugx.org/myclabs/deep-copy/downloads.svg)](https://packagist.org/packages/myclabs/deep-copy) Chris@2: Chris@2: Chris@2: ## Table of Contents Chris@2: Chris@2: 1. [How](#how) Chris@2: 1. [Why](#why) Chris@2: 1. [Using simply `clone`](#using-simply-clone) Chris@2: 1. [Overridding `__clone()`](#overridding-__clone) Chris@2: 1. [With `DeepCopy`](#with-deepcopy) Chris@2: 1. [How it works](#how-it-works) Chris@2: 1. [Going further](#going-further) Chris@2: 1. [Matchers](#matchers) Chris@2: 1. [Property name](#property-name) Chris@2: 1. [Specific property](#specific-property) Chris@2: 1. [Type](#type) Chris@2: 1. [Filters](#filters) Chris@2: 1. [`SetNullFilter`](#setnullfilter-filter) Chris@2: 1. [`KeepFilter`](#keepfilter-filter) Chris@2: 1. [`DoctrineCollectionFilter`](#doctrinecollectionfilter-filter) Chris@2: 1. [`DoctrineEmptyCollectionFilter`](#doctrineemptycollectionfilter-filter) Chris@2: 1. [`DoctrineProxyFilter`](#doctrineproxyfilter-filter) Chris@2: 1. [`ReplaceFilter`](#replacefilter-type-filter) Chris@2: 1. [`ShallowCopyFilter`](#doctrinecollectionfilter-type-filter) Chris@2: 1. [Edge cases](#edge-cases) Chris@2: 1. [Contributing](#contributing) Chris@2: 1. [Tests](#tests) Chris@2: Chris@2: Chris@2: ## How? Chris@2: Chris@2: Install with Composer: Chris@2: Chris@2: ```json Chris@2: composer require myclabs/deep-copy Chris@2: ``` Chris@2: Chris@2: Use simply: Chris@2: Chris@2: ```php Chris@2: use DeepCopy\DeepCopy; Chris@2: Chris@2: $copier = new DeepCopy(); Chris@2: $myCopy = $copier->copy($myObject); Chris@2: ``` Chris@2: Chris@2: Chris@2: ## Why? Chris@2: Chris@2: - How do you create copies of your objects? Chris@2: Chris@2: ```php Chris@2: $myCopy = clone $myObject; Chris@2: ``` Chris@2: Chris@2: - How do you create **deep** copies of your objects (i.e. copying also all the objects referenced in the properties)? Chris@2: Chris@2: You use [`__clone()`](http://www.php.net/manual/en/language.oop5.cloning.php#object.clone) and implement the behavior Chris@2: yourself. Chris@2: Chris@2: - But how do you handle **cycles** in the association graph? Chris@2: Chris@2: Now you're in for a big mess :( Chris@2: Chris@2: ![association graph](doc/graph.png) Chris@2: Chris@2: Chris@2: ### Using simply `clone` Chris@2: Chris@2: ![Using clone](doc/clone.png) Chris@2: Chris@2: Chris@2: ### Overridding `__clone()` Chris@2: Chris@2: ![Overridding __clone](doc/deep-clone.png) Chris@2: Chris@2: Chris@2: ### With `DeepCopy` Chris@2: Chris@2: ![With DeepCopy](doc/deep-copy.png) Chris@2: Chris@2: Chris@2: ## How it works Chris@2: Chris@2: DeepCopy recursively traverses all the object's properties and clones them. To avoid cloning the same object twice it Chris@2: keeps a hash map of all instances and thus preserves the object graph. Chris@2: Chris@2: To use it: Chris@2: Chris@2: ```php Chris@2: use function DeepCopy\deep_copy; Chris@2: Chris@2: $copy = deep_copy($var); Chris@2: ``` Chris@2: Chris@2: Alternatively, you can create your own `DeepCopy` instance to configure it differently for example: Chris@2: Chris@2: ```php Chris@2: use DeepCopy\DeepCopy; Chris@2: Chris@2: $copier = new DeepCopy(true); Chris@2: Chris@2: $copy = $copier->copy($var); Chris@2: ``` Chris@2: Chris@2: You may want to roll your own deep copy function: Chris@2: Chris@2: ```php Chris@2: namespace Acme; Chris@2: Chris@2: use DeepCopy\DeepCopy; Chris@2: Chris@2: function deep_copy($var) Chris@2: { Chris@2: static $copier = null; Chris@2: Chris@2: if (null === $copier) { Chris@2: $copier = new DeepCopy(true); Chris@2: } Chris@2: Chris@2: return $copier->copy($var); Chris@2: } Chris@2: ``` Chris@2: Chris@2: Chris@2: ## Going further Chris@2: Chris@2: You can add filters to customize the copy process. Chris@2: Chris@2: The method to add a filter is `DeepCopy\DeepCopy::addFilter($filter, $matcher)`, Chris@2: with `$filter` implementing `DeepCopy\Filter\Filter` Chris@2: and `$matcher` implementing `DeepCopy\Matcher\Matcher`. Chris@2: Chris@2: We provide some generic filters and matchers. Chris@2: Chris@2: Chris@2: ### Matchers Chris@2: Chris@2: - `DeepCopy\Matcher` applies on a object attribute. Chris@2: - `DeepCopy\TypeMatcher` applies on any element found in graph, including array elements. Chris@2: Chris@2: Chris@2: #### Property name Chris@2: Chris@2: The `PropertyNameMatcher` will match a property by its name: Chris@2: Chris@2: ```php Chris@2: use DeepCopy\Matcher\PropertyNameMatcher; Chris@2: Chris@2: // Will apply a filter to any property of any objects named "id" Chris@2: $matcher = new PropertyNameMatcher('id'); Chris@2: ``` Chris@2: Chris@2: Chris@2: #### Specific property Chris@2: Chris@2: The `PropertyMatcher` will match a specific property of a specific class: Chris@2: Chris@2: ```php Chris@2: use DeepCopy\Matcher\PropertyMatcher; Chris@2: Chris@2: // Will apply a filter to the property "id" of any objects of the class "MyClass" Chris@2: $matcher = new PropertyMatcher('MyClass', 'id'); Chris@2: ``` Chris@2: Chris@2: Chris@2: #### Type Chris@2: Chris@2: The `TypeMatcher` will match any element by its type (instance of a class or any value that could be parameter of Chris@2: [gettype()](http://php.net/manual/en/function.gettype.php) function): Chris@2: Chris@2: ```php Chris@2: use DeepCopy\TypeMatcher\TypeMatcher; Chris@2: Chris@2: // Will apply a filter to any object that is an instance of Doctrine\Common\Collections\Collection Chris@2: $matcher = new TypeMatcher('Doctrine\Common\Collections\Collection'); Chris@2: ``` Chris@2: Chris@2: Chris@2: ### Filters Chris@2: Chris@2: - `DeepCopy\Filter` applies a transformation to the object attribute matched by `DeepCopy\Matcher` Chris@2: - `DeepCopy\TypeFilter` applies a transformation to any element matched by `DeepCopy\TypeMatcher` Chris@2: Chris@2: Chris@2: #### `SetNullFilter` (filter) Chris@2: Chris@2: Let's say for example that you are copying a database record (or a Doctrine entity), so you want the copy not to have Chris@2: any ID: Chris@2: Chris@2: ```php Chris@2: use DeepCopy\DeepCopy; Chris@2: use DeepCopy\Filter\SetNullFilter; Chris@2: use DeepCopy\Matcher\PropertyNameMatcher; Chris@2: Chris@2: $object = MyClass::load(123); Chris@2: echo $object->id; // 123 Chris@2: Chris@2: $copier = new DeepCopy(); Chris@2: $copier->addFilter(new SetNullFilter(), new PropertyNameMatcher('id')); Chris@2: Chris@2: $copy = $copier->copy($object); Chris@2: Chris@2: echo $copy->id; // null Chris@2: ``` Chris@2: Chris@2: Chris@2: #### `KeepFilter` (filter) Chris@2: Chris@2: If you want a property to remain untouched (for example, an association to an object): Chris@2: Chris@2: ```php Chris@2: use DeepCopy\DeepCopy; Chris@2: use DeepCopy\Filter\KeepFilter; Chris@2: use DeepCopy\Matcher\PropertyMatcher; Chris@2: Chris@2: $copier = new DeepCopy(); Chris@2: $copier->addFilter(new KeepFilter(), new PropertyMatcher('MyClass', 'category')); Chris@2: Chris@2: $copy = $copier->copy($object); Chris@2: // $copy->category has not been touched Chris@2: ``` Chris@2: Chris@2: Chris@2: #### `DoctrineCollectionFilter` (filter) Chris@2: Chris@2: If you use Doctrine and want to copy an entity, you will need to use the `DoctrineCollectionFilter`: Chris@2: Chris@2: ```php Chris@2: use DeepCopy\DeepCopy; Chris@2: use DeepCopy\Filter\Doctrine\DoctrineCollectionFilter; Chris@2: use DeepCopy\Matcher\PropertyTypeMatcher; Chris@2: Chris@2: $copier = new DeepCopy(); Chris@2: $copier->addFilter(new DoctrineCollectionFilter(), new PropertyTypeMatcher('Doctrine\Common\Collections\Collection')); Chris@2: Chris@2: $copy = $copier->copy($object); Chris@2: ``` Chris@2: Chris@2: Chris@2: #### `DoctrineEmptyCollectionFilter` (filter) Chris@2: Chris@2: If you use Doctrine and want to copy an entity who contains a `Collection` that you want to be reset, you can use the Chris@2: `DoctrineEmptyCollectionFilter` Chris@2: Chris@2: ```php Chris@2: use DeepCopy\DeepCopy; Chris@2: use DeepCopy\Filter\Doctrine\DoctrineEmptyCollectionFilter; Chris@2: use DeepCopy\Matcher\PropertyMatcher; Chris@2: Chris@2: $copier = new DeepCopy(); Chris@2: $copier->addFilter(new DoctrineEmptyCollectionFilter(), new PropertyMatcher('MyClass', 'myProperty')); Chris@2: Chris@2: $copy = $copier->copy($object); Chris@2: Chris@2: // $copy->myProperty will return an empty collection Chris@2: ``` Chris@2: Chris@2: Chris@2: #### `DoctrineProxyFilter` (filter) Chris@2: Chris@2: If you use Doctrine and use cloning on lazy loaded entities, you might encounter errors mentioning missing fields on a Chris@2: Doctrine proxy class (...\\\_\_CG\_\_\Proxy). Chris@2: You can use the `DoctrineProxyFilter` to load the actual entity behind the Doctrine proxy class. Chris@2: **Make sure, though, to put this as one of your very first filters in the filter chain so that the entity is loaded Chris@2: before other filters are applied!** Chris@2: Chris@2: ```php Chris@2: use DeepCopy\DeepCopy; Chris@2: use DeepCopy\Filter\Doctrine\DoctrineProxyFilter; Chris@2: use DeepCopy\Matcher\Doctrine\DoctrineProxyMatcher; Chris@2: Chris@2: $copier = new DeepCopy(); Chris@2: $copier->addFilter(new DoctrineProxyFilter(), new DoctrineProxyMatcher()); Chris@2: Chris@2: $copy = $copier->copy($object); Chris@2: Chris@2: // $copy should now contain a clone of all entities, including those that were not yet fully loaded. Chris@2: ``` Chris@2: Chris@2: Chris@2: #### `ReplaceFilter` (type filter) Chris@2: Chris@2: 1. If you want to replace the value of a property: Chris@2: Chris@2: ```php Chris@2: use DeepCopy\DeepCopy; Chris@2: use DeepCopy\Filter\ReplaceFilter; Chris@2: use DeepCopy\Matcher\PropertyMatcher; Chris@2: Chris@2: $copier = new DeepCopy(); Chris@2: $callback = function ($currentValue) { Chris@2: return $currentValue . ' (copy)' Chris@2: }; Chris@2: $copier->addFilter(new ReplaceFilter($callback), new PropertyMatcher('MyClass', 'title')); Chris@2: Chris@2: $copy = $copier->copy($object); Chris@2: Chris@2: // $copy->title will contain the data returned by the callback, e.g. 'The title (copy)' Chris@2: ``` Chris@2: Chris@2: 2. If you want to replace whole element: Chris@2: Chris@2: ```php Chris@2: use DeepCopy\DeepCopy; Chris@2: use DeepCopy\TypeFilter\ReplaceFilter; Chris@2: use DeepCopy\TypeMatcher\TypeMatcher; Chris@2: Chris@2: $copier = new DeepCopy(); Chris@2: $callback = function (MyClass $myClass) { Chris@2: return get_class($myClass); Chris@2: }; Chris@2: $copier->addTypeFilter(new ReplaceFilter($callback), new TypeMatcher('MyClass')); Chris@2: Chris@2: $copy = $copier->copy([new MyClass, 'some string', new MyClass]); Chris@2: Chris@2: // $copy will contain ['MyClass', 'some string', 'MyClass'] Chris@2: ``` Chris@2: Chris@2: Chris@2: The `$callback` parameter of the `ReplaceFilter` constructor accepts any PHP callable. Chris@2: Chris@2: Chris@2: #### `ShallowCopyFilter` (type filter) Chris@2: Chris@2: Stop *DeepCopy* from recursively copying element, using standard `clone` instead: Chris@2: Chris@2: ```php Chris@2: use DeepCopy\DeepCopy; Chris@2: use DeepCopy\TypeFilter\ShallowCopyFilter; Chris@2: use DeepCopy\TypeMatcher\TypeMatcher; Chris@2: use Mockery as m; Chris@2: Chris@2: $this->deepCopy = new DeepCopy(); Chris@2: $this->deepCopy->addTypeFilter( Chris@2: new ShallowCopyFilter, Chris@2: new TypeMatcher(m\MockInterface::class) Chris@2: ); Chris@2: Chris@2: $myServiceWithMocks = new MyService(m::mock(MyDependency1::class), m::mock(MyDependency2::class)); Chris@2: // All mocks will be just cloned, not deep copied Chris@2: ``` Chris@2: Chris@2: Chris@2: ## Edge cases Chris@2: Chris@2: The following structures cannot be deep-copied with PHP Reflection. As a result they are shallow cloned and filters are Chris@2: not applied. There is two ways for you to handle them: Chris@2: Chris@2: - Implement your own `__clone()` method Chris@2: - Use a filter with a type matcher Chris@2: Chris@2: Chris@2: ## Contributing Chris@2: Chris@2: DeepCopy is distributed under the MIT license. Chris@2: Chris@2: Chris@2: ### Tests Chris@2: Chris@2: Running the tests is simple: Chris@2: Chris@2: ```php Chris@2: vendor/bin/phpunit Chris@2: ```