Completed
Push — v2 ( b8b784...b41681 )
by Joschi
04:45
created

ItemObjectModel::cacheLinkRelItems()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 3
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\LinkRel;
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
     * LinkRel item cache
52
     *
53
     * @var ItemListInterface
54
     */
55
    protected $rels = null;
56
57
    /**
58
     * Return all rel declarations of a particular type
59
     *
60
     * @param string|null $type Rel type
61
     * @param int|null $index Optional: particular index
62
     * @return ItemInterface|ItemInterface[] Single LinkRel item or list of LinkRel items
63
     * @api
64
     */
65 1
    public function rel($type = null, $index = null)
66
    {
67
        // One-time caching of rel elements
68 1
        if ($this->rels === null) {
69 1
            $this->cacheLinkRelItems();
70
        }
71
72
        // Find the matching LinkRel items
73 1
        $rels = ($type === null) ? $this->rels->getItems() : $this->rels->getItems($type);
74
75
        // Return rel item(s)
76 1
        return ($index === null) ? $rels : $this->getRelIndex($rels, $type, $index);
77
    }
78
79
    /**
80
     * Return a particular rel item by index
81
     *
82
     * @param ItemInterface[] $rels Rel items
83
     * @param string|null $type Rel type
84
     * @param int $index Rel item index
85
     * @return ItemInterface Rel item
86
     * @throws OutOfBoundsException If the rel index is out of bounds
87
     */
88 1
    protected function getRelIndex(array $rels, $type, $index)
89
    {
90
        // If the rel index is out of bounds
91 1
        if (!is_int($index) || !array_key_exists($index, $rels)) {
92 1
            throw new OutOfBoundsException(
93 1
                sprintf(OutOfBoundsException::INVALID_REL_INDEX_STR, $index, $type),
94 1
                OutOfBoundsException::INVALID_REL_INDEX
95
            );
96
        }
97
98 1
        return $rels[$index];
99
    }
100
101
    /**
102
     * One-time caching of LinkRel items
103
     */
104 1
    protected function cacheLinkRelItems()
105
    {
106 1
        $rels = [];
107 1
        foreach ($this->items as $item) {
108 1
            if ($item->getFormat() == LinkRel::FORMAT) {
109 1
                $rels[] = $item;
110
            }
111
        }
112 1
        $this->rels = new ItemList($rels);
113 1
    }
114
}
115