Passed
Push — master ( 297d0e...a57d9e )
by Joschi
02:13
created

CalcLengthFactory::handleUnitLengthToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 2
dl 0
loc 21
ccs 0
cts 13
cp 0
crap 6
rs 9.3142
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\Factory
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 ChrisKonnertz\StringCalc\Parser\Nodes\ContainerNode;
40
use ChrisKonnertz\StringCalc\StringCalc;
41
use ChrisKonnertz\StringCalc\Tokenizer\Token;
42
use Jkphl\Respimgcss\Application\Contract\UnitLengthInterface;
43
use Jkphl\Respimgcss\Application\Exceptions\InvalidArgumentException;
44
use Jkphl\Respimgcss\Application\Model\AbsoluteLength;
45
use Jkphl\Respimgcss\Application\Model\ViewportFunction;
46
use Jkphl\Respimgcss\Application\Model\ViewportLength;
47
use Jkphl\Respimgcss\Application\Service\LengthNormalizerService;
48
49
/**
50
 * Length factory for calc() based values
51
 *
52
 * @package    Jkphl\Respimgcss
53
 * @subpackage Jkphl\Respimgcss\Application\Factory
54
 */
55
class CalcLengthFactory
56
{
57
    /**
58
     * Create a unit length from a calc() size string
59
     *
60
     * @param string $calcString calc() size string
61
     * @param int $emPixel       EM to pixel ratio
62
     *
63
     * @return UnitLengthInterface
64
     * @throws \ChrisKonnertz\StringCalc\Exceptions\ContainerException
65
     * @throws \ChrisKonnertz\StringCalc\Exceptions\InvalidIdentifierException
66
     * @throws \ChrisKonnertz\StringCalc\Exceptions\NotFoundException
67
     */
68
    public static function createFromString(string $calcString, int $emPixel = 16): UnitLengthInterface
69
    {
70
        // If the calc() string is ill-formatted
71
        if (!preg_match('/^calc\(.+\)$/', $calcString)) {
72
            throw new InvalidArgumentException(
73
                sprintf(InvalidArgumentException::ILL_FORMATTED_CALC_LENGTH_STRING_STR, $calcString),
74
                InvalidArgumentException::ILL_FORMATTED_CALC_LENGTH_STRING
75
            );
76
        }
77
78
        return self::createCalculationContainerFromString($calcString, $emPixel);
79
    }
80
81
    /**
82
     * Parse a calculation string and return a precompiled calculation node container
83
     *
84
     * @param string $calcString Calculation string
85
     * @param int $emPixel       EM to pixel ratio
86
     *
87
     * @return ContainerNode Calculation node container
88
     * @throws \ChrisKonnertz\StringCalc\Exceptions\ContainerException
89
     * @throws \ChrisKonnertz\StringCalc\Exceptions\InvalidIdentifierException
90
     * @throws \ChrisKonnertz\StringCalc\Exceptions\NotFoundException
91
     */
92
    protected static function createCalculationContainerFromString(
93
        string $calcString,
94
        int $emPixel = 16
95
    ): UnitLengthInterface {
96
        $stringCalc    = new StringCalc();
97
        $calcTokens    = $stringCalc->tokenize($calcString);
98
        $refinedTokens = self::refineCalculationTokens($calcTokens, $emPixel);
99
100
        // If there's the viewport involved in the calculation: Create a relative calculated length
101
        /** @var Token $token */
102
        foreach ($refinedTokens as $token) {
103
            if (($token->getType() == Token::TYPE_WORD) && ($token->getValue() === 'viewport')) {
104
                return self::createViewportUnitFromTokens($refinedTokens, $stringCalc, $emPixel);
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::createViewp... $stringCalc, $emPixel) returns the type Jkphl\Respimgcss\Application\Model\ViewportLength which is incompatible with the documented return type ChrisKonnertz\StringCalc...ser\Nodes\ContainerNode.
Loading history...
105
            }
106
        }
107
108
        // Create and return an absolute length
109
        return new AbsoluteLength(
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Jkphl\Respimg...lizerService($emPixel)) returns the type Jkphl\Respimgcss\Application\Model\AbsoluteLength which is incompatible with the documented return type ChrisKonnertz\StringCalc...ser\Nodes\ContainerNode.
Loading history...
110
            $stringCalc->calculate($refinedTokens),
0 ignored issues
show
Bug introduced by
$refinedTokens of type array is incompatible with the type string expected by parameter $term of ChrisKonnertz\StringCalc\StringCalc::calculate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
            $stringCalc->calculate(/** @scrutinizer ignore-type */ $refinedTokens),
Loading history...
111
            UnitLengthInterface::UNIT_PIXEL,
112
            new LengthNormalizerService($emPixel)
113
        );
114
    }
115
116
    /**
117
     * Create a unit length from calculation tokens
118
     *
119
     * @param Token[] $tokens        Calculation tokens
120
     * @param StringCalc $stringCalc StringCalc instance
121
     * @param int $emPixel           EM to pixel ratio
122
     *
123
     * @return UnitLengthInterface Unit length
124
     * @throws \ChrisKonnertz\StringCalc\Exceptions\ContainerException
125
     * @throws \ChrisKonnertz\StringCalc\Exceptions\InvalidIdentifierException
126
     * @throws \ChrisKonnertz\StringCalc\Exceptions\NotFoundException
127
     */
