Passed
Push — master ( 689ec5...45f08c )
by Sebastian
05:26
created

HEXParser::parse()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 30
rs 9.7
cc 4
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AppUtils\RGBAColor\FormatsConverter;
6
7
use AppUtils\RGBAColor;
8
use AppUtils\RGBAColor\ColorException;
9
use AppUtils\RGBAColor\ColorFactory;
10
use AppUtils\RGBAColor\ColorChannel;
11
12
class HEXParser
13
{
14
    /**
15
     * Converts the HEX color string to an 8-Bit color array.
16
     *
17
     * @param string $hex
18
     * @param string $name
19
     * @return RGBAColor
20
     *
21
     * @throws ColorException
22
     * @see RGBAColor::ERROR_INVALID_HEX_LENGTH
23
     */
24
    public function parse(string $hex, string $name='') : RGBAColor
25
    {
26
        $hex = ltrim($hex, '#'); // Remove the hash if present
27
        $hex = strtoupper($hex);
28
        $length = strlen($hex);
29
30
        if($length === 3)
31
        {
32
            return $this->parseHEX3($hex, $name);
33
        }
34
35
        if($length === 6)
36
        {
37
            return $this->parseHEX6($hex, $name);
38
        }
39
40
        if ($length === 8)
41
        {
42
            return $this->parseHEX8($hex, $name);
43
        }
44
45
        throw new ColorException(
46
            'Invalid HEX color value.',
47
            sprintf(
48
                'The hex string [%s] has an invalid length ([%s] characters). '.
49
                'It must be either 6 characters (RRGGBB) or 8 characters (RRGGBBAA) long.',
50
                $hex,
51
                $length
52
            ),
53
            RGBAColor::ERROR_INVALID_HEX_LENGTH
54
        );
55
    }
56
57
    /**
58
     * Parses a three-letter HEX color string to a color array.
59
     *
60
     * @param string $hex
61
     * @param string $name
62
     * @return RGBAColor
63
     */
64
    private function parseHEX3(string $hex, string $name) : RGBAColor
65
    {
66
        return ColorFactory::create(
67
            ColorChannel::EightBit(hexdec(str_repeat($hex[0], 2))),
0 ignored issues
show
Bug introduced by
It seems like hexdec(str_repeat($hex[0], 2)) can also be of type double; however, parameter $value of AppUtils\RGBAColor\ColorChannel::EightBit() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

67
            ColorChannel::EightBit(/** @scrutinizer ignore-type */ hexdec(str_repeat($hex[0], 2))),
Loading history...
68
            ColorChannel::EightBit(hexdec(str_repeat($hex[1], 2))),
69
            ColorChannel::EightBit(hexdec(str_repeat($hex[2], 2))),
70
            null,
71
            $name
72
        );
73
    }
74
75
    /**
76
     * Parses a six-letter HEX color string to a color array.
77
     * @param string $hex
78
     * @param string $name
79
     * @return RGBAColor
80
     */
81
    private function parseHEX6(string $hex, string $name) : RGBAColor
82
    {
83
        return ColorFactory::create(
84
            ColorChannel::EightBit(hexdec(substr($hex, 0, 2))),
0 ignored issues
show
Bug introduced by
It seems like hexdec(substr($hex, 0, 2)) can also be of type double; however, parameter $value of AppUtils\RGBAColor\ColorChannel::EightBit() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

84
            ColorChannel::EightBit(/** @scrutinizer ignore-type */ hexdec(substr($hex, 0, 2))),
Loading history...
85
            ColorChannel::EightBit(hexdec(substr($hex, 2, 2))),
86
            ColorChannel::EightBit(hexdec(substr($hex, 4, 2))),
87
            null,
88
            $name
89
        );
90
    }
91
92
    /**
93
     * Parses an eight-letter HEX color string (with alpha) to a color array.
94
     * @param string $hex
95
     * @param string $name
96
     * @return RGBAColor
97
     */
98
    private function parseHEX8(string $hex, string $name) : RGBAColor
99
    {
100
        return ColorFactory::create(
101
            ColorChannel::EightBit(hexdec(substr($hex, 0, 2))),
0 ignored issues
show
Bug introduced by
It seems like hexdec(substr($hex, 0, 2)) can also be of type double; however, parameter $value of AppUtils\RGBAColor\ColorChannel::EightBit() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

101
            ColorChannel::EightBit(/** @scrutinizer ignore-type */ hexdec(substr($hex, 0, 2))),
Loading history...
102
            ColorChannel::EightBit(hexdec(substr($hex, 2, 2))),
103
            ColorChannel::EightBit(hexdec(substr($hex, 4, 2))),
104
            ColorChannel::EightBit(hexdec(substr($hex, 6, 2))),
105
            $name
106
        );
107
    }
108
}
109