You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

375 lines
9.7 KiB

3 years ago
  1. # DeepCopy
  2. DeepCopy helps you create deep copies (clones) of your objects. It is designed to handle cycles in the association graph.
  3. [![Build Status](https://travis-ci.org/myclabs/DeepCopy.png?branch=1.x)](https://travis-ci.org/myclabs/DeepCopy)
  4. [![Coverage Status](https://coveralls.io/repos/myclabs/DeepCopy/badge.png?branch=1.x)](https://coveralls.io/r/myclabs/DeepCopy?branch=1.x)
  5. [![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/myclabs/DeepCopy/badges/quality-score.png?s=2747100c19b275f93a777e3297c6c12d1b68b934)](https://scrutinizer-ci.com/g/myclabs/DeepCopy/)
  6. [![Total Downloads](https://poser.pugx.org/myclabs/deep-copy/downloads.svg)](https://packagist.org/packages/myclabs/deep-copy)
  7. ## Table of Contents
  8. 1. [How](#how)
  9. 1. [Why](#why)
  10. 1. [Using simply `clone`](#using-simply-clone)
  11. 1. [Overridding `__clone()`](#overridding-__clone)
  12. 1. [With `DeepCopy`](#with-deepcopy)
  13. 1. [How it works](#how-it-works)
  14. 1. [Going further](#going-further)
  15. 1. [Matchers](#matchers)
  16. 1. [Property name](#property-name)
  17. 1. [Specific property](#specific-property)
  18. 1. [Type](#type)
  19. 1. [Filters](#filters)
  20. 1. [`SetNullFilter`](#setnullfilter-filter)
  21. 1. [`KeepFilter`](#keepfilter-filter)
  22. 1. [`DoctrineCollectionFilter`](#doctrinecollectionfilter-filter)
  23. 1. [`DoctrineEmptyCollectionFilter`](#doctrineemptycollectionfilter-filter)
  24. 1. [`DoctrineProxyFilter`](#doctrineproxyfilter-filter)
  25. 1. [`ReplaceFilter`](#replacefilter-type-filter)
  26. 1. [`ShallowCopyFilter`](#shallowcopyfilter-type-filter)
  27. 1. [Edge cases](#edge-cases)
  28. 1. [Contributing](#contributing)
  29. 1. [Tests](#tests)
  30. ## How?
  31. Install with Composer:
  32. ```json
  33. composer require myclabs/deep-copy
  34. ```
  35. Use simply:
  36. ```php
  37. use DeepCopy\DeepCopy;
  38. $copier = new DeepCopy();
  39. $myCopy = $copier->copy($myObject);
  40. ```
  41. ## Why?
  42. - How do you create copies of your objects?
  43. ```php
  44. $myCopy = clone $myObject;
  45. ```
  46. - How do you create **deep** copies of your objects (i.e. copying also all the objects referenced in the properties)?
  47. You use [`__clone()`](http://www.php.net/manual/en/language.oop5.cloning.php#object.clone) and implement the behavior
  48. yourself.
  49. - But how do you handle **cycles** in the association graph?
  50. Now you're in for a big mess :(
  51. ![association graph](doc/graph.png)
  52. ### Using simply `clone`
  53. ![Using clone](doc/clone.png)
  54. ### Overridding `__clone()`
  55. ![Overridding __clone](doc/deep-clone.png)
  56. ### With `DeepCopy`
  57. ![With DeepCopy](doc/deep-copy.png)
  58. ## How it works
  59. DeepCopy recursively traverses all the object's properties and clones them. To avoid cloning the same object twice it
  60. keeps a hash map of all instances and thus preserves the object graph.
  61. To use it:
  62. ```php
  63. use function DeepCopy\deep_copy;
  64. $copy = deep_copy($var);
  65. ```
  66. Alternatively, you can create your own `DeepCopy` instance to configure it differently for example:
  67. ```php
  68. use DeepCopy\DeepCopy;
  69. $copier = new DeepCopy(true);
  70. $copy = $copier->copy($var);
  71. ```
  72. You may want to roll your own deep copy function:
  73. ```php
  74. namespace Acme;
  75. use DeepCopy\DeepCopy;
  76. function deep_copy($var)
  77. {
  78. static $copier = null;
  79. if (null === $copier) {
  80. $copier = new DeepCopy(true);
  81. }
  82. return $copier->copy($var);
  83. }
  84. ```
  85. ## Going further
  86. You can add filters to customize the copy process.
  87. The method to add a filter is `DeepCopy\DeepCopy::addFilter($filter, $matcher)`,
  88. with `$filter` implementing `DeepCopy\Filter\Filter`
  89. and `$matcher` implementing `DeepCopy\Matcher\Matcher`.
  90. We provide some generic filters and matchers.
  91. ### Matchers
  92. - `DeepCopy\Matcher` applies on a object attribute.
  93. - `DeepCopy\TypeMatcher` applies on any element found in graph, including array elements.
  94. #### Property name
  95. The `PropertyNameMatcher` will match a property by its name:
  96. ```php
  97. use DeepCopy\Matcher\PropertyNameMatcher;
  98. // Will apply a filter to any property of any objects named "id"
  99. $matcher = new PropertyNameMatcher('id');
  100. ```
  101. #### Specific property
  102. The `PropertyMatcher` will match a specific property of a specific class:
  103. ```php
  104. use DeepCopy\Matcher\PropertyMatcher;
  105. // Will apply a filter to the property "id" of any objects of the class "MyClass"
  106. $matcher = new PropertyMatcher('MyClass', 'id');
  107. ```
  108. #### Type
  109. The `TypeMatcher` will match any element by its type (instance of a class or any value that could be parameter of
  110. [gettype()](http://php.net/manual/en/function.gettype.php) function):
  111. ```php
  112. use DeepCopy\TypeMatcher\TypeMatcher;
  113. // Will apply a filter to any object that is an instance of Doctrine\Common\Collections\Collection
  114. $matcher = new TypeMatcher('Doctrine\Common\Collections\Collection');
  115. ```
  116. ### Filters
  117. - `DeepCopy\Filter` applies a transformation to the object attribute matched by `DeepCopy\Matcher`
  118. - `DeepCopy\TypeFilter` applies a transformation to any element matched by `DeepCopy\TypeMatcher`
  119. #### `SetNullFilter` (filter)
  120. Let's say for example that you are copying a database record (or a Doctrine entity), so you want the copy not to have
  121. any ID:
  122. ```php
  123. use DeepCopy\DeepCopy;
  124. use DeepCopy\Filter\SetNullFilter;
  125. use DeepCopy\Matcher\PropertyNameMatcher;
  126. $object = MyClass::load(123);
  127. echo $object->id; // 123
  128. $copier = new DeepCopy();
  129. $copier->addFilter(new SetNullFilter(), new PropertyNameMatcher('id'));
  130. $copy = $copier->copy($object);
  131. echo $copy->id; // null
  132. ```
  133. #### `KeepFilter` (filter)
  134. If you want a property to remain untouched (for example, an association to an object):
  135. ```php
  136. use DeepCopy\DeepCopy;
  137. use DeepCopy\Filter\KeepFilter;
  138. use DeepCopy\Matcher\PropertyMatcher;
  139. $copier = new DeepCopy();
  140. $copier->addFilter(new KeepFilter(), new PropertyMatcher('MyClass', 'category'));
  141. $copy = $copier->copy($object);
  142. // $copy->category has not been touched
  143. ```
  144. #### `DoctrineCollectionFilter` (filter)
  145. If you use Doctrine and want to copy an entity, you will need to use the `DoctrineCollectionFilter`:
  146. ```php
  147. use DeepCopy\DeepCopy;
  148. use DeepCopy\Filter\Doctrine\DoctrineCollectionFilter;
  149. use DeepCopy\Matcher\PropertyTypeMatcher;
  150. $copier = new DeepCopy();
  151. $copier->addFilter(new DoctrineCollectionFilter(), new PropertyTypeMatcher('Doctrine\Common\Collections\Collection'));
  152. $copy = $copier->copy($object);
  153. ```
  154. #### `DoctrineEmptyCollectionFilter` (filter)
  155. If you use Doctrine and want to copy an entity who contains a `Collection` that you want to be reset, you can use the
  156. `DoctrineEmptyCollectionFilter`
  157. ```php
  158. use DeepCopy\DeepCopy;
  159. use DeepCopy\Filter\Doctrine\DoctrineEmptyCollectionFilter;
  160. use DeepCopy\Matcher\PropertyMatcher;
  161. $copier = new DeepCopy();
  162. $copier->addFilter(new DoctrineEmptyCollectionFilter(), new PropertyMatcher('MyClass', 'myProperty'));
  163. $copy = $copier->copy($object);
  164. // $copy->myProperty will return an empty collection
  165. ```
  166. #### `DoctrineProxyFilter` (filter)
  167. If you use Doctrine and use cloning on lazy loaded entities, you might encounter errors mentioning missing fields on a
  168. Doctrine proxy class (...\\\_\_CG\_\_\Proxy).
  169. You can use the `DoctrineProxyFilter` to load the actual entity behind the Doctrine proxy class.
  170. **Make sure, though, to put this as one of your very first filters in the filter chain so that the entity is loaded
  171. before other filters are applied!**
  172. ```php
  173. use DeepCopy\DeepCopy;
  174. use DeepCopy\Filter\Doctrine\DoctrineProxyFilter;
  175. use DeepCopy\Matcher\Doctrine\DoctrineProxyMatcher;
  176. $copier = new DeepCopy();
  177. $copier->addFilter(new DoctrineProxyFilter(), new DoctrineProxyMatcher());
  178. $copy = $copier->copy($object);
  179. // $copy should now contain a clone of all entities, including those that were not yet fully loaded.
  180. ```
  181. #### `ReplaceFilter` (type filter)
  182. 1. If you want to replace the value of a property:
  183. ```php
  184. use DeepCopy\DeepCopy;
  185. use DeepCopy\Filter\ReplaceFilter;
  186. use DeepCopy\Matcher\PropertyMatcher;
  187. $copier = new DeepCopy();
  188. $callback = function ($currentValue) {
  189. return $currentValue . ' (copy)'
  190. };
  191. $copier->addFilter(new ReplaceFilter($callback), new PropertyMatcher('MyClass', 'title'));
  192. $copy = $copier->copy($object);
  193. // $copy->title will contain the data returned by the callback, e.g. 'The title (copy)'
  194. ```
  195. 2. If you want to replace whole element:
  196. ```php
  197. use DeepCopy\DeepCopy;
  198. use DeepCopy\TypeFilter\ReplaceFilter;
  199. use DeepCopy\TypeMatcher\TypeMatcher;
  200. $copier = new DeepCopy();
  201. $callback = function (MyClass $myClass) {
  202. return get_class($myClass);
  203. };
  204. $copier->addTypeFilter(new ReplaceFilter($callback), new TypeMatcher('MyClass'));
  205. $copy = $copier->copy([new MyClass, 'some string', new MyClass]);
  206. // $copy will contain ['MyClass', 'some string', 'MyClass']
  207. ```
  208. The `$callback` parameter of the `ReplaceFilter` constructor accepts any PHP callable.
  209. #### `ShallowCopyFilter` (type filter)
  210. Stop *DeepCopy* from recursively copying element, using standard `clone` instead:
  211. ```php
  212. use DeepCopy\DeepCopy;
  213. use DeepCopy\TypeFilter\ShallowCopyFilter;
  214. use DeepCopy\TypeMatcher\TypeMatcher;
  215. use Mockery as m;
  216. $this->deepCopy = new DeepCopy();
  217. $this->deepCopy->addTypeFilter(
  218. new ShallowCopyFilter,
  219. new TypeMatcher(m\MockInterface::class)
  220. );
  221. $myServiceWithMocks = new MyService(m::mock(MyDependency1::class), m::mock(MyDependency2::class));
  222. // All mocks will be just cloned, not deep copied
  223. ```
  224. ## Edge cases
  225. The following structures cannot be deep-copied with PHP Reflection. As a result they are shallow cloned and filters are
  226. not applied. There is two ways for you to handle them:
  227. - Implement your own `__clone()` method
  228. - Use a filter with a type matcher
  229. ## Contributing
  230. DeepCopy is distributed under the MIT license.
  231. ### Tests
  232. Running the tests is simple:
  233. ```php
  234. vendor/bin/phpunit
  235. ```
  236. ### Support
  237. Get professional support via [the Tidelift Subscription](https://tidelift.com/subscription/pkg/packagist-myclabs-deep-copy?utm_source=packagist-myclabs-deep-copy&utm_medium=referral&utm_campaign=readme).