128
    protected static function createViewportUnitFromTokens(
129
        array $tokens,
130
        StringCalc $stringCalc,
131
        int $emPixel = 16
132
    ): UnitLengthInterface {
133
        $stringHelper = $stringCalc->getContainer()->get('stringcalc_stringhelper');
134
        $stringCalc->getSymbolContainer()->add(new ViewportFunction($stringHelper));
135
136
        return new ViewportLength($stringCalc->parse($tokens), new LengthNormalizerService($emPixel));
137
    }
138
139
    /**
140
     * Refine a list of symbol tokens
141
     *
142
     * @param Token[] $tokens Symbol tokens
143
     * @param int $emPixel    EM to pixel ratio
144
     *
145
     * @return Token[] Refined symbol tokens
146
     */
147
    protected static function refineCalculationTokens(array $tokens, int $emPixel = 16): array
148
    {
149
        $refinedTokens = [];
150
        $previousToken = null;
151
152
        // Run through all tokens
153
        foreach ($tokens as $token) {
154
            $previousToken = self::handleToken($refinedTokens, $token, $previousToken, $emPixel);
155
        }
156
157
        // Add the last token
158
        if ($previousToken) {
159
            array_push($refinedTokens, $previousToken);
160
        }
161
162
        return $refinedTokens;
163
    }
164
165
    /**
166
     * Handle a particular token
167
     *
168
     * @param Token[] $refinedTokens    Refined tokens
169
     * @param Token $token              Token
170
     * @param Token|null $previousToken Previous token
171
     * @param int $emPixel              EM to pixel ratio
172
     *
173
     * @return Token|null               Stash token
174
     */
175
    protected static function handleToken(
176
        array &$refinedTokens,
177
        Token $token,
178
        Token $previousToken = null,
179
        int $emPixel = 16
180
    ): ?Token {
181
        // If it's a word token: Handle individually
182
        if ($token->getType() == Token::TYPE_WORD) {
183
            return self::handleWordToken($refinedTokens, $token, $previousToken, $emPixel);
184
        }
185
186
        // In all other cases: Register the previou token (if any)
187
        if ($previousToken) {
188
            array_push($refinedTokens, $previousToken);
189
        }
190
191
        // If it's a number token: Stash
192
        if ($token->getType() == Token::TYPE_NUMBER) {
193
            return $token;
194
        }
195
196
        array_push($refinedTokens, $token);
197
198
        return null;
199
    }
200
201
    /**
202
     * Handle a particular token
203
     *
204
     * The method returns a list of zero or more (possibly refined) tokens to preerve
205
     *
206
     * @param Token[] $refinedTokens    Refined tokens
207
     * @param Token $token              Token
208
     * @param Token|null $previousToken Previous token
209
     * @param int $emPixel              EM to pixel ratio
210
     *
211
     * @return Token|null               Stash token
212
     * @throws InvalidArgumentException If the word token is invalid
213
     */
214
    protected static function handleWordToken(
215
        array &$refinedTokens,
216
        Token $token,
217
        Token $previousToken = null,
218
        int $emPixel = 16
219
    ): ?Token {
220
        // If it's a calc() function call: Add the previous token and skip the current one
221
        if ($token->getValue() == 'calc') {
222
            if ($previousToken) {
223
                array_push($refinedTokens, $previousToken);
224
            }
225
226
            return null;
227
        }
228
229
        // If the previous token is a number: Try to generate a unit length
230
        if ($previousToken && ($previousToken->getType() == Token::TYPE_NUMBER)) {
231
            try {
232
                $unitLength = LengthFactory::createLengthFromString(
233
                    $previousToken->getValue().$token->getValue(),
234
                    $emPixel
235
                );
236
                self::handleUnitLengthToken($refinedTokens, $unitLength);
237
238
                return null;
239
            } catch (InvalidArgumentException $e) {
240
                // Ignore
241
            }
242
        }
243
244
        // Invalid word token
245
        throw new InvalidArgumentException(
246
            sprintf(InvalidArgumentException::INVALID_WORD_TOKEN_IN_SOURCE_SIZE_VALUE_STR, $token->getValue()),
247
            InvalidArgumentException::INVALID_WORD_TOKEN_IN_SOURCE_SIZE_VALUE
248
        );
249
    }
250
251
    /**
252
     * Handle a unit length token
253
     *
254
     * @param Token[] $refinedTokens          Refined tokens
255
     * @param UnitLengthInterface $unitLength Unit length
256
     */
257
    protected static function handleUnitLengthToken(
258
        array &$refinedTokens,
259
        UnitLengthInterface $unitLength
260
    ): void {
261
        // If it's an absolute value
262
        if ($unitLength->isAbsolute()) {
263
            array_push($refinedTokens, new Token(strval($unitLength->getValue()), Token::TYPE_NUMBER, 0));
264
265
            return;
266
        }
267
268
        // Else: Substitute with multiplied function expression
269
        array_push(
270
            $refinedTokens,
271
            new Token('(', Token::TYPE_CHARACTER, 0),
272
            new Token(strval($unitLength->getValue() / 100), Token::TYPE_NUMBER, 0),
273
            new Token('*', Token::TYPE_CHARACTER, 0),
274
            new Token('viewport', Token::TYPE_WORD, 0),
275
            new Token('(', Token::TYPE_CHARACTER, 0),
276
            new Token(')', Token::TYPE_CHARACTER, 0),
277
            new Token(')', Token::TYPE_CHARACTER, 0)
278
        );
279
    }
280
}