|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Isolate\UnitOfWork\Object; |
|
4
|
|
|
|
|
5
|
|
|
use Isolate\UnitOfWork\Exception\InvalidArgumentException; |
|
6
|
|
|
use Isolate\UnitOfWork\Exception\NotExistingPropertyException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @api |
|
10
|
|
|
*/ |
|
11
|
|
|
class PropertyCloner |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var PropertyAccessor |
|
15
|
|
|
*/ |
|
16
|
|
|
private $propertyAccessor; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->propertyAccessor = new PropertyAccessor(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param $target |
|
25
|
|
|
* @param $source |
|
26
|
|
|
* |
|
27
|
|
|
* @api |
|
28
|
|
|
*/ |
|
29
|
|
|
public function cloneProperties($target, $source) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->validaObjects($target, $source); |
|
32
|
|
|
|
|
33
|
|
|
$reflection = new \ReflectionClass($target); |
|
34
|
|
|
foreach ($reflection->getProperties() as $property) { |
|
35
|
|
|
|
|
36
|
|
|
$this->propertyAccessor->setValue( |
|
37
|
|
|
$target, |
|
38
|
|
|
$property->getName(), |
|
39
|
|
|
$this->propertyAccessor->getValue($source, $property->getName()) |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param $target |
|
46
|
|
|
* @param $source |
|
47
|
|
|
* @param $propertyName |
|
48
|
|
|
* @throws InvalidArgumentException |
|
49
|
|
|
* @throws NotExistingPropertyException |
|
50
|
|
|
* |
|
51
|
|
|
* @api |
|
52
|
|
|
*/ |
|
53
|
|
|
public function cloneProperty($target, $source, $propertyName) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->validaObjects($target, $source); |
|
56
|
|
|
|
|
57
|
|
|
$reflection = new \ReflectionClass($target); |
|
58
|
|
|
if (!$reflection->hasProperty($propertyName)) { |
|
59
|
|
|
throw new NotExistingPropertyException(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->propertyAccessor->setValue( |
|
63
|
|
|
$target, |
|
64
|
|
|
$propertyName, |
|
65
|
|
|
$this->propertyAccessor->getValue($source, $propertyName) |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param $firstObject |
|
71
|
|
|
* @param $secondObject |
|
72
|
|
|
* @throws InvalidArgumentException |
|
73
|
|
|
*/ |
|
74
|
|
View Code Duplication |
private function validaObjects($firstObject, $secondObject) |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
if (!is_object($firstObject) || !is_object($secondObject)) { |
|
77
|
|
|
throw new InvalidArgumentException("Compared values need to be a valid objects."); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (get_class($firstObject) !== get_class($secondObject)) { |
|
81
|
|
|
throw new InvalidArgumentException("Compared values need to be an instances of the same class."); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.