Completed
Push — master ( f583f7...ab742f )
by Joschi
02:07
created

LengthFactory::createAbsoluteLengthFromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
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 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\PercentageLength;
43
use Jkphl\Respimgcss\Application\Model\ViewportLength;
44
45
/**
46
 * AbstractLength Factory
47
 *
48
 * @package    Jkphl\Respimgcss
49
 * @subpackage Jkphl\Respimgcss\Application\Factory
50
 */
51
class LengthFactory extends AbstractLengthFactory
52
{
53
    /**
54
     * Absolute units
55
     *
56
     * @var array
57
     */
58
    const UNITS_ABSOLUTE = [
59
        UnitLengthInterface::UNIT_PIXEL,
60
        UnitLengthInterface::UNIT_EM,
61
        UnitLengthInterface::UNIT_REM,
62
        UnitLengthInterface::UNIT_CM,
63
        UnitLengthInterface::UNIT_MM,
64
        UnitLengthInterface::UNIT_IN,
65
        UnitLengthInterface::UNIT_PC,
66
        UnitLengthInterface::UNIT_PT,
67
    ];
68
    /**
69
     * Relative units
70
     *
71
     * @var array
72
     */
73
    const UNITS_RELATIVE = [
74
        UnitLengthInterface::UNIT_PERCENT,
75
        UnitLengthInterface::UNIT_VW,
76
    ];
77
78
    /**
79
     * Parse a length string and return a length with unit instance
80
     *
81
     * @param string $length AbstractLength string
82
     *
83
     * @return UnitLengthInterface AbstractLength with unit
84
     */
85 30
    public function createLengthFromString(string $length): UnitLengthInterface
86
    {
87 30
        $valueAndUnit = $this->matchValueAndUnit(
88 30
            $length,
89 30
            array_merge(self::UNITS_ABSOLUTE, self::UNITS_RELATIVE)
90
        );
91
92
        // If it's an absolute unit
93 28
        if (in_array($valueAndUnit[2], self::UNITS_ABSOLUTE)) {
94 20
            return new AbsoluteLength(floatval($valueAndUnit[1]), $valueAndUnit[2], $this->lengthNormalizerService);
95
        }
96
97 16
        return $this->makeRelativeInstance($valueAndUnit[1], $valueAndUnit[2]);
98
    }
99
100
    /**
101
     * Match the value and unit of a length descriptor
102
     *
103
     * @param string $length AbstractLength descriptor
104
     * @param array $units   Allowed units
105
     *
106
     * @return array Value and unit (PCRE match)
107
     */
108 31
    protected function matchValueAndUnit(string $length, array $units): array
109
    {
110 31
        $length = trim(strtolower($length));
111 31
        if (!strlen($length) || !preg_match('/^(\d+(?:\.\d+)?)('.implode('|', $units).')$/i', $length, $valueAndUnit)) {
112 5
            throw new InvalidArgumentException(
113 5
                sprintf(InvalidArgumentException::INVALID_LENGTH_STR, $length),
114 5
                InvalidArgumentException::INVALID_LENGTH
115
            );
116
        }
117
118 29
        return $valueAndUnit;
119
    }
120
121
    /**
122
     * Create a relative value with unit instance
123
     *
124
     * @param string $value Value
125
     * @param string $unit  Unit
126
     *
127
     * @return UnitLengthInterface Relative length with unit
128
     */
129 16
    protected function makeRelativeInstance(string $value, string $unit): UnitLengthInterface
130
    {
131
        // Viewport unit
132 16
        if ($unit === UnitLengthInterface::UNIT_VW) {
133 15
            $calculatorService = $this->calculatorServiceFactory->createCalculatorService();
134 15
            $tokens            = $calculatorService->tokenize(strval($value / 100).' *viewport()');
135
136 15
            return new ViewportLength($this->calculatorServiceFactory, $tokens, $value);
137
        }
138
139
        // Percentages
140 1
        return new PercentageLength(floatval($value));
141
    }
142
143
    /**
144
     * Parse a length string and return an absolute length with unit instance
145
     *
146
     * @param string $length AbstractLength string
147
     *
148
     * @return AbsoluteLength Absolute length with unit
149
     */
150 12
    public function createAbsoluteLengthFromString(string $length): AbsoluteLength
151
    {
152 12
        $valueAndUnit = $this->matchValueAndUnit($length, self::UNITS_ABSOLUTE);
153
154 9
        return new AbsoluteLength(floatval($valueAndUnit[1]), $valueAndUnit[2], $this->lengthNormalizerService);
155
    }
156
}