|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Isolate\LazyObjects\Proxy; |
|
4
|
|
|
|
|
5
|
|
|
use Isolate\LazyObjects\Exception\InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @api |
|
9
|
|
|
*/ |
|
10
|
|
|
class Definition |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var ClassName |
|
14
|
|
|
*/ |
|
15
|
|
|
private $className; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var LazyProperty[]|array $lazyProperties |
|
19
|
|
|
*/ |
|
20
|
|
|
private $lazyProperties; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $methodReplacements; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param ClassName $className |
|
29
|
|
|
* @param array|LazyProperty[] $lazyProperties |
|
30
|
|
|
* @param array|MethodReplacement[] $methodReplacements |
|
31
|
|
|
* @throws InvalidArgumentException |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(ClassName $className, array $lazyProperties = [], array $methodReplacements = []) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->validateLazyPropertiesDefinitions($lazyProperties); |
|
36
|
|
|
$this->validateMethodReplacementsDefinitions($methodReplacements); |
|
37
|
|
|
|
|
38
|
|
|
$this->className = $className; |
|
39
|
|
|
$this->lazyProperties = $lazyProperties; |
|
40
|
|
|
$this->methodReplacements = $methodReplacements; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return ClassName |
|
45
|
|
|
* |
|
46
|
|
|
* @api |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getClassName() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->className; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param $object |
|
55
|
|
|
* @return bool |
|
56
|
|
|
* |
|
57
|
|
|
* @api |
|
58
|
|
|
*/ |
|
59
|
|
|
public function describeProxyFor($object) |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->className->itFits($object); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return array|LazyProperty[] |
|
66
|
|
|
* |
|
67
|
|
|
* @api |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getLazyProperties() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->lazyProperties; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return array |
|
76
|
|
|
* |
|
77
|
|
|
* @api |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getMethodReplacements() |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->methodReplacements; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param array $lazyProperties |
|
86
|
|
|
* @throws InvalidArgumentException |
|
87
|
|
|
*/ |
|
88
|
|
|
private function validateLazyPropertiesDefinitions(array $lazyProperties) |
|
89
|
|
|
{ |
|
90
|
|
|
foreach ($lazyProperties as $property) { |
|
91
|
|
|
if (!$property instanceof LazyProperty) { |
|
92
|
|
|
throw new InvalidArgumentException("Proxy definitions require all properties to be an instance of Isolate\\LazyObjects\\Proxy\\LazyProperty"); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param array $methodReplacements |
|
99
|
|
|
* @throws InvalidArgumentException |
|
100
|
|
|
*/ |
|
101
|
|
|
private function validateMethodReplacementsDefinitions(array $methodReplacements) |
|
102
|
|
|
{ |
|
103
|
|
|
foreach ($methodReplacements as $methodReplacement) { |
|
104
|
|
|
if (!$methodReplacement instanceof MethodReplacement) { |
|
105
|
|
|
throw new InvalidArgumentException("Proxy definitions require all method replacements to be an instance of Isolate\\LazyObjects\\Proxy\\MethodReplacement"); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|