1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* micrometa |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Micrometa |
8
|
|
|
* @subpackage Jkphl\Micrometa\Ports\Item |
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\Infrastructure\Parser\LinkType; |
40
|
|
|
use Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Item object model |
44
|
|
|
* |
45
|
|
|
* @package Jkphl\Micrometa |
46
|
|
|
* @subpackage Jkphl\Micrometa\Ports |
47
|
|
|
*/ |
48
|
|
|
class ItemObjectModel extends ItemList implements ItemObjectModelInterface |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* DOM document |
52
|
|
|
* |
53
|
|
|
* @var \DOMDocument |
54
|
|
|
*/ |
55
|
|
|
protected $dom; |
56
|
|
|
/** |
57
|
|
|
* LinkType item cache |
58
|
|
|
* |
59
|
|
|
* @var ItemListInterface |
60
|
|
|
*/ |
61
|
|
|
protected $links = null; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Constructor |
65
|
|
|
* |
66
|
|
|
* @param \DOMDocument $dom DOM document |
67
|
|
|
* @param array $items Items |
68
|
|
|
*/ |
69
|
2 |
|
public function __construct(\DOMDocument $dom, $items = []) |
70
|
|
|
{ |
71
|
2 |
|
$this->dom = $dom; |
72
|
2 |
|
parent::__construct($items); |
73
|
2 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Return all link declarations of a particular type |
77
|
|
|
* |
78
|
|
|
* @param string|null $type Link type |
79
|
|
|
* @param int|null $index Optional: particular index |
80
|
|
|
* @return ItemInterface|ItemListInterface Single LinkType item or list of LinkType items |
81
|
|
|
* @api |
82
|
|
|
*/ |
83
|
1 |
|
public function link($type = null, $index = null) |
84
|
|
|
{ |
85
|
|
|
// One-time caching of link elements |
86
|
1 |
|
if ($this->links === null) { |
87
|
1 |
|
$this->cacheLinkTypeItems(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Find the matching LinkType items |
91
|
1 |
|
$links = ($type === null) ? $this->links->getItems() : $this->links->getItems($type); |
92
|
|
|
|
93
|
|
|
// Return link item(s) |
94
|
1 |
|
return ($index === null) ? new ItemList($links) : $this->getLinkIndex($links, $type, $index); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* One-time caching of LinkType items |
99
|
|
|
*/ |
100
|
1 |
|
protected function cacheLinkTypeItems() |
101
|
|
|
{ |
102
|
1 |
|
$links = []; |
103
|
1 |
|
foreach ($this->items as $item) { |
104
|
1 |
|
if ($item->getFormat() == LinkType::FORMAT) { |
105
|
1 |
|
$links[] = $item; |
106
|
|
|
} |
107
|
|
|
} |
108
|
1 |
|
$this->links = new ItemList($links); |
109
|
1 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Return a particular link item by index |
113
|
|
|
* |
114
|
|
|
* @param ItemInterface[] $links Link items |
115
|
|
|
* @param string|null $type Link type |
116
|
|
|
* @param int $index Link item index |
117
|
|
|
* @return ItemInterface Link item |
118
|
|
|
* @throws OutOfBoundsException If the link index is out of bounds |
119
|
|
|
*/ |
120
|
1 |
|
protected function getLinkIndex(array $links, $type, $index) |
121
|
|
|
{ |
122
|
|
|
// If the link index is out of bounds |
123
|
1 |
|
if (!is_int($index) || !array_key_exists($index, $links)) { |
124
|
1 |
|
throw new OutOfBoundsException( |
125
|
1 |
|
sprintf(OutOfBoundsException::INVALID_LINK_TYPE_INDEX_STR, $index, $type), |
126
|
1 |
|
OutOfBoundsException::INVALID_LINK_TYPE_INDEX |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
return $links[$index]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Return the original DOM document |
135
|
|
|
* |
136
|
|
|
* @return \DOMDocument DOM document |
137
|
|
|
*/ |
138
|
1 |
|
public function getDom() |
139
|
|
|
{ |
140
|
1 |
|
return $this->dom; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|