SourceSizeFactory::shiftMediaConditionValue()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
ccs 8
cts 8
cp 1
crap 3
1
<?php
2
3
/**
4
 * responsive-images-css
5
 *
6
 * @category   Jkphl
7
 * @package    Jkphl\Respimgcss
8
 * @subpackage Jkphl\Respimgcss\Application
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\Factory;
38
39
use Jkphl\Respimgcss\Application\Contract\UnitLengthInterface;
40
use Jkphl\Respimgcss\Application\Exceptions\InvalidArgumentException;
41
use Jkphl\Respimgcss\Application\Model\AbsoluteLength;
42
use Jkphl\Respimgcss\Application\Model\SourceSize;
43
use Jkphl\Respimgcss\Application\Model\SourceSizeMediaCondition;
44
use Jkphl\Respimgcss\Domain\Contract\LengthInterface;
45
use Jkphl\Respimgcss\Domain\Model\Css\ResolutionMediaCondition;
46
use Jkphl\Respimgcss\Domain\Model\Css\WidthMediaCondition;
47
48
/**
49
 * Source size factory
50
 *
51
 * @package    Jkphl\Respimgcss
52
 * @subpackage Jkphl\Respimgcss\Application
53
 */
54
class SourceSizeFactory extends AbstractLengthFactory
55
{
56
    /**
57
     * Create a source size value from a source size string
58
     *
59
     * @param string $sourceSizeStr Source size string
60
     *
61
     * @return SourceSize Source size
62
     */
63 16
    public function createFromSourceSizeStr(string $sourceSizeStr): SourceSize
64
    {
65
        // Determine the source size value
66 16
        $sourceSizeValue = $this->parseSourceSizeValue($sourceSizeStr);
67
68
        // Determine the associated media condition (if any)
69 13
        $mediaCondition = $this->parseMediaCondition($sourceSizeStr);
70
71
        // Return a source size instance
72 13
        return new SourceSize($sourceSizeValue, $mediaCondition);
73
    }
74
75
    /**
76
     * Parse the length component
77
     *
78
     * @param string $sourceSizeStr Source size string
79
     *
80
     * @return UnitLengthInterface AbstractLength component
81
     * @throws InvalidArgumentException If the source size string is ill-formatted
82
     */
83 16
    protected function parseSourceSizeValue(string &$sourceSizeStr): UnitLengthInterface
84
    {
85
        // If the source size string ends with a parenthesis: Try to parse a calc() base length
86 16
        if (substr($sourceSizeStr, -1) === ')') {
87 3
            return $this->parseSourceSizeCalculatedValue($sourceSizeStr);
88
        }
89
90
        // If the source size string is ill-formatted
91 13
        if (!preg_match('/^(.*\s+)?([^\s]+)$/', $sourceSizeStr, $sourceSizeStrMatch)) {
92 1
            throw new InvalidArgumentException(
93 1
                sprintf(InvalidArgumentException::ILL_FORMATTED_SOURCE_SIZE_STRING_STR, $sourceSizeStr),
94 1
                InvalidArgumentException::ILL_FORMATTED_SOURCE_SIZE_STRING
95
            );
96
        }
97
98
        // Post-process the remaining string
99 12
        $sourceSizeStr = trim($sourceSizeStrMatch[1]);
100
101
        // Return the parsed length
102 12
        return (new LengthFactory($this->calculatorServiceFactory, $this->emPixel))
103 12
            ->createLengthFromString($sourceSizeStrMatch[2]);
104
    }
105
106
    /**
107
     * Parse a calc() based length value
108
     *
109
     * @param string $sourceSizeStr Source size string
110
     *
111
     * @return UnitLengthInterface AbstractLength component
112
     * @throws InvalidArgumentException If the source size string is ill-formatted
113
     */
114 3
    protected function parseSourceSizeCalculatedValue(string &$sourceSizeStr): UnitLengthInterface
115
    {
116
        // Reverse-consume the source size string
117 3
        $sourceSizeRev = strrev($sourceSizeStr);
118 3
        $balance       = null;
119 3
        for ($pos = 0; $pos < strlen($sourceSizeStr); ++$pos) {
120 3
            $balance += $this->getCharacterBalance($sourceSizeRev[$pos]);
121 3
            if ($balance === 0) {
122 1
                $length        = (new CalcLengthFactory($this->calculatorServiceFactory, $this->emPixel))
123 1
                    ->createLengthFromString(substr($sourceSizeStr, -($pos + 5)));
124 1
                $sourceSizeStr = trim(substr($sourceSizeStr, 0, -($pos + 5)));
125
126 1
                return $length;
127
            }
128
        }
129
130
        // Else: The source size string is ill-formatted
131 2
        throw new InvalidArgumentException(
132 2
            sprintf(InvalidArgumentException::ILL_FORMATTED_SOURCE_SIZE_STRING_STR, $sourceSizeStr),
133 2
            InvalidArgumentException::ILL_FORMATTED_SOURCE_SIZE_STRING
134
        );
135
    }
136
137
    /**
138
     * Return the balance value for a particular value
139
     *
140
     * @param string $char Character
141
     *
142
     * @return int Balance value
143
     */
144 3
    protected function getCharacterBalance($char): string
145
    {
146 3
        return ($char === '(') ? 1 : (($char === ')') ? -1 : 0);
147
    }
148
149
    /**
150
     * Parse and instantiate a source size media condition
151
     *
152
     * @param string $mediaConditionString
153
     *
154
     * @return SourceSizeMediaCondition|null Source size media condition
155
     * @see https://drafts.csswg.org/mediaqueries-4/#typedef-media-condition
156
     * @see https://developer.mozilla.org/de/docs/Web/CSS/Media_Queries/Using_media_queries#Pseudo-BNF_(for_those_of_you_that_like_that_kind_of_thing)
157
     */
158 13
    protected function parseMediaCondition(string $mediaConditionString): ?SourceSizeMediaCondition
159
    {
160 13
        return new SourceSizeMediaCondition(
161 13
            $mediaConditionString,
162 13
            array_merge(
163 13
                $this->parseWidthMediaConditions($mediaConditionString),
164 13
                $this->parseResolutionMediaConditions($mediaConditionString)
165
            )
166
        );
167
    }
168
169
    /**
170
     * Extract width media conditions
171
     *
172
     * @param string $mediaConditionString Media condition string
173
     *
174
     * @return WidthMediaCondition[] Width media conditions
175
     */
176 13
    protected function parseWidthMediaConditions(string $mediaConditionString): array
177
    {
178 13
        $widthMediaConditions  = [];
179 13
        $widthConditionMatches = $this->matchMediaConditions(
180 13
            $mediaConditionString,
181
            // width | min-width | max-width
182
            // device-width | min-device-width | max-device-width
183 13
            '/((?:min|max)\-)?(?:device\-)?width\s*\:\s*/'
184
        );
185
186 13
        foreach ($widthConditionMatches as $match) {
187
            try {
188 12
                $widthMediaConditions[] = new WidthMediaCondition(
189 12
                    $this->parseWidthMediaConditionValue($match[0]),
190 10
                    $match[1]
191
                );
192 2
            } catch (\Exception $e) {
193 12
                continue;
194
            }
195
        }
196
197 13
        return $widthMediaConditions;
198
    }
199
200
    /**
201
     * Match a media condition string
202
     *
203
     * @param string $mediaConditionString Media condition string
204
     * @param string $pattern              PCRE pattern
205
     *
206
     * @return array Matches
207
     */
208 13
    protected function matchMediaConditions(string $mediaConditionString, string $pattern): array
209
    {
210 13
        preg_match_all($pattern, $mediaConditionString, $matches, PREG_OFFSET_CAPTURE);
211 13
        $refinedMatches = [];
212 13
        foreach ($matches[0] as $index => $match) {
213 13
            $matchLength      = strlen($match[0]) + $match[1];
214 13
            $matchModifier    = is_array($matches[1][$index]) ? $matches[1][$index][0] : '';
215 13
            $refinedMatches[] = [substr($mediaConditionString, $matchLength), $matchModifier];
216
        }
217
218 13
        return $refinedMatches;
219
    }
220
221
    /**
222
     * Parse a width media condition value
223
     *
224
     * @param string $widthMediaConditionStr Width media condition string
225
     *
226
     * @return AbsoluteLength Width media condition value
227
     */
228 12
    protected function parseWidthMediaConditionValue(string $widthMediaConditionStr): AbsoluteLength
229
    {
230 12
        $widthMediaConditionValueStr = $this->shiftMediaConditionValue($widthMediaConditionStr);
231
232
        // Try to parse as simple unit length
233
        try {
234 12
            return (new LengthFactory($this->calculatorServiceFactory, $this->emPixel))
235 12
                ->createAbsoluteLengthFromString($widthMediaConditionValueStr);
236 3
        } catch (InvalidArgumentException $e) {
237
            // Skip
238
        }
239
240
        // Try to parse as calc() length
241 3
        return (new CalcLengthFactory($this->calculatorServiceFactory, $this->emPixel))
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Jkphl\Respimg...MediaConditionValueStr) could return the type Jkphl\Respimgcss\Application\Model\ViewportLength which is incompatible with the type-hinted return Jkphl\Respimgcss\Application\Model\AbsoluteLength. Consider adding an additional type-check to rule them out.
Loading history...
242 3
            ->createLengthFromString($widthMediaConditionValueStr);
243
    }
