Skip to content

Commit 96fbdc0

Browse files
committed
Renamed DataFilter to ReplaceFilter + formatting tweaks
1 parent 739d730 commit 96fbdc0

File tree

5 files changed

+131
-136
lines changed

5 files changed

+131
-136
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -128,22 +128,22 @@ $myCopy = $deepCopy->copy($myObject);
128128
// $myCopy->category has not been touched
129129
```
130130

131-
### `DataFilter`
131+
### `ReplaceFilter`
132132

133-
If you want modify data of property like value of an array you can use the `DataFilter`.
133+
If you want to replace the value of a property:
134134

135135
```php
136136
$deepCopy = new DeepCopy();
137-
$callback = function($data) {
138-
$data['options']['option1'] = false; return $data;
137+
$callback = function ($currentValue) {
138+
return $currentValue . ' (copy)'
139139
};
140-
$deepCopy->addFilter(new DataFilter($callback), new PropertyMatcher('MyClass', 'myProperty'));
140+
$deepCopy->addFilter(new ReplaceFilter($callback), new PropertyMatcher('MyClass', 'title'));
141141
$myCopy = $deepCopy->copy($myObject);
142142

143-
// $myCopy->myProperty will return data modified by your callback
143+
// $myCopy->title will contain the data returned by the callback, e.g. 'The title (copy)'
144144
```
145145

146-
**NOTE** : The "callback" parameter of the DataFilter constructor accepts any PHP callable.
146+
The `$callback` parameter of the `ReplaceFilter` constructor accepts any PHP callable.
147147

148148

149149
#### `DoctrineCollectionFilter`

src/DeepCopy/Filter/DataFilter.php

-40
This file was deleted.

src/DeepCopy/Filter/ReplaceFilter.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace DeepCopy\Filter;
4+
5+
/**
6+
* Replace the value of a property
7+
*/
8+
class ReplaceFilter implements Filter
9+
{
10+
/**
11+
* @var callable
12+
*/
13+
protected $callback;
14+
15+
/**
16+
* @param callable $callable Will be called to get the new value for each property to replace
17+
*/
18+
public function __construct(callable $callable)
19+
{
20+
$this->callback = $callable;
21+
}
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function apply($object, $property, $objectCopier)
27+
{
28+
$reflectionProperty = new \ReflectionProperty($object, $property);
29+
$reflectionProperty->setAccessible(true);
30+
31+
$value = call_user_func($this->callback, $reflectionProperty->getValue($object));
32+
33+
$reflectionProperty->setValue($object, $value);
34+
}
35+
}

tests/DeepCopyTest/Filter/DataFilterTest.php

-89
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace DeepCopyTest\Filter;
4+
5+
use DeepCopy\DeepCopy;
6+
use DeepCopy\Filter\ReplaceFilter;
7+
use DeepCopy\Matcher\PropertyMatcher;
8+
9+
class ReplaceFilterTest extends \PHPUnit_Framework_TestCase
10+
{
11+
/**
12+
* @dataProvider handlerProvider
13+
*/
14+
public function testApply(callable $callback, array $expected)
15+
{
16+
$object = new \stdClass();
17+
$object->data = ['foo' => 'bar', 'baz' => 'foo'];
18+
19+
$filter = new ReplaceFilter($callback);
20+
21+
$filter->apply($object, 'data', function () {
22+
return null;
23+
});
24+
25+
$this->assertEquals($expected, $object->data);
26+
}
27+
28+
public function handlerProvider()
29+
{
30+
$closure = function ($data) {
31+
unset($data['baz']);
32+
return $data;
33+
};
34+
35+
return [
36+
[$closure, ['foo' => 'bar']],
37+
['DeepCopyTest\Filter\Callback::copy', ['foo' => 'bar', 'baz' => 'foo', 'foobar' => 'baz']],
38+
[[new Callback(), 'callback'], ['foo' => 'foo']]
39+
];
40+
}
41+
42+
public function testIntegration()
43+
{
44+
// Prepare object to copy
45+
$object = new \StdClass();
46+
$object->data = [
47+
'foo' => 'bar',
48+
'baz' => ['bar' => 'foo'],
49+
'bar' => 'foo'
50+
];
51+
52+
// Copy
53+
$deepCopy = new DeepCopy();
54+
$deepCopy->addFilter(new ReplaceFilter(function ($data) {
55+
$data['baz']['bar'] = 'dummy_change';
56+
unset($data['bar']);
57+
58+
return $data;
59+
}), new PropertyMatcher(get_class($object), 'data'));
60+
61+
$copied = $deepCopy->copy($object);
62+
63+
// Check copied
64+
$this->assertEquals([
65+
'foo' => 'bar',
66+
'baz' => ['bar' => 'dummy_change']
67+
], $copied->data);
68+
69+
// Check original object is unchanged
70+
$this->assertEquals(
71+
['foo' => 'bar', 'baz' => ['bar' => 'foo'], 'bar' => 'foo'],
72+
$object->data
73+
);
74+
}
75+
}
76+
77+
class Callback
78+
{
79+
public static function copy($data)
80+
{
81+
$data['foobar'] = 'baz';
82+
return $data;
83+
}
84+
85+
public function callback($data)
86+
{
87+
return ['foo' => 'foo'];
88+
}
89+
}

0 commit comments

Comments
 (0)