| 148 |
lars |
1 |
# DeepCopy
|
|
|
2 |
|
|
|
3 |
DeepCopy helps you create deep copies (clones) of your objects. It is designed to handle cycles in the association graph.
|
|
|
4 |
|
|
|
5 |
[](https://packagist.org/packages/myclabs/deep-copy)
|
|
|
6 |
[](https://github.com/myclabs/DeepCopy/actions)
|
|
|
7 |
|
|
|
8 |
## Table of Contents
|
|
|
9 |
|
|
|
10 |
1. [How](#how)
|
|
|
11 |
1. [Why](#why)
|
|
|
12 |
1. [Using simply `clone`](#using-simply-clone)
|
|
|
13 |
1. [Overriding `__clone()`](#overriding-__clone)
|
|
|
14 |
1. [With `DeepCopy`](#with-deepcopy)
|
|
|
15 |
1. [How it works](#how-it-works)
|
|
|
16 |
1. [Going further](#going-further)
|
|
|
17 |
1. [Matchers](#matchers)
|
|
|
18 |
1. [Property name](#property-name)
|
|
|
19 |
1. [Specific property](#specific-property)
|
|
|
20 |
1. [Type](#type)
|
|
|
21 |
1. [Filters](#filters)
|
|
|
22 |
1. [`SetNullFilter`](#setnullfilter-filter)
|
|
|
23 |
1. [`KeepFilter`](#keepfilter-filter)
|
|
|
24 |
1. [`DoctrineCollectionFilter`](#doctrinecollectionfilter-filter)
|
|
|
25 |
1. [`DoctrineEmptyCollectionFilter`](#doctrineemptycollectionfilter-filter)
|
|
|
26 |
1. [`DoctrineProxyFilter`](#doctrineproxyfilter-filter)
|
|
|
27 |
1. [`ReplaceFilter`](#replacefilter-type-filter)
|
|
|
28 |
1. [`ShallowCopyFilter`](#shallowcopyfilter-type-filter)
|
|
|
29 |
1. [Edge cases](#edge-cases)
|
|
|
30 |
1. [Contributing](#contributing)
|
|
|
31 |
1. [Tests](#tests)
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
## How?
|
|
|
35 |
|
|
|
36 |
Install with Composer:
|
|
|
37 |
|
|
|
38 |
```
|
|
|
39 |
composer require myclabs/deep-copy
|
|
|
40 |
```
|
|
|
41 |
|
|
|
42 |
Use it:
|
|
|
43 |
|
|
|
44 |
```php
|
|
|
45 |
use DeepCopy\DeepCopy;
|
|
|
46 |
|
|
|
47 |
$copier = new DeepCopy();
|
|
|
48 |
$myCopy = $copier->copy($myObject);
|
|
|
49 |
```
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
## Why?
|
|
|
53 |
|
|
|
54 |
- How do you create copies of your objects?
|
|
|
55 |
|
|
|
56 |
```php
|
|
|
57 |
$myCopy = clone $myObject;
|
|
|
58 |
```
|
|
|
59 |
|
|
|
60 |
- How do you create **deep** copies of your objects (i.e. copying also all the objects referenced in the properties)?
|
|
|
61 |
|
|
|
62 |
You use [`__clone()`](http://www.php.net/manual/en/language.oop5.cloning.php#object.clone) and implement the behavior
|
|
|
63 |
yourself.
|
|
|
64 |
|
|
|
65 |
- But how do you handle **cycles** in the association graph?
|
|
|
66 |
|
|
|
67 |
Now you're in for a big mess :(
|
|
|
68 |
|
|
|
69 |

|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
### Using simply `clone`
|
|
|
73 |
|
|
|
74 |

|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
### Overriding `__clone()`
|
|
|
78 |
|
|
|
79 |

|
|
|
80 |
|
|
|
81 |
|
|
|
82 |
### With `DeepCopy`
|
|
|
83 |
|
|
|
84 |

|
|
|
85 |
|
|
|
86 |
|
|
|
87 |
## How it works
|
|
|
88 |
|
|
|
89 |
DeepCopy recursively traverses all the object's properties and clones them. To avoid cloning the same object twice it
|
|
|
90 |
keeps a hash map of all instances and thus preserves the object graph.
|
|
|
91 |
|
|
|
92 |
To use it:
|
|
|
93 |
|
|
|
94 |
```php
|
|
|
95 |
use function DeepCopy\deep_copy;
|
|
|
96 |
|
|
|
97 |
$copy = deep_copy($var);
|
|
|
98 |
```
|
|
|
99 |
|
|
|
100 |
Alternatively, you can create your own `DeepCopy` instance to configure it differently for example:
|
|
|
101 |
|
|
|
102 |
```php
|
|
|
103 |
use DeepCopy\DeepCopy;
|
|
|
104 |
|
|
|
105 |
$copier = new DeepCopy(true);
|
|
|
106 |
|
|
|
107 |
$copy = $copier->copy($var);
|
|
|
108 |
```
|
|
|
109 |
|
|
|
110 |
You may want to roll your own deep copy function:
|
|
|
111 |
|
|
|
112 |
```php
|
|
|
113 |
namespace Acme;
|
|
|
114 |
|
|
|
115 |
use DeepCopy\DeepCopy;
|
|
|
116 |
|
|
|
117 |
function deep_copy($var)
|
|
|
118 |
{
|
|
|
119 |
static $copier = null;
|
|
|
120 |
|
|
|
121 |
if (null === $copier) {
|
|
|
122 |
$copier = new DeepCopy(true);
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
return $copier->copy($var);
|
|
|
126 |
}
|
|
|
127 |
```
|
|
|
128 |
|
|
|
129 |
|
|
|
130 |
## Going further
|
|
|
131 |
|
|
|
132 |
You can add filters to customize the copy process.
|
|
|
133 |
|
|
|
134 |
The method to add a filter is `DeepCopy\DeepCopy::addFilter($filter, $matcher)`,
|
|
|
135 |
with `$filter` implementing `DeepCopy\Filter\Filter`
|
|
|
136 |
and `$matcher` implementing `DeepCopy\Matcher\Matcher`.
|
|
|
137 |
|
|
|
138 |
We provide some generic filters and matchers.
|
|
|
139 |
|
|
|
140 |
|
|
|
141 |
### Matchers
|
|
|
142 |
|
|
|
143 |
- `DeepCopy\Matcher` applies on a object attribute.
|
|
|
144 |
- `DeepCopy\TypeMatcher` applies on any element found in graph, including array elements.
|
|
|
145 |
|
|
|
146 |
|
|
|
147 |
#### Property name
|
|
|
148 |
|
|
|
149 |
The `PropertyNameMatcher` will match a property by its name:
|
|
|
150 |
|
|
|
151 |
```php
|
|
|
152 |
use DeepCopy\Matcher\PropertyNameMatcher;
|
|
|
153 |
|
|
|
154 |
// Will apply a filter to any property of any objects named "id"
|
|
|
155 |
$matcher = new PropertyNameMatcher('id');
|
|
|
156 |
```
|
|
|
157 |
|
|
|
158 |
|
|
|
159 |
#### Specific property
|
|
|
160 |
|
|
|
161 |
The `PropertyMatcher` will match a specific property of a specific class:
|
|
|
162 |
|
|
|
163 |
```php
|
|
|
164 |
use DeepCopy\Matcher\PropertyMatcher;
|
|
|
165 |
|
|
|
166 |
// Will apply a filter to the property "id" of any objects of the class "MyClass"
|
|
|
167 |
$matcher = new PropertyMatcher('MyClass', 'id');
|
|
|
168 |
```
|
|
|
169 |
|
|
|
170 |
|
|
|
171 |
#### Type
|
|
|
172 |
|
|
|
173 |
The `TypeMatcher` will match any element by its type (instance of a class or any value that could be parameter of
|
|
|
174 |
[gettype()](http://php.net/manual/en/function.gettype.php) function):
|
|
|
175 |
|
|
|
176 |
```php
|
|
|
177 |
use DeepCopy\TypeMatcher\TypeMatcher;
|
|
|
178 |
|
|
|
179 |
// Will apply a filter to any object that is an instance of Doctrine\Common\Collections\Collection
|
|
|
180 |
$matcher = new TypeMatcher('Doctrine\Common\Collections\Collection');
|
|
|
181 |
```
|
|
|
182 |
|
|
|
183 |
|
|
|
184 |
### Filters
|
|
|
185 |
|
|
|
186 |
- `DeepCopy\Filter` applies a transformation to the object attribute matched by `DeepCopy\Matcher`
|
|
|
187 |
- `DeepCopy\TypeFilter` applies a transformation to any element matched by `DeepCopy\TypeMatcher`
|
|
|
188 |
|
|
|
189 |
|
|
|
190 |
#### `SetNullFilter` (filter)
|
|
|
191 |
|
|
|
192 |
Let's say for example that you are copying a database record (or a Doctrine entity), so you want the copy not to have
|
|
|
193 |
any ID:
|
|
|
194 |
|
|
|
195 |
```php
|
|
|
196 |
use DeepCopy\DeepCopy;
|
|
|
197 |
use DeepCopy\Filter\SetNullFilter;
|
|
|
198 |
use DeepCopy\Matcher\PropertyNameMatcher;
|
|
|
199 |
|
|
|
200 |
$object = MyClass::load(123);
|
|
|
201 |
echo $object->id; // 123
|
|
|
202 |
|
|
|
203 |
$copier = new DeepCopy();
|
|
|
204 |
$copier->addFilter(new SetNullFilter(), new PropertyNameMatcher('id'));
|
|
|
205 |
|
|
|
206 |
$copy = $copier->copy($object);
|
|
|
207 |
|
|
|
208 |
echo $copy->id; // null
|
|
|
209 |
```
|
|
|
210 |
|
|
|
211 |
|
|
|
212 |
#### `KeepFilter` (filter)
|
|
|
213 |
|
|
|
214 |
If you want a property to remain untouched (for example, an association to an object):
|
|
|
215 |
|
|
|
216 |
```php
|
|
|
217 |
use DeepCopy\DeepCopy;
|
|
|
218 |
use DeepCopy\Filter\KeepFilter;
|
|
|
219 |
use DeepCopy\Matcher\PropertyMatcher;
|
|
|
220 |
|
|
|
221 |
$copier = new DeepCopy();
|
|
|
222 |
$copier->addFilter(new KeepFilter(), new PropertyMatcher('MyClass', 'category'));
|
|
|
223 |
|
|
|
224 |
$copy = $copier->copy($object);
|
|
|
225 |
// $copy->category has not been touched
|
|
|
226 |
```
|
|
|
227 |
|
|
|
228 |
|
|
|
229 |
#### `DoctrineCollectionFilter` (filter)
|
|
|
230 |
|
|
|
231 |
If you use Doctrine and want to copy an entity, you will need to use the `DoctrineCollectionFilter`:
|
|
|
232 |
|
|
|
233 |
```php
|
|
|
234 |
use DeepCopy\DeepCopy;
|
|
|
235 |
use DeepCopy\Filter\Doctrine\DoctrineCollectionFilter;
|
|
|
236 |
use DeepCopy\Matcher\PropertyTypeMatcher;
|
|
|
237 |
|
|
|
238 |
$copier = new DeepCopy();
|
|
|
239 |
$copier->addFilter(new DoctrineCollectionFilter(), new PropertyTypeMatcher('Doctrine\Common\Collections\Collection'));
|
|
|
240 |
|
|
|
241 |
$copy = $copier->copy($object);
|
|
|
242 |
```
|
|
|
243 |
|
|
|
244 |
|
|
|
245 |
#### `DoctrineEmptyCollectionFilter` (filter)
|
|
|
246 |
|
|
|
247 |
If you use Doctrine and want to copy an entity who contains a `Collection` that you want to be reset, you can use the
|
|
|
248 |
`DoctrineEmptyCollectionFilter`
|
|
|
249 |
|
|
|
250 |
```php
|
|
|
251 |
use DeepCopy\DeepCopy;
|
|
|
252 |
use DeepCopy\Filter\Doctrine\DoctrineEmptyCollectionFilter;
|
|
|
253 |
use DeepCopy\Matcher\PropertyMatcher;
|
|
|
254 |
|
|
|
255 |
$copier = new DeepCopy();
|
|
|
256 |
$copier->addFilter(new DoctrineEmptyCollectionFilter(), new PropertyMatcher('MyClass', 'myProperty'));
|
|
|
257 |
|
|
|
258 |
$copy = $copier->copy($object);
|
|
|
259 |
|
|
|
260 |
// $copy->myProperty will return an empty collection
|
|
|
261 |
```
|
|
|
262 |
|
|
|
263 |
|
|
|
264 |
#### `DoctrineProxyFilter` (filter)
|
|
|
265 |
|
|
|
266 |
If you use Doctrine and use cloning on lazy loaded entities, you might encounter errors mentioning missing fields on a
|
|
|
267 |
Doctrine proxy class (...\\\_\_CG\_\_\Proxy).
|
|
|
268 |
You can use the `DoctrineProxyFilter` to load the actual entity behind the Doctrine proxy class.
|
|
|
269 |
**Make sure, though, to put this as one of your very first filters in the filter chain so that the entity is loaded
|
|
|
270 |
before other filters are applied!**
|
|
|
271 |
|
|
|
272 |
```php
|
|
|
273 |
use DeepCopy\DeepCopy;
|
|
|
274 |
use DeepCopy\Filter\Doctrine\DoctrineProxyFilter;
|
|
|
275 |
use DeepCopy\Matcher\Doctrine\DoctrineProxyMatcher;
|
|
|
276 |
|
|
|
277 |
$copier = new DeepCopy();
|
|
|
278 |
$copier->addFilter(new DoctrineProxyFilter(), new DoctrineProxyMatcher());
|
|
|
279 |
|
|
|
280 |
$copy = $copier->copy($object);
|
|
|
281 |
|
|
|
282 |
// $copy should now contain a clone of all entities, including those that were not yet fully loaded.
|
|
|
283 |
```
|
|
|
284 |
|
|
|
285 |
|
|
|
286 |
#### `ReplaceFilter` (type filter)
|
|
|
287 |
|
|
|
288 |
1. If you want to replace the value of a property:
|
|
|
289 |
|
|
|
290 |
```php
|
|
|
291 |
use DeepCopy\DeepCopy;
|
|
|
292 |
use DeepCopy\Filter\ReplaceFilter;
|
|
|
293 |
use DeepCopy\Matcher\PropertyMatcher;
|
|
|
294 |
|
|
|
295 |
$copier = new DeepCopy();
|
|
|
296 |
$callback = function ($currentValue) {
|
|
|
297 |
return $currentValue . ' (copy)'
|
|
|
298 |
};
|
|
|
299 |
$copier->addFilter(new ReplaceFilter($callback), new PropertyMatcher('MyClass', 'title'));
|
|
|
300 |
|
|
|
301 |
$copy = $copier->copy($object);
|
|
|
302 |
|
|
|
303 |
// $copy->title will contain the data returned by the callback, e.g. 'The title (copy)'
|
|
|
304 |
```
|
|
|
305 |
|
|
|
306 |
2. If you want to replace whole element:
|
|
|
307 |
|
|
|
308 |
```php
|
|
|
309 |
use DeepCopy\DeepCopy;
|
|
|
310 |
use DeepCopy\TypeFilter\ReplaceFilter;
|
|
|
311 |
use DeepCopy\TypeMatcher\TypeMatcher;
|
|
|
312 |
|
|
|
313 |
$copier = new DeepCopy();
|
|
|
314 |
$callback = function (MyClass $myClass) {
|
|
|
315 |
return get_class($myClass);
|
|
|
316 |
};
|
|
|
317 |
$copier->addTypeFilter(new ReplaceFilter($callback), new TypeMatcher('MyClass'));
|
|
|
318 |
|
|
|
319 |
$copy = $copier->copy([new MyClass, 'some string', new MyClass]);
|
|
|
320 |
|
|
|
321 |
// $copy will contain ['MyClass', 'some string', 'MyClass']
|
|
|
322 |
```
|
|
|
323 |
|
|
|
324 |
|
|
|
325 |
The `$callback` parameter of the `ReplaceFilter` constructor accepts any PHP callable.
|
|
|
326 |
|
|
|
327 |
|
|
|
328 |
#### `ShallowCopyFilter` (type filter)
|
|
|
329 |
|
|
|
330 |
Stop *DeepCopy* from recursively copying element, using standard `clone` instead:
|
|
|
331 |
|
|
|
332 |
```php
|
|
|
333 |
use DeepCopy\DeepCopy;
|
|
|
334 |
use DeepCopy\TypeFilter\ShallowCopyFilter;
|
|
|
335 |
use DeepCopy\TypeMatcher\TypeMatcher;
|
|
|
336 |
use Mockery as m;
|
|
|
337 |
|
|
|
338 |
$this->deepCopy = new DeepCopy();
|
|
|
339 |
$this->deepCopy->addTypeFilter(
|
|
|
340 |
new ShallowCopyFilter,
|
|
|
341 |
new TypeMatcher(m\MockInterface::class)
|
|
|
342 |
);
|
|
|
343 |
|
|
|
344 |
$myServiceWithMocks = new MyService(m::mock(MyDependency1::class), m::mock(MyDependency2::class));
|
|
|
345 |
// All mocks will be just cloned, not deep copied
|
|
|
346 |
```
|
|
|
347 |
|
|
|
348 |
|
|
|
349 |
## Edge cases
|
|
|
350 |
|
|
|
351 |
The following structures cannot be deep-copied with PHP Reflection. As a result they are shallow cloned and filters are
|
|
|
352 |
not applied. There is two ways for you to handle them:
|
|
|
353 |
|
|
|
354 |
- Implement your own `__clone()` method
|
|
|
355 |
- Use a filter with a type matcher
|
|
|
356 |
|
|
|
357 |
|
|
|
358 |
## Contributing
|
|
|
359 |
|
|
|
360 |
DeepCopy is distributed under the MIT license.
|
|
|
361 |
|
|
|
362 |
|
|
|
363 |
### Tests
|
|
|
364 |
|
|
|
365 |
Running the tests is simple:
|
|
|
366 |
|
|
|
367 |
```php
|
|
|
368 |
vendor/bin/phpunit
|
|
|
369 |
```
|
|
|
370 |
|
|
|
371 |
### Support
|
|
|
372 |
|
|
|
373 |
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).
|