Skip to content

Commit 6247d82

Browse files
committed
added "skipUncloneable" config
1 parent f639d82 commit 6247d82

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/DeepCopy/DeepCopy.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ class DeepCopy
2525
*/
2626
private $filters = [];
2727

28+
private $skipUncloneable = false;
29+
30+
/**
31+
* Cloning uncloneable properties won't throw exception.
32+
* @param $skipUncloneable
33+
* @return $this
34+
*/
35+
public function skipUncloneable($skipUncloneable = true)
36+
{
37+
$this->skipUncloneable = $skipUncloneable;
38+
return $this;
39+
}
40+
2841
/**
2942
* Perform a deep copy of the object.
3043
* @param object $object
@@ -92,9 +105,9 @@ private function copyObject($object)
92105

93106
$reflectedObject = new \ReflectionObject($object);
94107

95-
if (!$reflectedObject->isCloneable()) {
108+
if (!$reflectedObject->isCloneable() and $this->skipUncloneable) {
96109
$this->hashMap[$objectHash] = $object;
97-
return $object;
110+
return $object;
98111
}
99112

100113
$newObject = clone $object;

tests/DeepCopyTest/DeepCopyTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function testNonClonableItems()
9595
{
9696
$a = new \ReflectionClass('DeepCopyTest\A');
9797
$deepCopy = new DeepCopy();
98-
$a2 = $deepCopy->copy($a);
98+
$a2 = $deepCopy->skipUncloneable()->copy($a);
9999
$this->assertSame($a, $a2);
100100
}
101101

0 commit comments

Comments
 (0)