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.
51 lines
1.0 KiB
51 lines
1.0 KiB
<?php
|
|
|
|
namespace DeepCopy\TypeFilter\Spl;
|
|
|
|
use Closure;
|
|
use DeepCopy\DeepCopy;
|
|
use DeepCopy\TypeFilter\TypeFilter;
|
|
use SplDoublyLinkedList;
|
|
|
|
/**
|
|
* @final
|
|
*/
|
|
class SplDoublyLinkedListFilter implements TypeFilter
|
|
{
|
|
private $copier;
|
|
|
|
public function __construct(DeepCopy $copier)
|
|
{
|
|
$this->copier = $copier;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function apply($element)
|
|
{
|
|
$newElement = clone $element;
|
|
|
|
$copy = $this->createCopyClosure();
|
|
|
|
return $copy($newElement);
|
|
}
|
|
|
|
private function createCopyClosure()
|
|
{
|
|
$copier = $this->copier;
|
|
|
|
$copy = function (SplDoublyLinkedList $list) use ($copier) {
|
|
// Replace each element in the list with a deep copy of itself
|
|
for ($i = 1; $i <= $list->count(); $i++) {
|
|
$copy = $copier->recursiveCopy($list->shift());
|
|
|
|
$list->push($copy);
|
|
}
|
|
|
|
return $list;
|
|
};
|
|
|
|
return Closure::bind($copy, null, DeepCopy::class);
|
|
}
|
|
}
|
|
|