You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
912 B
39 lines
912 B
<?php
|
|
|
|
namespace DeepCopy\Filter;
|
|
|
|
use DeepCopy\Reflection\ReflectionHelper;
|
|
|
|
/**
|
|
* @final
|
|
*/
|
|
class ReplaceFilter implements Filter
|
|
{
|
|
/**
|
|
* @var callable
|
|
*/
|
|
protected $callback;
|
|
|
|
/**
|
|
* @param callable $callable Will be called to get the new value for each property to replace
|
|
*/
|
|
public function __construct(callable $callable)
|
|
{
|
|
$this->callback = $callable;
|
|
}
|
|
|
|
/**
|
|
* Replaces the object property by the result of the callback called with the object property.
|
|
*
|
|
* {@inheritdoc}
|
|
*/
|
|
public function apply($object, $property, $objectCopier)
|
|
{
|
|
$reflectionProperty = ReflectionHelper::getProperty($object, $property);
|
|
$reflectionProperty->setAccessible(true);
|
|
|
|
$value = call_user_func($this->callback, $reflectionProperty->getValue($object));
|
|
|
|
$reflectionProperty->setValue($object, $value);
|
|
}
|
|
}
|
|
|