Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#98 - Allow applying further filters after DoctrineProxyFilter #101

Merged
merged 1 commit into from
May 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I should add this filter since we are in the DoctrineProxyFilter part, but I believe that they will be used together most of the time.


$copy = $copier->copy($object);

Expand Down
24 changes: 24 additions & 0 deletions fixtures/f009/A.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace DeepCopy\f009;

use Doctrine\Common\Persistence\Proxy;

class A implements Proxy
{
public $foo = 1;

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

/**
* @inheritdoc
*/
public function __isInitialized()
{
}
}
13 changes: 13 additions & 0 deletions src/DeepCopy/DeepCopy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DateTimeInterface;
use DateTimeZone;
use DeepCopy\Exception\CloneException;
use DeepCopy\Filter\ChainableFilter;
use DeepCopy\Filter\Filter;
use DeepCopy\Matcher\Matcher;
use DeepCopy\TypeFilter\Date\DateIntervalFilter;
Expand Down Expand Up @@ -207,6 +208,8 @@ private function copyObjectProperty($object, ReflectionProperty $property)
return;
}

$filterWasApplied = false;

// Apply the filters
foreach ($this->filters as $item) {
/** @var Matcher $matcher */
Expand All @@ -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);

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,13 +2,13 @@

namespace DeepCopy\Filter\Doctrine;

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

/**
* @final
*/
class DoctrineProxyFilter implements Filter
class DoctrineProxyFilter implements ChainableFilter
{
/**
* Triggers the magic method __load() on a Doctrine Proxy class to load the
Expand Down
19 changes: 19 additions & 0 deletions tests/DeepCopyTest/DeepCopyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
use DeepCopy\f006;
use DeepCopy\f007;
use DeepCopy\f008;
use DeepCopy\f009;
use DeepCopy\Filter\Doctrine\DoctrineProxyFilter;
use DeepCopy\Filter\KeepFilter;
use DeepCopy\Filter\ReplaceFilter;
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 @@ -440,6 +443,22 @@ public function test_private_property_of_parent_object_copy_with_filters_and_mat
$this->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);
Expand Down