@@ -17,6 +17,8 @@ composer require myclabs/deep-copy
17
17
Use simply:
18
18
19
19
``` php
20
+ use DeepCopy\DeepCopy;
21
+
20
22
$deepCopy = new DeepCopy();
21
23
$myCopy = $deepCopy->copy($myObject);
22
24
```
@@ -77,6 +79,8 @@ We provide some generic filters and matchers.
77
79
The ` PropertyNameMatcher ` will match a property by its name:
78
80
79
81
``` php
82
+ use DeepCopy\Matcher\PropertyNameMatcher;
83
+
80
84
$matcher = new PropertyNameMatcher('id');
81
85
// will apply a filter to any property of any objects named "id"
82
86
```
@@ -86,6 +90,8 @@ $matcher = new PropertyNameMatcher('id');
86
90
The ` PropertyMatcher ` will match a specific property of a specific class:
87
91
88
92
``` php
93
+ use DeepCopy\Matcher\PropertyMatcher;
94
+
89
95
$matcher = new PropertyMatcher('MyClass', 'id');
90
96
// will apply a filter to the property "id" of any objects of the class "MyClass"
91
97
```
@@ -95,6 +101,8 @@ $matcher = new PropertyMatcher('MyClass', 'id');
95
101
The ` PropertyTypeMatcher ` will match a property by its type (instance of a class):
96
102
97
103
``` php
104
+ use DeepCopy\Matcher\PropertyTypeMatcher;
105
+
98
106
$matcher = new PropertyTypeMatcher('Doctrine\Common\Collections\Collection');
99
107
// will apply a filter to any property that is an instance of Doctrine\Common\Collections\Collection
100
108
```
@@ -106,6 +114,10 @@ $matcher = new PropertyTypeMatcher('Doctrine\Common\Collections\Collection');
106
114
Let's say for example that you are copying a database record (or a Doctrine entity), so you want the copy not to have any ID:
107
115
108
116
``` php
117
+ use DeepCopy\DeepCopy;
118
+ use DeepCopy\Filter\SetNullFilter;
119
+ use DeepCopy\Matcher\PropertyNameMatcher;
120
+
109
121
$myObject = MyClass::load(123);
110
122
echo $myObject->id; // 123
111
123
@@ -121,6 +133,10 @@ echo $myCopy->id; // null
121
133
If you want a property to remain untouched (for example, an association to an object):
122
134
123
135
``` php
136
+ use DeepCopy\DeepCopy;
137
+ use DeepCopy\Filter\KeepFilter;
138
+ use DeepCopy\Matcher\PropertyMatcher;
139
+
124
140
$deepCopy = new DeepCopy();
125
141
$deepCopy->addFilter(new KeepFilter(), new PropertyMatcher('MyClass', 'category'));
126
142
$myCopy = $deepCopy->copy($myObject);
@@ -133,6 +149,10 @@ $myCopy = $deepCopy->copy($myObject);
133
149
If you want to replace the value of a property:
134
150
135
151
``` php
152
+ use DeepCopy\DeepCopy;
153
+ use DeepCopy\Filter\ReplaceFilter;
154
+ use DeepCopy\Matcher\PropertyMatcher;
155
+
136
156
$deepCopy = new DeepCopy();
137
157
$callback = function ($currentValue) {
138
158
return $currentValue . ' (copy)'
@@ -151,6 +171,10 @@ The `$callback` parameter of the `ReplaceFilter` constructor accepts any PHP cal
151
171
If you use Doctrine and want to copy an entity, you will need to use the ` DoctrineCollectionFilter ` :
152
172
153
173
``` php
174
+ use DeepCopy\DeepCopy;
175
+ use DeepCopy\Filter\Doctrine\DoctrineCollectionFilter;
176
+ use DeepCopy\Matcher\PropertyTypeMatcher;
177
+
154
178
$deepCopy = new DeepCopy();
155
179
$deepCopy->addFilter(new DoctrineCollectionFilter(), new PropertyTypeMatcher('Doctrine\Common\Collections\Collection'));
156
180
$myCopy = $deepCopy->copy($myObject);
@@ -161,6 +185,10 @@ $myCopy = $deepCopy->copy($myObject);
161
185
If you use Doctrine and want to copy an entity who contains a ` Collection ` that you want to be reset, you can use the ` DoctrineEmptyCollectionFilter `
162
186
163
187
``` php
188
+ use DeepCopy\DeepCopy;
189
+ use DeepCopy\Filter\Doctrine\DoctrineEmptyCollectionFilter;
190
+ use DeepCopy\Matcher\PropertyMatcher;
191
+
164
192
$deepCopy = new DeepCopy();
165
193
$deepCopy->addFilter(new DoctrineEmptyCollectionFilter(), new PropertyMatcher('MyClass', 'myProperty'));
166
194
$myCopy = $deepCopy->copy($myObject);
0 commit comments