244
245
    /**
246
     * Shift a media condition value off the beginning of a media condition string
247
     *
248
     * @param string $mediaConditionValueStr Media condition string
249
     *
250
     * @return string Media condition value string
251
     */
252 13
    protected function shiftMediaConditionValue(string $mediaConditionValueStr): string
253
    {
254 13
        $stringLength = strlen($mediaConditionValueStr);
255 13
        $balance      = 1;
256 13
        for ($char = 0; $char < $stringLength; ++$char) {
257 13
            $balance += $this->getCharacterBalanceModifier($mediaConditionValueStr[$char]);
258 13
            if ($balance == 0) {
259 12
                return substr($mediaConditionValueStr, 0, $char);
260
            }
261
        }
262
263 1
        return $mediaConditionValueStr;
264
    }
265
266
    /**
267
     * Return the balance modifier for a particular character
268
     *
269
     * @param string $char Character
270
     *
271
     * @return int Balance modifier
272
     */
273 13
    protected function getCharacterBalanceModifier(string $char): int
274
    {
275 13
        return ($char == ')') ? -1 : (($char == '(') ? 1 : 0);
276
    }
277
278
    /**
279
     * Extract resolution media conditions
280
     *
281
     * @param string $mediaConditionString Media condition string
282
     *
283
     * @return ResolutionMediaCondition[] Resolution media conditions
284
     */
