1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Isolate\LazyObjects\Object\Property; |
4
|
|
|
|
5
|
|
|
use Isolate\LazyObjects\Exception\InvalidArgumentException; |
6
|
|
|
use Isolate\LazyObjects\Object\PropertyAccessor; |
7
|
|
|
use Isolate\LazyObjects\Proxy\LazyProperty; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @api |
11
|
|
|
*/ |
12
|
|
|
final class Initializer |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $initializedProperties; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var PropertyAccessor |
21
|
|
|
*/ |
22
|
|
|
private $propertyAccessor; |
23
|
|
|
|
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
$this->propertyAccessor = new PropertyAccessor(); |
27
|
|
|
$this->initializedProperties = []; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $lazyProperties |
32
|
|
|
* @param string $triggerMethod |
33
|
|
|
* @param $targetObject |
34
|
|
|
* @throws InvalidArgumentException |
35
|
|
|
* |
36
|
|
|
* @api |
37
|
|
|
*/ |
38
|
|
|
public function initialize($lazyProperties = [], $triggerMethod, $targetObject) |
39
|
|
|
{ |
40
|
|
|
foreach ($lazyProperties as $property) { |
41
|
|
|
/* @var $property LazyProperty */ |
42
|
|
|
$this->validateLazyProperty($property); |
43
|
|
|
|
44
|
|
|
if ($this->wasInitialized($property)) { |
45
|
|
|
continue ; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (!$property->hasTriggers()) { |
49
|
|
|
$this->initializeProperty($property, $targetObject); |
50
|
|
|
continue ; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($property->isTriggeredBy($triggerMethod)) { |
54
|
|
|
$this->initializeProperty($property, $targetObject); |
55
|
|
|
continue ; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $property |
62
|
|
|
* @throws InvalidArgumentException |
63
|
|
|
*/ |
64
|
|
|
private function validateLazyProperty($property) |
65
|
|
|
{ |
66
|
|
|
if (!$property instanceof LazyProperty) { |
67
|
|
|
throw new InvalidArgumentException("Only lazy properties can be initialized"); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param LazyProperty $property |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
private function wasInitialized(LazyProperty $property) |
76
|
|
|
{ |
77
|
|
|
return in_array((string) $property->getName(), $this->initializedProperties, true); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param LazyProperty $property |
82
|
|
|
*/ |
83
|
|
|
private function markAsInitialized(LazyProperty $property) |
84
|
|
|
{ |
85
|
|
|
$this->initializedProperties[] = (string) $property->getName(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param LazyProperty $property |
90
|
|
|
* @param $targetObject |
91
|
|
|
*/ |
92
|
|
|
private function initializeProperty(LazyProperty $property, $targetObject) |
93
|
|
|
{ |
94
|
|
|
$defaultValue = $this->propertyAccessor->get($targetObject, (string)$property->getName()); |
95
|
|
|
$newValue = $property->getValueInitializer()->initialize($targetObject, $defaultValue); |
96
|
|
|
$this->propertyAccessor->set($targetObject, (string)$property->getName(), $newValue); |
97
|
|
|
$this->markAsInitialized($property); |
98
|
|
|
|
99
|
|
|
if ($property->hasInitializationCallback()) { |
100
|
|
|
$property->getInitializationCallback()->execute($defaultValue, $newValue, $targetObject); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|