1# DeepCopy 2 3DeepCopy helps you create deep copies (clones) of your objects. It is designed to handle cycles in the association graph. 4 5[![Build Status](https://travis-ci.org/myclabs/DeepCopy.png?branch=1.x)](https://travis-ci.org/myclabs/DeepCopy) 6[![Coverage Status](https://coveralls.io/repos/myclabs/DeepCopy/badge.png?branch=1.x)](https://coveralls.io/r/myclabs/DeepCopy?branch=1.x) 7[![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/myclabs/DeepCopy/badges/quality-score.png?s=2747100c19b275f93a777e3297c6c12d1b68b934)](https://scrutinizer-ci.com/g/myclabs/DeepCopy/) 8[![Total Downloads](https://poser.pugx.org/myclabs/deep-copy/downloads.svg)](https://packagist.org/packages/myclabs/deep-copy) 9 10 11**You are browsing the 1.x version, this version is in maintenance mode only. Please check the new 12[2.x](https://github.com/myclabs/DeepCopy/tree/2.x) version.** 13 14 15## Table of Contents 16 171. [How](#how) 181. [Why](#why) 19 1. [Using simply `clone`](#using-simply-clone) 20 1. [Overridding `__clone()`](#overridding-__clone) 21 1. [With `DeepCopy`](#with-deepcopy) 221. [How it works](#how-it-works) 231. [Going further](#going-further) 24 1. [Matchers](#matchers) 25 1. [Property name](#property-name) 26 1. [Specific property](#specific-property) 27 1. [Type](#type) 28 1. [Filters](#filters) 29 1. [`SetNullFilter`](#setnullfilter-filter) 30 1. [`KeepFilter`](#keepfilter-filter) 31 1. [`DoctrineCollectionFilter`](#doctrinecollectionfilter-filter) 32 1. [`DoctrineEmptyCollectionFilter`](#doctrineemptycollectionfilter-filter) 33 1. [`DoctrineProxyFilter`](#doctrineproxyfilter-filter) 34 1. [`ReplaceFilter`](#replacefilter-type-filter) 35 1. [`ShallowCopyFilter`](#shallowcopyfilter-type-filter) 361. [Edge cases](#edge-cases) 371. [Contributing](#contributing) 38 1. [Tests](#tests) 39 40 41## How? 42 43Install with Composer: 44 45```json 46composer require myclabs/deep-copy 47``` 48 49Use simply: 50 51```php 52use DeepCopy\DeepCopy; 53 54$copier = new DeepCopy(); 55$myCopy = $copier->copy($myObject); 56``` 57 58 59## Why? 60 61- How do you create copies of your objects? 62 63```php 64$myCopy = clone $myObject; 65``` 66 67- How do you create **deep** copies of your objects (i.e. copying also all the objects referenced in the properties)? 68 69You use [`__clone()`](http://www.php.net/manual/en/language.oop5.cloning.php#object.clone) and implement the behavior 70yourself. 71 72- But how do you handle **cycles** in the association graph? 73 74Now you're in for a big mess :( 75 76![association graph](doc/graph.png) 77 78 79### Using simply `clone` 80 81![Using clone](doc/clone.png) 82 83 84### Overridding `__clone()` 85 86![Overridding __clone](doc/deep-clone.png) 87 88 89### With `DeepCopy` 90 91![With DeepCopy](doc/deep-copy.png) 92 93 94## How it works 95 96DeepCopy recursively traverses all the object's properties and clones them. To avoid cloning the same object twice it 97keeps a hash map of all instances and thus preserves the object graph. 98 99To use it: 100 101```php 102use function DeepCopy\deep_copy; 103 104$copy = deep_copy($var); 105``` 106 107Alternatively, you can create your own `DeepCopy` instance to configure it differently for example: 108 109```php 110use DeepCopy\DeepCopy; 111 112$copier = new DeepCopy(true); 113 114$copy = $copier->copy($var); 115``` 116 117You may want to roll your own deep copy function: 118 119```php 120namespace Acme; 121 122use DeepCopy\DeepCopy; 123 124function deep_copy($var) 125{ 126 static $copier = null; 127 128 if (null === $copier) { 129 $copier = new DeepCopy(true); 130 } 131 132 return $copier->copy($var); 133} 134``` 135 136 137## Going further 138 139You can add filters to customize the copy process. 140 141The method to add a filter is `DeepCopy\DeepCopy::addFilter($filter, $matcher)`, 142with `$filter` implementing `DeepCopy\Filter\Filter` 143and `$matcher` implementing `DeepCopy\Matcher\Matcher`. 144 145We provide some generic filters and matchers. 146 147 148### Matchers 149 150 - `DeepCopy\Matcher` applies on a object attribute. 151 - `DeepCopy\TypeMatcher` applies on any element found in graph, including array elements. 152 153 154#### Property name 155 156The `PropertyNameMatcher` will match a property by its name: 157 158```php 159use DeepCopy\Matcher\PropertyNameMatcher; 160 161// Will apply a filter to any property of any objects named "id" 162$matcher = new PropertyNameMatcher('id'); 163``` 164 165 166#### Specific property 167 168The `PropertyMatcher` will match a specific property of a specific class: 169 170```php 171use DeepCopy\Matcher\PropertyMatcher; 172 173// Will apply a filter to the property "id" of any objects of the class "MyClass" 174$matcher = new PropertyMatcher('MyClass', 'id'); 175``` 176 177 178#### Type 179 180The `TypeMatcher` will match any element by its type (instance of a class or any value that could be parameter of 181[gettype()](http://php.net/manual/en/function.gettype.php) function): 182 183```php 184use DeepCopy\TypeMatcher\TypeMatcher; 185 186// Will apply a filter to any object that is an instance of Doctrine\Common\Collections\Collection 187$matcher = new TypeMatcher('Doctrine\Common\Collections\Collection'); 188``` 189 190 191### Filters 192 193- `DeepCopy\Filter` applies a transformation to the object attribute matched by `DeepCopy\Matcher` 194- `DeepCopy\TypeFilter` applies a transformation to any element matched by `DeepCopy\TypeMatcher` 195 196 197#### `SetNullFilter` (filter) 198 199Let's say for example that you are copying a database record (or a Doctrine entity), so you want the copy not to have 200any ID: 201 202```php 203use DeepCopy\DeepCopy; 204use DeepCopy\Filter\SetNullFilter; 205use DeepCopy\Matcher\PropertyNameMatcher; 206 207$object = MyClass::load(123); 208echo $object->id; // 123 209 210$copier = new DeepCopy(); 211$copier->addFilter(new SetNullFilter(), new PropertyNameMatcher('id')); 212 213$copy = $copier->copy($object); 214 215echo $copy->id; // null 216``` 217 218 219#### `KeepFilter` (filter) 220 221If you want a property to remain untouched (for example, an association to an object): 222 223```php 224use DeepCopy\DeepCopy; 225use DeepCopy\Filter\KeepFilter; 226use DeepCopy\Matcher\PropertyMatcher; 227 228$copier = new DeepCopy(); 229$copier->addFilter(new KeepFilter(), new PropertyMatcher('MyClass', 'category')); 230 231$copy = $copier->copy($object); 232// $copy->category has not been touched 233``` 234 235 236#### `DoctrineCollectionFilter` (filter) 237 238If you use Doctrine and want to copy an entity, you will need to use the `DoctrineCollectionFilter`: 239 240```php 241use DeepCopy\DeepCopy; 242use DeepCopy\Filter\Doctrine\DoctrineCollectionFilter; 243use DeepCopy\Matcher\PropertyTypeMatcher; 244 245$copier = new DeepCopy(); 246$copier->addFilter(new DoctrineCollectionFilter(), new PropertyTypeMatcher('Doctrine\Common\Collections\Collection')); 247 248$copy = $copier->copy($object); 249``` 250 251 252#### `DoctrineEmptyCollectionFilter` (filter) 253 254If you use Doctrine and want to copy an entity who contains a `Collection` that you want to be reset, you can use the 255`DoctrineEmptyCollectionFilter` 256 257```php 258use DeepCopy\DeepCopy; 259use DeepCopy\Filter\Doctrine\DoctrineEmptyCollectionFilter; 260use DeepCopy\Matcher\PropertyMatcher; 261 262$copier = new DeepCopy(); 263$copier->addFilter(new DoctrineEmptyCollectionFilter(), new PropertyMatcher('MyClass', 'myProperty')); 264 265$copy = $copier->copy($object); 266 267// $copy->myProperty will return an empty collection 268``` 269 270 271#### `DoctrineProxyFilter` (filter) 272 273If you use Doctrine and use cloning on lazy loaded entities, you might encounter errors mentioning missing fields on a 274Doctrine proxy class (...\\\_\_CG\_\_\Proxy). 275You can use the `DoctrineProxyFilter` to load the actual entity behind the Doctrine proxy class. 276**Make sure, though, to put this as one of your very first filters in the filter chain so that the entity is loaded 277before other filters are applied!** 278 279```php 280use DeepCopy\DeepCopy; 281use DeepCopy\Filter\Doctrine\DoctrineProxyFilter; 282use DeepCopy\Matcher\Doctrine\DoctrineProxyMatcher; 283 284$copier = new DeepCopy(); 285$copier->addFilter(new DoctrineProxyFilter(), new DoctrineProxyMatcher()); 286 287$copy = $copier->copy($object); 288 289// $copy should now contain a clone of all entities, including those that were not yet fully loaded. 290``` 291 292 293#### `ReplaceFilter` (type filter) 294 2951. If you want to replace the value of a property: 296 297```php 298use DeepCopy\DeepCopy; 299use DeepCopy\Filter\ReplaceFilter; 300use DeepCopy\Matcher\PropertyMatcher; 301 302$copier = new DeepCopy(); 303$callback = function ($currentValue) { 304 return $currentValue . ' (copy)' 305}; 306$copier->addFilter(new ReplaceFilter($callback), new PropertyMatcher('MyClass', 'title')); 307 308$copy = $copier->copy($object); 309 310// $copy->title will contain the data returned by the callback, e.g. 'The title (copy)' 311``` 312 3132. If you want to replace whole element: 314 315```php 316use DeepCopy\DeepCopy; 317use DeepCopy\TypeFilter\ReplaceFilter; 318use DeepCopy\TypeMatcher\TypeMatcher; 319 320$copier = new DeepCopy(); 321$callback = function (MyClass $myClass) { 322 return get_class($myClass); 323}; 324$copier->addTypeFilter(new ReplaceFilter($callback), new TypeMatcher('MyClass')); 325 326$copy = $copier->copy([new MyClass, 'some string', new MyClass]); 327 328// $copy will contain ['MyClass', 'some string', 'MyClass'] 329``` 330 331 332The `$callback` parameter of the `ReplaceFilter` constructor accepts any PHP callable. 333 334 335#### `ShallowCopyFilter` (type filter) 336 337Stop *DeepCopy* from recursively copying element, using standard `clone` instead: 338 339```php 340use DeepCopy\DeepCopy; 341use DeepCopy\TypeFilter\ShallowCopyFilter; 342use DeepCopy\TypeMatcher\TypeMatcher; 343use Mockery as m; 344 345$this->deepCopy = new DeepCopy(); 346$this->deepCopy->addTypeFilter( 347 new ShallowCopyFilter, 348 new TypeMatcher(m\MockInterface::class) 349); 350 351$myServiceWithMocks = new MyService(m::mock(MyDependency1::class), m::mock(MyDependency2::class)); 352// All mocks will be just cloned, not deep copied 353``` 354 355 356## Edge cases 357 358The following structures cannot be deep-copied with PHP Reflection. As a result they are shallow cloned and filters are 359not applied. There is two ways for you to handle them: 360 361- Implement your own `__clone()` method 362- Use a filter with a type matcher 363 364 365## Contributing 366 367DeepCopy is distributed under the MIT license. 368 369 370### Tests 371 372Running the tests is simple: 373 374```php 375vendor/bin/phpunit 376``` 377