1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* micrometa |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Micrometa |
8
|
|
|
* @subpackage Jkphl\Micrometa\Ports |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2017 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 © 2017 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 Jkphl\Micrometa\Ports\Item; |
38
|
|
|
|
39
|
|
|
use Jkphl\Micrometa\Application\Item\ItemInterface as ApplicationItemInterface; |
40
|
|
|
use Jkphl\Micrometa\Domain\Exceptions\OutOfBoundsException as DomainOutOfBoundsException; |
41
|
|
|
use Jkphl\Micrometa\Infrastructure\Factory\ProfiledNamesFactory; |
42
|
|
|
use Jkphl\Micrometa\Infrastructure\Parser\ProfiledNamesList; |
43
|
|
|
use Jkphl\Micrometa\Ports\Exceptions\InvalidArgumentException; |
44
|
|
|
use Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Micro information item |
48
|
|
|
* |
49
|
|
|
* @package Jkphl\Micrometa |
50
|
|
|
* @subpackage Jkphl\Micrometa\Ports |
51
|
|
|
*/ |
52
|
|
|
class Item implements ItemInterface |
53
|
|
|
{ |
54
|
|
|
/** |
55
|
|
|
* Application item |
56
|
|
|
* |
57
|
|
|
* @var ApplicationItemInterface |
58
|
|
|
*/ |
59
|
|
|
protected $item; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Item constructor |
63
|
|
|
* |
64
|
|
|
* @param ApplicationItemInterface $item Application item |
65
|
|
|
*/ |
66
|
4 |
|
public function __construct(ApplicationItemInterface $item) |
67
|
|
|
{ |
68
|
4 |
|
$this->item = $item; |
69
|
4 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return whether the item is of a particular type (or contained in a list of types) |
73
|
|
|
* |
74
|
|
|
* The item type(s) can be specified in a variety of ways, @see ProfiledNamesFactory::createFromArguments(). |
75
|
|
|
* |
76
|
|
|
* @param string $name Name |
77
|
|
|
* @param string|null $profile Profile |
78
|
|
|
* @return boolean Item type is contained in the list of types |
79
|
|
|
*/ |
80
|
1 |
|
public function isOfType($name, $profile = null) |
81
|
|
|
{ |
82
|
|
|
/** @var ProfiledNamesList $types */ |
83
|
1 |
|
$types = ProfiledNamesFactory::createFromArguments(func_get_args()); |
84
|
|
|
|
85
|
|
|
// Run through all item types |
86
|
|
|
/** @var \stdClass $itemType */ |
87
|
1 |
|
foreach ($this->item->getType() as $itemType) { |
88
|
|
|
// Run through all query types |
89
|
|
|
/** @var \stdClass $queryType */ |
90
|
1 |
|
foreach ($types as $queryType) { |
91
|
1 |
|
if (($queryType->name == $itemType->name) && |
92
|
1 |
|
(($queryType->profile === null) ? true : ($queryType->profile == $itemType->profile)) |
93
|
|
|
) { |
94
|
1 |
|
return true; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get a single property (value) |
104
|
|
|
* |
105
|
|
|
* @param string $name Property name |
106
|
|
|
* @param string $profile Property profile |
107
|
|
|
* @param int $index Property value index |
108
|
|
|
* @return array|string|ItemInterface Property value(s) |
109
|
|
|
* @throws OutOfBoundsException If the property name is unknown |
110
|
|
|
* @throws OutOfBoundsException If the property value index is out of bounds |
111
|
|
|
*/ |
112
|
2 |
|
public function getProperty($name, $profile = null, $index = null) |
113
|
|
|
{ |
114
|
|
|
try { |
115
|
2 |
|
$propertyValues = $this->item->getProperty($name, $profile); |
116
|
2 |
|
} catch (DomainOutOfBoundsException $e) { |
117
|
2 |
|
throw new OutOfBoundsException($e->getMessage(), $e->getCode()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// If all property values should be returned |
121
|
2 |
|
if ($index === null) { |
122
|
2 |
|
return $propertyValues; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// If the property value index is out of bounds |
126
|
2 |
|
if (!isset($propertyValues[$index])) { |
127
|
|
|
throw new OutOfBoundsException( |
128
|
|
|
sprintf(OutOfBoundsException::INVALID_PROPERTY_VALUE_INDEX_STR, $index), |
129
|
|
|
OutOfBoundsException::INVALID_PROPERTY_VALUE_INDEX |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
2 |
|
return $propertyValues[$index]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the values or first value of an item property |
138
|
|
|
* |
139
|
|
|
* Prepend the property name with an "s" to retrieve the list of all available property values. |
140
|
|
|
* |
141
|
|
|
* @param string $name Item property name |
142
|
|
|
* @return string Values or first value of an item property |
143
|
|
|
*/ |
144
|
|
|
public function __get($name) |
145
|
|
|
{ |
146
|
|
|
// TODO: Implement __get() method. |
147
|
|
|
return ''; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get all values or the first value for a particular property (in a property list) |
152
|
|
|
* |
153
|
|
|
* The property name(s) can be specified in a variety of ways, @see ProfiledNamesFactory::createFromArguments(). |
154
|
|
|
* Append the property names with an "s" to retrieve the list of all available property values. |
155
|
|
|
* |
156
|
|
|
* @param array ...$names Property names |
157
|
|
|
* @return string|string[] Property value(s) |
158
|
|
|
* @throws InvalidArgumentException If no property name was given |
159
|
|
|
*/ |
160
|
|
|
public function firstOf(...$names) |
161
|
|
|
{ |
162
|
|
|
// If no property name was given |
163
|
|
|
if (!count($names)) { |
164
|
|
|
throw new InvalidArgumentException( |
165
|
|
|
sprintf(InvalidArgumentException::MISSING_PROPERTY_NAME_STR, __CLASS__.'::'.__METHOD__), |
166
|
|
|
InvalidArgumentException::MISSING_PROPERTY_NAME |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return ''; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|