diff --git a/README.md b/README.md index 7f010df..b160d1e 100644 --- a/README.md +++ b/README.md @@ -189,6 +189,9 @@ $matcher = new TypeMatcher('Doctrine\Common\Collections\Collection'); - `DeepCopy\Filter` applies a transformation to the object attribute matched by `DeepCopy\Matcher` - `DeepCopy\TypeFilter` applies a transformation to any element matched by `DeepCopy\TypeMatcher` +Except a few exceptions ([`DoctrineProxyFilter`](#doctrineproxyfilter-filter)), matching a filter will stop the chain +of filters (i.e. the next ones will not be applied). + #### `SetNullFilter` (filter) @@ -271,14 +274,18 @@ Doctrine proxy class (...\\\_\_CG\_\_\Proxy). You can use the `DoctrineProxyFilter` to load the actual entity behind the Doctrine proxy class. **Make sure, though, to put this as one of your very first filters in the filter chain so that the entity is loaded before other filters are applied!** +This filter won't stop the chain of filters (i.e. the next ones may be applied). ```php use DeepCopy\DeepCopy; use DeepCopy\Filter\Doctrine\DoctrineProxyFilter; +use DeepCopy\Filter\SetNullFilter; use DeepCopy\Matcher\Doctrine\DoctrineProxyMatcher; +use DeepCopy\Matcher\PropertyNameMatcher; $copier = new DeepCopy(); $copier->addFilter(new DoctrineProxyFilter(), new DoctrineProxyMatcher()); +$copier->addFilter(new SetNullFilter(), new PropertyNameMatcher('id')); $copy = $copier->copy($object); diff --git a/fixtures/f009/A.php b/fixtures/f009/A.php new file mode 100644 index 0000000..a339034 --- /dev/null +++ b/fixtures/f009/A.php @@ -0,0 +1,24 @@ +filters as $item) { /** @var Matcher $matcher */ @@ -223,11 +226,21 @@ function ($object) { } ); + $filterWasApplied = true; + + if ($filter instanceof ChainableFilter) { + continue; + } + // If a filter matches, we stop processing this property return; } } + if ($filterWasApplied) { + return; + } + $property->setAccessible(true); $propertyValue = $property->getValue($object); diff --git a/src/DeepCopy/Filter/ChainableFilter.php b/src/DeepCopy/Filter/ChainableFilter.php new file mode 100644 index 0000000..70710ca --- /dev/null +++ b/src/DeepCopy/Filter/ChainableFilter.php @@ -0,0 +1,10 @@ +assertSame('foo', $new->getBProp()); } + /** + * @ticket https://github.com/myclabs/DeepCopy/issues/98 + */ + public function test_it_can_apply_two_filters() + { + $object = new f009\A(); + + $deepCopy = new DeepCopy(); + $deepCopy->addFilter(new DoctrineProxyFilter(), new DoctrineProxyMatcher()); + $deepCopy->addFilter(new SetNullFilter(), new PropertyNameMatcher('foo')); + + $copy = $deepCopy->copy($object); + + $this->assertNull($copy->foo); + } + private function assertEqualButNotSame($expected, $val) { $this->assertEquals($expected, $val);