1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @year 2016 |
4
|
|
|
* @link https://github.com/nnx-framework/cloner |
5
|
|
|
* @author Lobanov Aleksandr <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Nnx\Cloner; |
9
|
|
|
use Assert\Assertion; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Cloner |
13
|
|
|
* |
14
|
|
|
* @package Nnx\Cloner |
15
|
|
|
*/ |
16
|
|
|
class Cloner implements ClonerInterface |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ClonerManagerInterface |
21
|
|
|
*/ |
22
|
|
|
private $clonerManager; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Options\ClonerOptions |
26
|
|
|
*/ |
27
|
|
|
private $options; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Cloner constructor. |
31
|
|
|
* |
32
|
|
|
* @param ClonerInterface $clonerManager |
33
|
|
|
* @param Options\ClonerOptions $options |
34
|
|
|
*/ |
35
|
|
|
public function __construct(ClonerInterface $clonerManager, Options\ClonerOptions $options) |
36
|
|
|
{ |
37
|
|
|
$this->clonerManager = $clonerManager; |
|
|
|
|
38
|
|
|
$this->options = $options; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param mixed |
43
|
|
|
* |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
|
|
public function cloneObject($object) |
47
|
|
|
{ |
48
|
|
|
Assertion::isObject($object); |
49
|
|
|
|
50
|
|
|
$cloneObject = clone $object; |
51
|
|
|
$this->afterClone($object, $cloneObject); |
52
|
|
|
|
53
|
|
|
foreach ($this->options->getRelations() as $relationName => $relation) { |
54
|
|
|
$cloneRelation = $this->handleRelation($object, $relationName, $relation); |
55
|
|
|
|
56
|
|
|
$relationSetter = 'set' . ucfirst($relationName); |
57
|
|
|
Assertion::methodExists($relationSetter, $object); |
58
|
|
|
|
59
|
|
|
$cloneObject->$relationSetter($cloneRelation); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $cloneObject; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param mixed $object |
67
|
|
|
* @param string $relationName |
68
|
|
|
* @param Options\Cloner\RelationOptions $options |
69
|
|
|
* |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
protected function handleRelation($object, $relationName, Options\Cloner\RelationOptions $options) |
73
|
|
|
{ |
74
|
|
|
$relationGetter = 'get' . ucfirst($relationName); |
75
|
|
|
Assertion::methodExists($relationGetter, $object); |
76
|
|
|
|
77
|
|
|
$relationData = $object->$relationGetter(); |
78
|
|
|
if ($relationData === null) { |
79
|
|
|
return null; |
80
|
|
|
} |
81
|
|
|
$cloneData = null; |
|
|
|
|
82
|
|
|
if (is_array($relationData) |
83
|
|
|
|| $relationData instanceof \Traversable |
84
|
|
|
) { |
85
|
|
|
$cloneData = []; |
86
|
|
|
foreach ($relationData as $data) { |
87
|
|
|
if ($relationArchiverResult = $this->getRelationClonerResult($options->getClonerName(), $data)) { |
88
|
|
|
$cloneData[] = $relationArchiverResult; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} else { |
92
|
|
|
$cloneData = $this->getRelationClonerResult($options->getClonerName(), $relationData); |
93
|
|
|
} |
94
|
|
|
return $cloneData; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $clonerName |
99
|
|
|
* @param mixed $relationData |
100
|
|
|
* |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
|
|
protected function getRelationClonerResult($clonerName, $relationData) |
104
|
|
|
{ |
105
|
|
|
$cloner = $this->getClonerManager()->get($clonerName); |
106
|
|
|
return $cloner->cloneObject($relationData); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param mixed $object Исходный объект |
111
|
|
|
* @param mixed $cloneObject Склонированный объект |
112
|
|
|
*/ |
113
|
|
|
protected function afterClone($object, $cloneObject) |
114
|
|
|
{ |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return ClonerManagerInterface |
119
|
|
|
*/ |
120
|
|
|
protected function getClonerManager() |
121
|
|
|
{ |
122
|
|
|
return $this->clonerManager; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return Options\ClonerOptions |
127
|
|
|
*/ |
128
|
|
|
protected function getOptions() |
129
|
|
|
{ |
130
|
|
|
return $this->options; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..