Skip to content

Commit

Permalink
Merge pull request #5 from ProPheT777/feature-2
Browse files Browse the repository at this point in the history
Add empty collection filter
  • Loading branch information
mnapoli committed Aug 29, 2014
2 parents 42526a3 + 23ee698 commit f0cb3bd
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ $deepCopy->addFilter(new DoctrineCollectionFilter(), new PropertyTypeMatcher('Do
$myCopy = $deepCopy->copy($myObject);
```

#### `DoctrineEmptyCollection`

If you use Doctrine and want to copy an entity who contains `Collection` and you want reset her, you will need to use `DoctrineEmptyCollectionFilter`

```php
// Assuming property children is a collection who contains many elements
$deepCopy = new DeepCopy();
$deepCopy->addFilter(new DoctrineEmptyCollectionFilter(), new PropertyMatcher('MyClass', 'children'));
$myCopy = $deepCopy->copy($myObject);

// $myCopy->children will return an empty collection
```


## Contributing

Expand Down
24 changes: 24 additions & 0 deletions src/DeepCopy/Filter/Doctrine/DoctrineEmptyCollectionFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace DeepCopy\Filter\Doctrine;

use DeepCopy\Filter\Filter;
use Doctrine\Common\Collections\ArrayCollection;

class DoctrineEmptyCollectionFilter implements Filter
{
/**
* Apply the filter to the object.
*
* @param object $object
* @param string $property
* @param callable $objectCopier
*/
public function apply($object, $property, $objectCopier)
{
$reflectionProperty = new \ReflectionProperty($object, $property);
$reflectionProperty->setAccessible(true);

$reflectionProperty->setValue($object, new ArrayCollection());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* Test Doctrine Collection filter
*/
class CollectionFilterTest extends \PHPUnit_Framework_TestCase
class DoctrineCollectionFilterTest extends \PHPUnit_Framework_TestCase
{
public function testApply()
{
Expand All @@ -32,15 +32,15 @@ public function testApply()

public function testIntegration()
{
$o = new CollectionFilterTestFixture();
$o = new DoctrineCollectionFilterTestFixture();
$oldCollection = new ArrayCollection();
$oldCollectionItem = new \stdClass();
$oldCollection->add($oldCollectionItem);
$o->property1 = $oldCollection;

$deepCopy = new DeepCopy();
$deepCopy->addFilter(new DoctrineCollectionFilter(), new PropertyMatcher(get_class($o), 'property1'));
/** @var CollectionFilterTestFixture $new */
/** @var DoctrineCollectionFilterTestFixture $new */
$new = $deepCopy->copy($o);

$this->assertTrue($new->property1 instanceof Collection);
Expand All @@ -51,7 +51,7 @@ public function testIntegration()
}
}

class CollectionFilterTestFixture
class DoctrineCollectionFilterTestFixture
{
public $property1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace DeepCopyTest\Filter\Doctrine;

use DeepCopy\DeepCopy;
use DeepCopy\Filter\Doctrine\DoctrineEmptyCollectionFilter;
use DeepCopy\Matcher\PropertyMatcher;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

/**
* Test Doctrine Collection filter
*/
class DoctrineEmptyCollectionFilterTest extends \PHPUnit_Framework_TestCase
{
public function testApply()
{
$object = new \StdClass();

$collection = new ArrayCollection();
$collection->add(new \StdClass());

$object->foo = $collection;

$filter = new DoctrineEmptyCollectionFilter();
$filter->apply($object, 'foo', function($item){ return null; });

$this->assertTrue($object->foo instanceof Collection);
$this->assertNotSame($collection, $object->foo);
$this->assertTrue($object->foo->isEmpty());
}

public function testIntegration()
{
//Prepare object to copy
$doctrineEmptyCollectionFixture = new \StdClass();
$originalCollection = new ArrayCollection();
$originalCollection->add(new \StdClass());
$doctrineEmptyCollectionFixture->foo = $originalCollection;

//Copy
$deepCopy = new DeepCopy();
$deepCopy->addFilter(new DoctrineEmptyCollectionFilter(), new PropertyMatcher(get_class($doctrineEmptyCollectionFixture), 'foo'));
$copied = $deepCopy->copy($doctrineEmptyCollectionFixture);

//Check result
$this->assertTrue($copied->foo instanceof Collection);
$this->assertNotSame($originalCollection, $copied->foo);
$this->assertTrue($copied->foo->isEmpty());
}
}

0 comments on commit f0cb3bd

Please sign in to comment.