Completed
Push — master ( 6b3217...3aebab )
by Joschi
02:09
created

SourceSizeMediaCondition::matches()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 9
nop 3
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 6
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * responsive-images-css
5
 *
6
 * @category   Jkphl
7
 * @package    Jkphl\Respimgcss
8
 * @subpackage Jkphl\Respimgcss\Application\Model
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\Application\Model;
38
39
use Jkphl\Respimgcss\Domain\Contract\AbsoluteLengthInterface;
40
use Jkphl\Respimgcss\Domain\Contract\CssMinMaxMediaConditionInterface;
41
use Jkphl\Respimgcss\Domain\Model\Css\ResolutionMediaCondition;
42
use Jkphl\Respimgcss\Domain\Model\Css\WidthMediaCondition;
43
44
/**
45
 * Source size media condition
46
 *
47
 * @package    Jkphl\Respimgcss
48
 * @subpackage Jkphl\Respimgcss\Application\Model
49
 */
50
class SourceSizeMediaCondition
51
{
52
    /**
53
     * Media condition string
54
     *
55
     * @var string
56
     */
57
    protected $value;
58
    /**
59
     * Size and resolution conditions
60
     *
61
     * @var CssMinMaxMediaConditionInterface[]
62
     */
63
    protected $conditions;
64
    /**
65
     * Minimum width
66
     *
67
     * @var int|null
68
     */
69
    protected $minimumWidth = null;
70
    /**
71
     * Maximum width
72
     *
73
     * @var int|null
74
     */
75
    protected $maximumWidth = null;
76
    /**
77
     * Minimum resolution
78
     *
79
     * @var float|null
80
     */
81
    protected $minimumResolution = null;
82
    /**
83
     * Maximum resolution
84
     *
85
     * @var float|null
86
     */
87
    protected $maximumResolution = null;
88
89
    /**
90
     * Source size media condition constructor
91
     *
92
     * @param string $value                                  Media condition string
93
     * @param CssMinMaxMediaConditionInterface[] $conditions Size and resolution conditions
94
     */
95 17
    public function __construct(string $value, array $conditions = [])
96
    {
97 17
        $this->value      = $value;
98 17
        $this->conditions = $conditions;
99 17
        $this->initialize();
100 17
    }
101
102
    /**
103
     * Initialize the minimum / maximum values
104
     */
105 17
    protected function initialize(): void
106
    {
107
        /** @var CssMinMaxMediaConditionInterface $condition */
108 17
        foreach ($this->conditions as $condition) {
109
            // If this is a width condition
110 14
            if ($condition instanceof WidthMediaCondition) {
111 12
                $this->initializeCondition($condition, 'minimumWidth', 'maximumWidth');
112 12
                continue;
113
            }
114
            // If this is a resolution condition
115 6
            if ($condition instanceof ResolutionMediaCondition) {
116 6
                $this->initializeCondition($condition, 'minimumResolution', 'maximumResolution');
117
            }
118
        }
119 17
    }
120
121
    /**
122
     * Initialize a media condition
123
     *
124
     * @param CssMinMaxMediaConditionInterface $condition Media condition
125
     * @param string $minProperty                         Minimum property name
126
     * @param string $maxProperty                         Maximum property name
127
     */
128 13
    protected function initializeCondition(
129
        CssMinMaxMediaConditionInterface $condition,
130
        string $minProperty,
131
        string $maxProperty
132
    ): void {
133 13
        $modifier = $condition->getModifier();
134 13
        $value    = $condition->getValue()->getValue();
135
136
        // Minimum value
137 13
        if ($modifier == CssMinMaxMediaConditionInterface::MIN) {
138 10
            $this->initializeMinProperty($minProperty, $value);
139
140 10
            return;
141
        }
142
143
        // Maximum value
144 7
        if ($modifier == CssMinMaxMediaConditionInterface::MAX) {
145 6
            $this->initializeMaxProperty($maxProperty, $value);
146
147 6
            return;
148
        }
149
150 3
        $this->$minProperty = $this->$maxProperty = $value;
151 3
    }
152
153
    /**
154
     * Initialize a minimum property
155
     *
156
     * @param string $minProperty Property name
157
     * @param float $value        Property Value
158
     */
159 10
    protected function initializeMinProperty(string $minProperty, float $value): void
160
    {
161 10
        $this->$minProperty = ($this->$minProperty === null) ? $value : (min($value, $this->$minProperty));
162 10
    }
163
164
    /**
165
     * Initialize a maximum property
166
     *
167
     * @param string $maxProperty Property name
168
     * @param float $value        Property Value
169
     */
170 6
    protected function initializeMaxProperty(string $maxProperty, float $value): void
171
    {
172 6
        $this->$maxProperty = ($this->$maxProperty === null) ? $value : (max($value, $this->$maxProperty));
173 6
    }
174
175
    /**
176
     * Return the media condition string
177
     *
178
     * @return string Media condition string
179
     */
180 4
    public function getValue(): string
181
    {
182 4
        return $this->value;
183
    }
184
185
    /**
186
     * Return the size and resolution conditions
187
     *
188
     * @return CssMinMaxMediaConditionInterface[] Size and resolution conditions
189
     */
190 9
    public function getConditions(): array
191
    {
192 9
        return $this->conditions;
193
    }
194
195
    /**
196
     * Test if this source size condition matches a particular width and density
197
     *
198
     * @param AbsoluteLengthInterface $width Width
199
     * @param float $density                 Density
200
     * @param int|null $lastMinimumWidth     Minimum width of the next higher breakpoint
201
     *
202
     * @return bool This source size condition matches
203
     */
204 7
    public function matches(AbsoluteLengthInterface $width, float $density, int $lastMinimumWidth = null): bool
205
    {
206 7
        $match = true;
207
208
        // Run through all conditions
209
        /** @var CssMinMaxMediaConditionInterface $condition */
210 7
        foreach ($this->conditions as $condition) {
211 7
            $rangeLower = ($condition instanceof WidthMediaCondition) ? ($width->getValue() * $density) : $density;
212 7
            $rangeUpper = ($lastMinimumWidth === null) ? $rangeLower : ($lastMinimumWidth * $density - 1);
213 7
            if (!$condition->matches($rangeLower) || !$condition->matches($rangeUpper)) {
214 5
                $match = false;
215 7
                break;
216
            }
217
        }
218
219 7
        return $match;
220
    }
221
222
    /**
223
     * Return the minimum width
224
     *
225
     * @return int|null Minimum width
226
     */
227 6
    public function getMinimumWidth(): ?int
228
    {
229 6
        return $this->minimumWidth;
230
    }
231
232
    /**
233
     * Return the maximum width
234
     *
235
     * @return int|null Maximum width
236
     */
237 8
    public function getMaximumWidth(): ?int
238
    {
239 8
        return $this->maximumWidth;
240
    }
241
242
    /**
243
     * Return the minimum resolution
244
     *
245
     * @return float Minimum resolution
246
     */
247 3
    public function getMinimumResolution(): ?float
248
    {
249 3
        return $this->minimumResolution;
250
    }
251
252
    /**
253
     * Return the maximum resolution
254
     *
255
     * @return float Maximum resolution
256
     */
257 3
    public function getMaximumResolution(): ?float
258
    {
259 3
        return $this->maximumResolution;
260
    }
261
}
262