Passed
Push — master ( 1c419a...a5b134 )
by Christopher
06:29
created

Char::getTypeCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace POData\Providers\Metadata\Type;
6
7
use POData\Common\NotImplementedException;
8
9
/**
10
 * Class Char.
11
 */
12
class Char implements IType
13
{
14
    const A               = 65;
15
    const Z               = 90;
16
    const SMALL_A         = 97;
17
    const SMALL_Z         = 122;
18
    const F               = 70;
19
    const SMALL_F         = 102;
20
    const ZERO            = 48;
21
    const NINE            = 57;
22
    const TAB             = 9;
23
    const NEWLINE         = 10;
24
    const CARRIAGE_RETURN = 13;
25
    const SPACE           = 32;
26
27
    /**
28
     * Checks a character is whitespace.
29
     *
30
     * @param char|string $char character to check
31
     *
32
     * @return bool
33
     */
34
    public static function isWhiteSpace($char)
35
    {
36
        $asciiVal = ord($char);
0 ignored issues
show
Bug introduced by
It seems like $char can also be of type POData\Providers\Metadata\Type\char; however, parameter $character of ord() does only seem to accept string, 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

36
        $asciiVal = ord(/** @scrutinizer ignore-type */ $char);
Loading history...
37
38
        return self::SPACE == $asciiVal
39
            || self::TAB == $asciiVal
40
            || self::CARRIAGE_RETURN == $asciiVal
41
            || self::NEWLINE == $asciiVal;
42
    }
43
44
    /**
45
     * Checks a character is letter or digit.
46
     *
47
     * @param char|string $char character to check
48
     *
49
     * @return bool
50
     */
51
    public static function isLetterOrDigit($char)
52
    {
53
        return self::isDigit($char) || self::isLetter($char);
54
    }
55
56
    /**
57
     * Checks a character is digit.
58
     *
59
     * @param char|string $char character to check
60
     *
61
     * @return bool
62
     */
63
    public static function isDigit($char)
64
    {
65
        $asciiVal = ord($char);
0 ignored issues
show
Bug introduced by
It seems like $char can also be of type POData\Providers\Metadata\Type\char; however, parameter $character of ord() does only seem to accept string, 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

65
        $asciiVal = ord(/** @scrutinizer ignore-type */ $char);
Loading history...
66
67
        return self::ZERO <= $asciiVal && self::NINE >= $asciiVal;
68
    }
69
70
    /**
71
     * Checks a character is letter.
72
     *
73
     * @param char|string $char character to check
74
     *
75
     * @return bool
76
     */
77
    public static function isLetter($char)
78
    {
79
        $asciiVal = ord($char);
0 ignored issues
show
Bug introduced by
It seems like $char can also be of type POData\Providers\Metadata\Type\char; however, parameter $character of ord() does only seem to accept string, 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

79
        $asciiVal = ord(/** @scrutinizer ignore-type */ $char);
Loading history...
80
81
        return ($asciiVal >= self::A && $asciiVal <= self::Z)
82
            || ($asciiVal >= self::SMALL_A && $asciiVal <= self::SMALL_Z);
83
    }
84
85
    /**
86
     * Gets the type code
87
     * Note: implementation of IType::getTypeCode.
88
     *
89
     * @return TypeCode
90
     */
91
    public function getTypeCode(): TypeCode
92
    {
93
        return TypeCode::CHAR();
94
    }
95
96
    /**
97
     * Checks this type is compatible with another type
98
     * Note: implementation of IType::isCompatibleWith.
99
     *
100
     * @param IType $type Type to check compatibility
101
     *
102
     * @return bool
103
     */
104
    public function isCompatibleWith(IType $type): bool
105
    {
106
        switch ($type->getTypeCode()) {
107
            case TypeCode::BYTE():
108
            case TypeCode::CHAR():
109
                return true;
110
        }
111
112
        return false;
113
    }
114
115
    /**
116
     * Validate a value in Astoria uri is in a format for this type
117
     * Note: implementation of IType::validate.
118
     *
119
     * @param string $value     The value to validate
120
     * @param string &$outValue The stripped form of $value that can
121
     *                          be used in PHP expressions
122
     *
123
     * @throws NotImplementedException
124
     * @return bool
125
     */
126
    public function validate(string $value, ?string &$outValue): bool
127
    {
128
        //No EDM Char primitive type
129
        throw new NotImplementedException();
130
    }
131
132
    /**
133
     * Converts the given string value to char type.
134
     * Note: This function will not perform any conversion.
135
     *
136
     * @param string $stringValue The value to convert
137
     *
138
     * @return string
139
     */
140
    public function convert(string $stringValue): string
141
    {
142
        return $stringValue;
143
    }
144
145
    /**
146
     * Convert the given value to a form that can be used in OData uri.
147
     * Note: The calling function should not pass null value, as this
148
     * function will not perform any check for nullability.
149
     *
150
     * @param mixed $value The value to convert
151
     *
152
     * @return string
153
     */
154
    public function convertToOData($value): string
155
    {
156
        return $value;
157
    }
158
159
    /**
160
     * Gets full name of the type implementing this interface in EDM namespace
161
     * Note: implementation of IType::getFullTypeName.
162
     *
163
     * @return string
164
     */
165
    public function getName(): string
166
    {
167
        return $this->getFullTypeName();
168
    }
169
170
    /**
171
     * Gets full name of this type in EDM namespace
172
     * Note: implementation of IType::getFullTypeName.
173
     *
174
     * @return string
175
     */
176
    public function getFullTypeName(): string
177
    {
178
        return 'System.Char';
179
    }
180
}
181