285 13
    protected function parseResolutionMediaConditions(string $mediaConditionString): array
286
    {
287 13
        $resolutionMediaConditions  = [];
288 13
        $resolutionConditionMatches = $this->matchMediaConditions(
289 13
            $mediaConditionString,
290
            // resolution | min-resolution | max-resolution
291 13
            '/((?:min|max)\-)?resolution\s*\:\s*/'
292
        );
293
294 13
        foreach ($resolutionConditionMatches as $match) {
295
            try {
296 4
                $resolutionMediaConditions[] = new ResolutionMediaCondition(
297 4
                    $this->parseResolutionMediaConditionValue($match[0]),
298 3
                    $match[1]
299
                );
300 1
            } catch (\Exception $e) {
301 4
                continue;
302
            }
303
        }
304
305 13
        return $resolutionMediaConditions;
306
    }
307
308
    /**
309
     * Parse a resolution media condition value
310
     *
311
     * @param string $resolutionMediaConditionStr Resolution media condition string
312
     *
313
     * @return LengthInterface Resolution media condition value
314
     * @throws InvalidArgumentException If the value is not numeric
315
     */
316 4
    protected function parseResolutionMediaConditionValue(string $resolutionMediaConditionStr): AbsoluteLength
317
    {
318 4
        $resolutionMediaConditionValueStr = $this->shiftMediaConditionValue($resolutionMediaConditionStr);
319
320
        // If the value is not numeric
321 4
        if (!is_numeric($resolutionMediaConditionValueStr)) {
322 1
            throw new InvalidArgumentException(
323 1
                sprintf(InvalidArgumentException::NON_WELL_FORMED_NUMERIC_VALUE_STR, $resolutionMediaConditionValueStr),
324 1
                InvalidArgumentException::NON_WELL_FORMED_NUMERIC_VALUE
325
            );
326
        }
327
328 3
        return $this->createAbsoluteLength(floatval($resolutionMediaConditionValueStr));
329
    }
330
}
331