Completed
Push — v2 ( b3c0e1...e198fb )
by Joschi
08:19
created

ItemObjectModel::alternate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 6
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\Ports\Exceptions\OutOfBoundsException;
40
use Jkphl\Micrometa\Ports\Rel\AlternateInterface;
41
use Jkphl\Micrometa\Ports\Rel\RelInterface;
42
43
/**
44
 * Item object model
45
 *
46
 * @package Jkphl\Micrometa
47
 * @subpackage Jkphl\Micrometa\Ports
48
 */
49
class ItemObjectModel extends ItemList implements ItemObjectModelInterface
50
{
51
    /**
52
     * Rel declarations
53
     *
54
     * @var RelInterface[]
55
     */
56
    protected $rels;
57
58
    /**
59
     * Alternate resources
60
     *
61
     * @var AlternateInterface[]
62
     */
63
    protected $alternates;
64
65
    /**
66
     * ItemList constructor
67
     *
68
     * @param ItemInterface[] $items Items
69
     * @param RelInterface[] $rels Rel declarations
70
     * @param AlternateInterface[] $alternates Alternate resources
71
     */
72 4
    public function __construct(array $items = [], array $rels = [], array $alternates = [])
73
    {
74 4
        parent::__construct($items);
75 4
        $this->rels = $rels;
76 4
        $this->alternates = $alternates;
77 4
    }
78
79
    /**
80
     * Return all rel declarations of a particular type
81
     *
82
     * @param string $type Rel type
83
     * @param int|null $index Optional: particular index
84
     * @return RelInterface|RelInterface[] Single rel=* declaration or list of particular rel declarations
85
     * @throws OutOfBoundsException If the rel type is out of bounds
86
     * @throws OutOfBoundsException If the rel index is out of bounds
87
     * @api
88
     */
89 2
    public function rel($type, $index = null)
90
    {
91
        // If the rel type is out of bounds
92 2
        if (!array_key_exists($type, $this->rels)) {
93 1
            throw new OutOfBoundsException(
94 1
                sprintf(OutOfBoundsException::INVALID_REL_TYPE_STR, $type),
95 1
                OutOfBoundsException::INVALID_REL_TYPE
96
            );
97
        }
98
99
        // If all rel values should be returned
100 2
        if ($index === null) {
101 1
            return $this->rels[$type];
102
        }
103
104
        // If the rel index is out of bounds
105 2
        if (!is_int($index) || !array_key_exists($index, $this->rels[$type])) {
106 1
            throw new OutOfBoundsException(
107 1
                sprintf(OutOfBoundsException::INVALID_REL_INDEX_STR, $index, $type),
108 1
                OutOfBoundsException::INVALID_REL_INDEX
109
            );
110
        }
111
112 1
        return $this->rels[$type][$index];
113
    }
114
115
    /**
116
     * Return all rel=* declaration groups
117
     *
118
     * @return RelInterface[] Rel=* declaration groups
119
     * @api
120
     */
121 1
    public function rels()
122
    {
123 1
        return $this->rels;
124
    }
125
126
    /**
127
     * Return all alternate resources
128
     *
129
     * @return AlternateInterface[] Alternate resources
130
     * @api
131
     */
132 1
    public function alternates()
133
    {
134 1
        return $this->alternates;
135
    }
136
}
137