Completed
Push — v2 ( e198fb...470733 )
by Joschi
04:46
created

ItemObjectModel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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
     * @throws OutOfBoundsException If the rel index is out of bounds
64
     * @api
65
     */
66 1
    public function rel($type = null, $index = null)
67
    {
68
        // One-time caching of rel elements
69 1
        if ($this->rels === null) {
70 1
            $this->cacheLinkRelItems();
71
        }
72
73
        // Find the matching LinkRel items
74 1
        $rels = ($type === null) ? $this->rels->getItems() : $this->rels->getItems($type);
75
76
        // If all LinkRels should be returned
77 1
        if ($index === null) {
78 1
            return $rels;
79
        }
80
81
        // If the rel index is out of bounds
82 1
        if (!is_int($index) || !array_key_exists($index, $rels)) {
83 1
            throw new OutOfBoundsException(
84 1
                sprintf(OutOfBoundsException::INVALID_REL_INDEX_STR, $index, $type),
85 1
                OutOfBoundsException::INVALID_REL_INDEX
86
            );
87
        }
88
89 1
        return $rels[$index];
90
    }
91
92
    /**
93
     * One-time caching of LinkRel items
94
     */
95 1
    protected function cacheLinkRelItems()
96
    {
97 1
        $rels = [];
98 1
        foreach ($this->items as $item) {
99 1
            if ($item->getFormat() == LinkRel::FORMAT) {
100 1
                $rels[] = $item;
101
            }
102
        }
103 1
        $this->rels = new ItemList($rels);
104 1
    }
105
}
106