Skip to content

Commit

Permalink
Allow applying further filters after DoctrineProxyFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
fsevestre committed Apr 29, 2022
1 parent 14daed4 commit 7316d2e
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,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)

Expand Down Expand Up @@ -268,6 +271,7 @@ 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;
Expand Down
24 changes: 24 additions & 0 deletions fixtures/f013/A.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace DeepCopy\f013;

use Doctrine\Persistence\Proxy;

class A implements Proxy
{
public $foo = 1;

/**
* @inheritdoc
*/
public function __load()
{
}

/**
* @inheritdoc
*/
public function __isInitialized()
{
}
}
34 changes: 34 additions & 0 deletions fixtures/f013/B.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace DeepCopy\f013;

use Doctrine\Persistence\Proxy;

class B implements Proxy
{
private $foo;

/**
* @inheritdoc
*/
public function __load()
{
}

/**
* @inheritdoc
*/
public function __isInitialized()
{
}

public function getFoo()
{
return $this->foo;
}

public function setFoo($foo)
{
$this->foo = $foo;
}
}
13 changes: 13 additions & 0 deletions fixtures/f013/C.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace DeepCopy\f013;

class C
{
public $foo = 1;

public function __clone()
{
$this->foo = null;
}
}
5 changes: 5 additions & 0 deletions src/DeepCopy/DeepCopy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DateTimeInterface;
use DateTimeZone;
use DeepCopy\Exception\CloneException;
use DeepCopy\Filter\ChainableFilter;
use DeepCopy\Filter\Filter;
use DeepCopy\Matcher\Matcher;
use DeepCopy\Reflection\ReflectionHelper;
Expand Down Expand Up @@ -239,6 +240,10 @@ function ($object) {
}
);

if ($filter instanceof ChainableFilter) {
continue;
}

// If a filter matches, we stop processing this property
return;
}
Expand Down
10 changes: 10 additions & 0 deletions src/DeepCopy/Filter/ChainableFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace DeepCopy\Filter;

/**
* Defines a filter that will not stop the chain of filters.
*/
interface ChainableFilter extends Filter
{
}
4 changes: 2 additions & 2 deletions src/DeepCopy/Filter/Doctrine/DoctrineProxyFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace DeepCopy\Filter\Doctrine;

use DeepCopy\Filter\Filter;
use DeepCopy\Filter\ChainableFilter;

/**
* @final
*/
class DoctrineProxyFilter implements Filter
class DoctrineProxyFilter implements ChainableFilter
{
/**
* Triggers the magic method __load() on a Doctrine Proxy class to load the
Expand Down
33 changes: 33 additions & 0 deletions tests/DeepCopyTest/DeepCopyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
use DeepCopy\f009;
use DeepCopy\f011;
use DeepCopy\f012\Suit;
use DeepCopy\f013;
use DeepCopy\Filter\Doctrine\DoctrineProxyFilter;
use DeepCopy\Filter\KeepFilter;
use DeepCopy\Filter\SetNullFilter;
use DeepCopy\Matcher\Doctrine\DoctrineProxyMatcher;
use DeepCopy\Matcher\PropertyNameMatcher;
use DeepCopy\Matcher\PropertyTypeMatcher;
use DeepCopy\TypeFilter\ShallowCopyFilter;
Expand Down Expand Up @@ -508,6 +511,36 @@ public function test_it_keeps_enums()
$this->assertSame($enum, $copy);
}

/**
* @ticket https://github.com/myclabs/DeepCopy/issues/98
*/
public function test_it_can_apply_two_filters()
{
$object = new f013\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);
}

public function test_it_can_copy_property_after_applying_doctrine_proxy_filter()
{
$object = new f013\B();
$object->setFoo(new f013\C());

$deepCopy = new DeepCopy();
$deepCopy->addFilter(new DoctrineProxyFilter(), new DoctrineProxyMatcher());

/** @var f013\B $copy */
$copy = $deepCopy->copy($object);

$this->assertNotEquals($copy->getFoo(), $object->getFoo());
}

private function assertEqualButNotSame($expected, $val)
{
$this->assertEquals($expected, $val);
Expand Down

0 comments on commit 7316d2e

Please sign in to comment.