ImageCandidateSet::key()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
/**
4
 * responsive-images-css
5
 *
6
 * @category   Jkphl
7
 * @package    Jkphl\Respimgcss
8
 * @subpackage Jkphl\Respimgcss\Domain
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\Respimgcss\Domain\Model;
38
39
use Jkphl\Respimgcss\Domain\Contract\ImageCandidateInterface;
40
use Jkphl\Respimgcss\Domain\Contract\ImageCandidateSetInterface;
41
use Jkphl\Respimgcss\Domain\Exceptions\InvalidArgumentException;
42
43
/**
44
 * Image candidate set
45
 *
46
 * @package    Jkphl\Respimgcss
47
 * @subpackage Jkphl\Respimgcss\Domain
48
 */
49
class ImageCandidateSet implements ImageCandidateSetInterface
50
{
51
    /**
52
     * Image candidates
53
     *
54
     * @var ImageCandidateInterface[]
55
     */
56
    protected $imageCandidates = [];
57
    /**
58
     * Internal pointer
59
     *
60
     * @var int
61
     */
62
    protected $pointer = 0;
63
64
    /**
65
     * Return the current image candidate
66
     *
67
     * @return ImageCandidateInterface Image candidate
68
     */
69 13
    public function current(): ImageCandidateInterface
70
    {
71 13
        return $this->imageCandidates[$this->pointer];
72
    }
73
74
    /**
75
     * Move forward to next image candidate
76
     *
77
     * @return void
78
     */
79 12
    public function next(): void
80
    {
81 12
        ++$this->pointer;
82 12
    }
83
84
    /**
85
     * Return the key of the current image candidate
86
     *
87
     * @return int Current key
88
     */
89 1
    public function key(): int
90
    {
91 1
        return $this->pointer;
92
    }
93
94
    /**
95
     * Checks if current position is valid
96
     *
97
     * @return boolean The return value will be casted to boolean and then evaluated.
98
     */
99 13
    public function valid(): bool
100
    {
101 13
        return isset($this->imageCandidates[$this->pointer]);
102
    }
103
104
    /**
105
     * Rewind the Iterator to the first element
106
     *
107
     * @return void
108
     */
109 13
    public function rewind(): void
110
    {
111 13
        $this->pointer = 0;
112 13
    }
113
114
    /**
115
     * Whether a offset exists
116
     *
117
     * @param int $offset Offset
118
     *
119
     * @return boolean Offset exists
120
     */
121 1
    public function offsetExists($offset): bool
122
    {
123 1
        return isset($this->imageCandidates[intval($offset)]);
124
    }
125
126
    /**
127
     * Offset to retrieve
128
     *
129
     * @param int $offset Offset
130
     *
131
     * @return ImageCandidateInterface Image candidate
132
     */
133 4
    public function offsetGet($offset): ImageCandidateInterface
134
    {
135 4
        return $this->imageCandidates[intval($offset)];
136
    }
137
138
    /**
139
     * Offset to set
140
     *
141
     * @param int|null $offset Offset
142
     * @param mixed $value
143
     *
144
     * @return void
145
     * @throws InvalidArgumentException If the value given is not a valid image candidate
146
     */
147 16
    public function offsetSet($offset, $value): void
148
    {
149 16
        if (!($value instanceof ImageCandidateInterface)) {
150 1
            throw new InvalidArgumentException(
151 1
                InvalidArgumentException::INVALID_IMAGE_CANDIDATE_STR,
152 1
                InvalidArgumentException::INVALID_IMAGE_CANDIDATE
153
            );
154
        }
155
156 15
        $this->imageCandidates[is_int($offset) ? $offset : count($this->imageCandidates)] = $value;
157 15
    }
158
159
    /**
160
     * Offset to unset
161
     *
162
     * @param int $offset Offset
163
     *
164
     * @return void
165
     */
166 1
    public function offsetUnset($offset): void
167
    {
168 1
        unset($this->imageCandidates[intval($offset)]);
169 1
        $this->imageCandidates = array_values($this->imageCandidates);
170 1
    }
171
172
    /**
173
     * Count elements of an object
174
     *
175
     * @return int Number of registered image candidates
176
     */
177 5
    public function count()
178
    {
179 5
        return count($this->imageCandidates);
180
    }
181
}
182