Passed
Push — master ( 568aa3...59f616 )
by Joschi
02:05
created

LengthFactory::createLengthFromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
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 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
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\RelativeLength;
43
use Jkphl\Respimgcss\Application\Service\LengthNormalizerService;
44
45
/**
46
 * Length Factory
47
 *
48
 * @package    Jkphl\Respimgcss
49
 * @subpackage Jkphl\Respimgcss\Application\Factory
50
 */
51
class LengthFactory
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 Length string
82
     * @param int $emPixel   EM to pixel ratio
83
     *
84
     * @return UnitLengthInterface Length with unit
85
     */
86 12
    public static function createLengthFromString(string $length, int $emPixel = 16): UnitLengthInterface
87
    {
88 12
        $valueAndUnit = self::matchValueAndUnit($length);
89 11
        return self::makeInstance($valueAndUnit[1], $valueAndUnit[2], $emPixel);
90
    }
91
92
    /**
93
     * Match the value and unit of a length descriptor
94
     *
95
     * @param string $length Length descriptor
96
     *
97
     * @return array Value and unit (PCRE match)
98
     * @throws InvalidArgumentException If the length string is invalid
99
     */
100 12
    protected static function matchValueAndUnit($length): array
101
    {
102 12
        $length = trim(strtolower($length));
103 12
        if (!strlen($length) || !preg_match('/^(\d+(?:\.\d+)?)([pxremcintvw%]+)$/i', $length, $valueAndUnit)) {
104 1
            throw new InvalidArgumentException(
105 1
                sprintf(InvalidArgumentException::INVALID_LENGTH_STR, $length),
106 1
                InvalidArgumentException::INVALID_LENGTH
107
            );
108
        }
109
110 11
        return $valueAndUnit;
111
    }
112
113
    /**
114
     * Create a value with unit instance
115
     *
116
     * @param string $value Value
117
     * @param string $unit  Unit
118
     * @param int $emPixel  EM to pixel ratio
119
     *
120
     * @return UnitLengthInterface Length with unit
121
     * @throws InvalidArgumentException If the unit is invalid
122
     */
123 11
    protected static function makeInstance(string $value, string $unit, int $emPixel): UnitLengthInterface
124
    {
125
        // If it's an absolute unit
126 11
        if (in_array($unit, self::UNITS_ABSOLUTE)) {
127 9
            return new AbsoluteLength(floatval($value), $unit, new LengthNormalizerService($emPixel));
128
        }
129
130
        // If it's a relative unit
131 2
        if (in_array($unit, self::UNITS_RELATIVE)) {
132 1
            return new RelativeLength(floatval($value), $unit);
133
        }
134
135
        // Invalid unit
136 1
        throw new InvalidArgumentException(
137 1
            sprintf(InvalidArgumentException::INVALID_UNIT_STR, $unit),
138 1
            InvalidArgumentException::INVALID_UNIT
139
        );
140
    }
141
}