@@ -44,31 +44,38 @@ final class DeepCopy
44
44
private $ useCloneMethod ;
45
45
46
46
/**
47
- * @param bool $useCloneMethod If set to true, when an object implements the __clone() function, it will be used
48
- * instead of the regular deep cloning.
47
+ * @param bool $useCloneMethod If set to true, when an object implements the __clone() function, it will
48
+ * be used instead of the regular deep cloning.
49
+ * @param bool $skipUncloneable If enabled, will not throw an exception when coming across an uncloneable
50
+ * property.
51
+ * @param Array<Filter, Matcher> List of filter-matcher pairs
52
+ * @param Array<TypeFilter, TypeMatcher> List of type filter-matcher pairs
49
53
*/
50
- public function __construct (bool $ useCloneMethod = false )
51
- {
54
+ public function __construct (
55
+ bool $ useCloneMethod = false ,
56
+ bool $ skipUncloneable = true ,
57
+ array $ filters = [],
58
+ array $ typeFilters = []
59
+ ) {
52
60
$ this ->useCloneMethod = $ useCloneMethod ;
53
61
54
- $ this -> addTypeFilter (
62
+ $ filters [] = [
55
63
new DateIntervalFilter (),
56
64
new TypeMatcher (DateInterval::class)
57
- );
58
- $ this ->addTypeFilter (
65
+ ];
66
+
67
+ foreach ($ filters as [$ filter , $ matcher ]) {
68
+ $ this ->addFilter ($ filter , $ matcher );
69
+ }
70
+
71
+ $ typeFilters [] = [
59
72
new SplDoublyLinkedListFilter ($ this ),
60
73
new TypeMatcher (SplDoublyLinkedList::class)
61
- );
62
- }
74
+ ];
63
75
64
- /**
65
- * If enabled, will not throw an exception when coming across an uncloneable property.
66
- */
67
- public function skipUncloneable (bool $ skipUncloneable = true ): self
68
- {
69
- $ this ->skipUncloneable = $ skipUncloneable ;
70
-
71
- return $ this ;
76
+ foreach ($ typeFilters as [$ filter , $ matcher ]) {
77
+ $ this ->addTypeFilter ($ filter , $ matcher );
78
+ }
72
79
}
73
80
74
81
/**
@@ -85,12 +92,12 @@ public function copy($value)
85
92
return $ this ->recursiveCopy ($ value );
86
93
}
87
94
88
- public function addFilter (Filter $ filter , Matcher $ matcher ): void
95
+ private function addFilter (Filter $ filter , Matcher $ matcher ): void
89
96
{
90
97
$ this ->filters [] = [$ matcher , $ filter ];
91
98
}
92
99
93
- public function addTypeFilter (TypeFilter $ filter , TypeMatcher $ matcher ): void
100
+ private function addTypeFilter (TypeFilter $ filter , TypeMatcher $ matcher ): void
94
101
{
95
102
$ this ->typeFilters [] = [$ matcher , $ filter ];
96
103
}
@@ -146,12 +153,7 @@ private function copyObject(object $object): object
146
153
return $ object ;
147
154
}
148
155
149
- throw new CloneException (
150
- sprintf (
151
- 'The class "%s" is not cloneable. ' ,
152
- $ reflectedObject ->getName ()
153
- )
154
- );
156
+ throw CloneException::unclonableClass ($ reflectedObject ->getName ());
155
157
}
156
158
157
159
$ newObject = clone $ object ;
0 commit comments