Skip to content

Commit

Permalink
Merge pull request #202 from alexz707/add-prepend-type-filter
Browse files Browse the repository at this point in the history
Add PrependTypeFilter method
  • Loading branch information
mnapoli authored Feb 12, 2025
2 parents 4764e04 + 2356a16 commit 024473a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/DeepCopy/DeepCopy.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ public function addTypeFilter(TypeFilter $filter, TypeMatcher $matcher)
];
}

public function prependTypeFilter(TypeFilter $filter, TypeMatcher $matcher)
{
array_unshift($this->typeFilters, [
'matcher' => $matcher,
'filter' => $filter,
]);
}

private function recursiveCopy($var)
{
// Matches Type Filter
Expand Down
16 changes: 16 additions & 0 deletions tests/DeepCopyTest/DeepCopyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use DeepCopy\Matcher\Doctrine\DoctrineProxyMatcher;
use DeepCopy\Matcher\PropertyNameMatcher;
use DeepCopy\Matcher\PropertyTypeMatcher;
use DeepCopy\TypeFilter\ReplaceFilter;
use DeepCopy\TypeFilter\ShallowCopyFilter;
use DeepCopy\TypeMatcher\TypeMatcher;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -475,6 +476,21 @@ public function test_it_can_prepend_filter()
$this->assertNull($copy->getFoo());
}

public function test_it_can_prepend_type_filter()
{
$object = new f008\A('bar');
$deepCopy = new DeepCopy();
$deepCopy->addTypeFilter(new ReplaceFilter(function ($object) {
return new f008\A('baz');
}), new TypeMatcher(f008\A::class));
$deepCopy->prependTypeFilter(new ReplaceFilter(function ($object) {
return new f008\A('foo');
}), new TypeMatcher(f008\A::class));

$copy = $deepCopy->copy($object);
$this->assertEquals('foo',$copy->getFoo());
}

/**
* @ticket https://github.com/myclabs/DeepCopy/issues/143
* @requires PHP 7.4
Expand Down

0 comments on commit 024473a

Please sign in to comment.