-
Notifications
You must be signed in to change notification settings - Fork 83
/
Duplicator.php
85 lines (72 loc) · 2.11 KB
/
Duplicator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace Oro\Component\Duplicator;
use DeepCopy\DeepCopy;
use DeepCopy\Filter\Filter;
use DeepCopy\Matcher\Matcher;
use Oro\Component\Duplicator\Filter\FilterFactory;
use Oro\Component\Duplicator\Matcher\MatcherFactory;
class Duplicator implements DuplicatorInterface
{
/**
* @var FilterFactory
*/
protected $filterFactory;
/**
* @var MatcherFactory
*/
protected $matcherFactory;
/**
* @param object $object
* @param array $settings
* @return mixed
*/
public function duplicate($object, array $settings = [])
{
$deepCopy = new DeepCopy();
foreach ($settings as $option) {
if (!isset($option[0]) || !isset($option[1])) {
throw new \InvalidArgumentException('Invalid arguments to clone entity');
}
$filterOptions = $option[0];
$matcherArguments = $option[1];
$filter = $this->getFilter($filterOptions);
$deepCopy->addFilter($filter, $this->getMatcher($matcherArguments));
}
return $deepCopy->copy($object);
}
/**
* @param $filterOptions
* @return Filter
* @internal param array|string $filterName
*/
protected function getFilter($filterOptions)
{
$filterName = $filterOptions[0];
$filterParameters = isset($filterOptions[1]) ? $filterOptions[1] : null;
return $this->filterFactory->create($filterName, array_filter([$filterParameters]));
}
/**
* @param $matcherArguments
* @return Matcher
*/
protected function getMatcher($matcherArguments)
{
$matcherKeyword = $matcherArguments[0];
$arguments = $matcherArguments[1];
return $this->matcherFactory->create($matcherKeyword, $arguments);
}
/**
* @param FilterFactory $filterFactory
*/
public function setFilterFactory($filterFactory)
{
$this->filterFactory = $filterFactory;
}
/**
* @param MatcherFactory $matcherFactory
*/
public function setMatcherFactory($matcherFactory)
{
$this->matcherFactory = $matcherFactory;
}
}