1 | <?php |
||
55 | class Item extends ItemList implements ItemInterface |
||
56 | { |
||
57 | /** |
||
58 | * Application item |
||
59 | * |
||
60 | * @var ApplicationItemInterface |
||
61 | */ |
||
62 | protected $item; |
||
63 | |||
64 | /** |
||
65 | * Item constructor |
||
66 | * |
||
67 | * @param ApplicationItemInterface $item Application item |
||
68 | */ |
||
69 | 13 | public function __construct(ApplicationItemInterface $item) |
|
74 | |||
75 | /** |
||
76 | * Get the first value of an item property |
||
77 | * |
||
78 | * @param string $name Item property name |
||
79 | * @return string First value of an item property |
||
80 | * @api |
||
81 | */ |
||
82 | 2 | public function __get($name) |
|
86 | |||
87 | /** |
||
88 | * Get a single property (value) |
||
89 | * |
||
90 | * @param string $name Property name |
||
91 | * @param string $profile Property profile |
||
92 | * @param int $index Property value index |
||
93 | * @return array|string|ItemInterface Property value(s) |
||
94 | * @throws OutOfBoundsException If the property name is unknown |
||
95 | * @throws OutOfBoundsException If the property value index is out of bounds |
||
96 | * @api |
||
97 | */ |
||
98 | 7 | public function getProperty($name, $profile = null, $index = null) |
|
99 | { |
||
100 | try { |
||
101 | 7 | $propertyValues = $this->item->getProperty($name, $profile); |
|
102 | 4 | } catch (DomainOutOfBoundsException $e) { |
|
103 | 4 | throw new OutOfBoundsException($e->getMessage(), $e->getCode()); |
|
104 | } |
||
105 | |||
106 | // If all property values should be returned |
||
107 | 7 | if ($index === null) { |
|
108 | 5 | return array_map([$this, 'exportPropertyValue'], $propertyValues); |
|
109 | } |
||
110 | |||
111 | // If the property value index is out of bounds |
||
112 | 6 | if (!isset($propertyValues[$index])) { |
|
113 | 1 | throw new OutOfBoundsException( |
|
114 | 1 | sprintf(OutOfBoundsException::INVALID_PROPERTY_VALUE_INDEX_STR, $index), |
|
115 | 1 | OutOfBoundsException::INVALID_PROPERTY_VALUE_INDEX |
|
116 | ); |
||
117 | } |
||
118 | |||
119 | 5 | return $this->exportPropertyValue($propertyValues[$index]); |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Prepare a property value for returning it |
||
124 | * |
||
125 | * @param ValueInterface $value Property value |
||
126 | * @return Item|mixed Returnable property value |
||
127 | */ |
||
128 | 6 | protected function exportPropertyValue(ValueInterface $value) |
|
133 | |||
134 | /** |
||
135 | * Return whether the item is of a particular type (or contained in a list of types) |
||
136 | * |
||
137 | * The item type(s) can be specified in a variety of ways, @see ProfiledNamesFactory::createFromArguments() |
||
138 | * |
||
139 | * @param string $name Name |
||
140 | * @param string|null $profile Profile |
||
141 | * @return boolean Item type is contained in the list of types |
||
142 | * @api |
||
143 | */ |
||
144 | 5 | public function isOfType($name, $profile = null) |
|
145 | { |
||
146 | /** @var ProfiledNamesList $types */ |
||
147 | 5 | $types = ProfiledNamesFactory::createFromArguments(func_get_args()); |
|
148 | 5 | $aliasFactory = new AliasFactory(); |
|
149 | |||
150 | // Run through all item types |
||
151 | /** @var \stdClass $itemType */ |
||
152 | 5 | foreach ($this->item->getType() as $itemType) { |
|
153 | 5 | $itemTypeNames = $aliasFactory->createAliases($itemType->name); |
|
154 | 5 | if ($this->isOfProfiledTypes($itemType->profile, $itemTypeNames, $types)) { |
|
155 | 5 | return true; |
|
156 | } |
||
157 | } |
||
158 | |||
159 | 3 | return false; |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * Return whether an aliased item type in contained in a set of query types |
||
164 | * |
||
165 | * @param string $profile Type profile |
||
166 | * @param array $names Aliased type names |
||
167 | * @param ProfiledNamesList $types Query types |
||
168 | * @return bool Item type is contained in the set of query types |
||
169 | */ |
||
170 | 5 | protected function isOfProfiledTypes($profile, array $names, ProfiledNamesList $types) |
|
171 | { |
||
172 | // Run through all query types |
||
173 | /** @var \stdClass $queryType */ |
||
174 | 5 | foreach ($types as $queryType) { |
|
175 | 5 | if (in_array($queryType->name, $names) && |
|
176 | 5 | (($queryType->profile === null) ? true : ($queryType->profile == $profile)) |
|
177 | ) { |
||
178 | 5 | return true; |
|
179 | } |
||
180 | } |
||
181 | 3 | return false; |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * Get all values of the first available property in a stack |
||
186 | * |
||
187 | * The property stack can be specified in a variety of ways, @see ProfiledNamesFactory::createFromArguments() |
||
188 | * |
||
189 | * @param string $name Name |
||
190 | * @param string $profile Profile |
||
191 | * @return array Property values |
||
192 | * @throws InvalidArgumentException If no property name was given |
||
193 | * @throws OutOfBoundsException If none of the requested properties is known |
||
194 | * @api |
||
195 | */ |
||
196 | 2 | public function getFirstProperty($name, $profile = null) |
|
218 | |||
219 | /** |
||
220 | * Return all properties |
||
221 | * |
||
222 | * @return array[] Properties |
||
223 | * @api |
||
224 | */ |
||
225 | 1 | public function getProperties() |
|
229 | |||
230 | /** |
||
231 | * Return an object representation of the item |
||
232 | * |
||
233 | * @return \stdClass Micro information item |
||
234 | * @api |
||
235 | */ |
||
236 | 2 | public function toObject() |
|
240 | |||
241 | /** |
||
242 | * Get the item type |
||
243 | * |
||
244 | * @return \stdClass[] Item type |
||
245 | */ |
||
246 | 1 | public function getType() |
|
250 | |||
251 | /** |
||
252 | * Get the item format |
||
253 | * |
||
254 | * @return int Item format |
||
255 | */ |
||
256 | 1 | public function getFormat() |
|
260 | } |
||
261 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.