1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat/object |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Object |
8
|
|
|
* @subpackage Apparat\Object\Infrastructure |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Apparat\Object\Infrastructure\Model\Object\Apparat\Traits; |
38
|
|
|
|
39
|
|
|
use Apparat\Object\Application\Model\Object\ApplicationObjectInterface; |
40
|
|
|
use Apparat\Object\Ports\Exceptions\InvalidArgumentException; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Apparat object trait |
44
|
|
|
* |
45
|
|
|
* @package Apparat\Object |
46
|
|
|
* @subpackage Apparat\Object\Infrastructure |
47
|
|
|
* @property ApplicationObjectInterface $object |
48
|
|
|
*/ |
49
|
|
|
trait ApparatObjectTrait |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* Property mapping |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $mapping = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Generic getter |
60
|
|
|
* |
61
|
|
|
* @param string $method Method name |
62
|
|
|
* @param array $arguments Arguments |
63
|
|
|
* @throws \BadMethodCallException If the method is unknown |
64
|
|
|
*/ |
65
|
1 |
|
public function __call($method, array $arguments) |
66
|
|
|
{ |
67
|
|
|
// If a getter was called |
68
|
1 |
|
if (!strncmp('get', $method, 3)) { |
69
|
1 |
|
$property = lcfirst(substr($method, 3)); |
70
|
1 |
|
if (array_key_exists($property, $this->mapping)) { |
71
|
1 |
|
$arguments = (array)$this->mapping[$property]; |
72
|
1 |
|
$getter = 'get'.ucfirst(array_shift($arguments)); |
73
|
1 |
|
return $this->delegateObjectGetter($property, $getter, $arguments); |
74
|
|
|
} |
75
|
1 |
|
} |
76
|
|
|
|
77
|
|
|
// If the method is unknown |
78
|
1 |
|
throw new \BadMethodCallException(sprintf('Unknown apparat object method "%s()"', $method)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return whether a particular property exists |
83
|
|
|
* |
84
|
|
|
* @param string $offset Property name |
85
|
|
|
*/ |
86
|
1 |
|
public function offsetExists($offset) |
87
|
|
|
{ |
88
|
1 |
|
return array_key_exists($offset, $this->mapping); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Return a particular property |
93
|
|
|
* |
94
|
|
|
* @param string $offset Property name |
95
|
|
|
* @return mixed Property value |
96
|
|
|
*/ |
97
|
2 |
|
public function offsetGet($offset) |
98
|
|
|
{ |
99
|
|
|
// If a known object property has been requested |
100
|
2 |
|
if (array_key_exists($offset, $this->mapping)) { |
101
|
2 |
|
$arguments = (array)$this->mapping[$offset]; |
102
|
2 |
|
$property = array_shift($arguments); |
103
|
2 |
|
$getter = 'get'.ucfirst($property); |
104
|
2 |
|
return $this->delegateObjectGetter($property, $getter, $arguments); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
throw new InvalidArgumentException( |
108
|
|
|
sprintf('Invalid apparat object property "%s"', $offset), |
109
|
|
|
InvalidArgumentException::INVALID_APPARAT_OBJECT_PROPERTY |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set a particular property |
115
|
|
|
* |
116
|
|
|
* @param string $offset Property name |
117
|
|
|
* @param mixed $value Property value |
118
|
|
|
*/ |
119
|
1 |
|
public function offsetSet($offset, $value) |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
// TODO: Implement offsetSet() method. |
122
|
1 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Unset a particular property |
126
|
|
|
* |
127
|
|
|
* @param string $offset Property name |
128
|
|
|
*/ |
129
|
1 |
|
public function offsetUnset($offset) |
130
|
|
|
{ |
131
|
1 |
|
throw new InvalidArgumentException( |
132
|
1 |
|
sprintf('Cannot unset apparat object property "%s"', $offset), |
133
|
|
|
InvalidArgumentException::CANNOT_UNSET_APPARAT_OBJECT_PROPERTY |
134
|
1 |
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Delegate the mapped object getter |
139
|
|
|
* |
140
|
|
|
* @param string $property Property name |
141
|
|
|
* @param string $getter Getter name |
142
|
|
|
* @param array $arguments Getter arguments |
143
|
|
|
* @return mixed Property value |
144
|
|
|
* @throws InvalidArgumentException If the property is invalid |
145
|
|
|
*/ |
146
|
2 |
|
protected function delegateObjectGetter($property, $getter, array $arguments) |
147
|
|
|
{ |
148
|
|
|
// If the property is invalid |
149
|
2 |
|
if (!is_callable([$this->object, $getter])) { |
150
|
1 |
|
throw new InvalidArgumentException( |
151
|
1 |
|
sprintf('Invalid apparat object property "%s"', $property), |
152
|
|
|
InvalidArgumentException::INVALID_APPARAT_OBJECT_PROPERTY |
153
|
1 |
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
return $this->object->$getter(...$arguments); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.