Completed
Push — master ( 787a24...f25cb1 )
by Tomáš
42:21
created

Formatter::getLocalName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Inspirum\XML\Services;
4
5
use InvalidArgumentException;
6
7
class Formatter
8
{
9
    /**
10
     * Parse node name to namespace prefix and local name
11
     *
12
     * @param string $name
13
     *
14
     * @return array<int,string|null>
15
     */
16
    public static function parseQualifiedName(string $name): array
17
    {
18
        static::validateElementName($name);
0 ignored issues
show
Bug introduced by
Since validateElementName() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of validateElementName() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
19
20
        return array_pad(explode(':', $name, 2), -2, null);
21
    }
22
23
    /**
24
     * Get local name from node name
25
     *
26
     * @param string $name
27
     *
28
     * @return string|null
29
     */
30
    public static function getLocalName(string $name): ?string
31
    {
32
        return static::parseQualifiedName($name)[1];
33
    }
34
35
    /**
36
     * Get namespace prefix from node name
37
     *
38
     * @param string $name
39
     *
40
     * @return string|null
41
     */
42
    public static function getNamespacePrefix(string $name): ?string
43
    {
44
        return static::parseQualifiedName($name)[0];
45
    }
46
47
    /**
48
     * Validate element name
49
     *
50
     * @param string $value
51
     *
52
     * @return void
53
     *
54
     * @throws \InvalidArgumentException
55
     */
56
    private static function validateElementName(string $value): void
57
    {
58
        $regex = '/^[a-zA-Z][a-zA-Z0-9]*(\:[a-zA-Z][a-zA-Z0-9]*)?$/';
59
60
        if (preg_match($regex, $value) !== 1) {
61
            throw new InvalidArgumentException(
62
                sprintf('Element name or namespace prefix [%s] has invalid value', $value)
63
            );
64
        }
65
    }
66
67
    /**
68
     * Normalize value.
69
     *
70
     * @param mixed $value
71
     *
72
     * @return mixed
73
     */
74
    public static function encodeValue($value)
75
    {
76
        if (is_bool($value)) {
77
            $value = $value ? 'true' : 'false';
78
        }
79
80
        return $value;
81
    }
82
83
    /**
84
     * Normalize value.
85
     *
86
     * @param mixed $value
87
     *
88
     * @return mixed
89
     */
90
    public static function decodeValue($value)
91
    {
92
        if (is_numeric($value)) {
93
            return $value + 0;
94
        }
95
96
        if (in_array($value, ['true', 'True'])) {
97
            return true;
98
        }
99
100
        if (in_array($value, ['false', 'False'])) {
101
            return false;
102
        }
103
104
        return $value;
105
    }
106
}
107