ItemObjectModel   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 97
c 0
b 0
f 0
wmc 12
lcom 1
cbo 2
ccs 24
cts 24
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A link() 0 13 4
A cacheLinkTypeItems() 0 10 3
A getLinkIndex() 0 12 3
A getDom() 0 4 1
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 © 2018 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 © 2018 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
     *
81
     * @return ItemInterface|ItemListInterface Single LinkType item or list of LinkType items
82
     * @api
83
     */
84 1
    public function link($type = null, $index = null)
85
    {
86
        // One-time caching of link elements
87 1
        if ($this->links === null) {
88 1
            $this->cacheLinkTypeItems();
89
        }
90
91
        // Find the matching LinkType items
92 1
        $links = ($type === null) ? $this->links->getItems() : $this->links->getItems($type);
93
94
        // Return link item(s)
95 1
        return ($index === null) ? new ItemList($links) : $this->getLinkIndex($links, $type, $index);
96
    }
97
98
    /**
99
     * One-time caching of LinkType items
100
     */
101 1
    protected function cacheLinkTypeItems()
102
    {
103 1
        $links = [];
104 1
        foreach ($this->items as $item) {
105 1
            if ($item->getFormat() == LinkType::FORMAT) {
106 1
                $links[] = $item;
107
            }
108
        }
109 1
        $this->links = new ItemList($links);
110 1
    }
111
112
    /**
113
     * Return a particular link item by index
114
     *
115
     * @param ItemInterface[] $links Link items
116
     * @param string|null $type      Link type
117
     * @param int $index             Link item index
118
     *
119
     * @return ItemInterface Link item
120
     * @throws OutOfBoundsException If the link index is out of bounds
121
     */
122 1
    protected function getLinkIndex(array $links, $type, $index)
123
    {
124
        // If the link index is out of bounds
125 1
        if (!is_int($index) || !array_key_exists($index, $links)) {
126 1
            throw new OutOfBoundsException(
127 1
                sprintf(OutOfBoundsException::INVALID_LINK_TYPE_INDEX_STR, $index, $type),
128 1
                OutOfBoundsException::INVALID_LINK_TYPE_INDEX
129
            );
130
        }
131
132 1
        return $links[$index];
133
    }
134
135
    /**
136
     * Return the original DOM document
137
     *
138
     * @return \DOMDocument DOM document
139
     */
140 1
    public function getDom()
141
    {
142 1
        return $this->dom;
143
    }
144
}
145