Passed
Branch master (950424)
by Christopher
11:06
created

Char::convert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace POData\Providers\Metadata\Type;
4
5
use Mockery\Matcher\Not;
6
use POData\Common\NotImplementedException;
7
8
/**
9
 * Class Char.
10
 */
11
class Char implements IType
12
{
13
    const A = 65;
14
    const Z = 90;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_LNUMBER on line 14 at column 10
Loading history...
15
    const SMALL_A = 97;
16
    const SMALL_Z = 122;
17
    const F = 70;
18
    const SMALL_F = 102;
19
    const ZERO = 48;
20
    const NINE = 57;
21
    const TAB = 9;
22
    const NEWLINE = 10;
23
    const CARRIAGE_RETURN = 13;
24
    const SPACE = 32;
25
26
    /**
27
     * Gets the type code
28
     * Note: implementation of IType::getTypeCode.
29
     *
30
     * @return TypeCode
31
     */
32
    public function getTypeCode()
33
    {
34
        return TypeCode::CHAR;
35
    }
36
37
    /**
38
     * Checks this type is compatible with another type
39
     * Note: implementation of IType::isCompatibleWith.
40
     *
41
     * @param IType $type Type to check compatibility
42
     *
43
     * @return bool
44
     */
45
    public function isCompatibleWith(IType $type)
46
    {
47
        switch ($type->getTypeCode()) {
48
            case TypeCode::BYTE:
49
            case TypeCode::CHAR:
50
                return true;
51
        }
52
53
        return false;
54
    }
55
56
    /**
57
     * Validate a value in Astoria uri is in a format for this type
58
     * Note: implementation of IType::validate.
59
     *
60
     * @param string $value     The value to validate
61
     * @param string &$outValue The stripped form of $value that can
62
     *                          be used in PHP expressions
63
     *
64
     * @throws NotImplementedException
65
     * @return NoType
66
     */
67
    public function validate($value, &$outValue)
68
    {
69
        //No EDM Char primitive type
70
        throw new NotImplementedException();
71
    }
72
73
    /**
74
     * Gets full name of this type in EDM namespace
75
     * Note: implementation of IType::getFullTypeName.
76
     *
77
     * @return string
78
     */
79
    public function getFullTypeName()
80
    {
81
        return 'System.Char';
82
    }
83
84
    /**
85
     * Converts the given string value to char type.
86
     * Note: This function will not perform any conversion.
87
     *
88
     * @param string $stringValue The value to convert
89
     *
90
     * @return string
91
     */
92
    public function convert($stringValue)
93
    {
94
        return $stringValue;
95
    }
96
97
    /**
98
     * Convert the given value to a form that can be used in OData uri.
99
     * Note: The calling function should not pass null value, as this
100
     * function will not perform any check for nullability.
101
     *
102
     * @param mixed $value The value to convert
103
     *
104
     * @return string
105
     */
106
    public function convertToOData($value)
107
    {
108
        return $value;
109
    }
110
111
    /**
112
     * Checks a character is whilespace.
113
     *
114
     * @param char|string $char character to check
115
     *
116
     * @return bool
117
     */
118
    public static function isWhiteSpace($char)
119
    {
120
        $asciiVal = ord($char);
121
122
        return self::SPACE == $asciiVal
123
            || self::TAB == $asciiVal
124
            || self::CARRIAGE_RETURN == $asciiVal
125
            || self::NEWLINE == $asciiVal;
126
    }
127
128
    /**
129
     * Checks a character is letter.
130
     *
131
     * @param char|string $char character to check
132
     *
133
     * @return bool
134
     */
135
    public static function isLetter($char)
136
    {
137
        $asciiVal = ord($char);
138
139
        return ($asciiVal >= self::A && $asciiVal <= self::Z)
140
            || ($asciiVal >= self::SMALL_A && $asciiVal <= self::SMALL_Z);
141
    }
142
143
    /**
144
     * Checks a character is digit.
145
     *
146
     * @param char|string $char character to check
147
     *
148
     * @return bool
149
     */
150
    public static function isDigit($char)
151
    {
152
        $asciiVal = ord($char);
153
154
        return self::ZERO <= $asciiVal && self::NINE >= $asciiVal;
155
    }
156
157
    /**
158
     * Checks a character is letter or digit.
159
     *
160
     * @param char|string $char character to check
161
     *
162
     * @return bool
163
     */
164
    public static function isLetterOrDigit($char)
165
    {
166
        return self::isDigit($char) || self::isLetter($char);
167
    }
168
169
    /**
170
     * Gets full name of the type implementing this interface in EDM namespace
171
     * Note: implementation of IType::getFullTypeName.
172
     *
173
     * @return string
174
     */
175
    public function getName()
176
    {
177
        return $this->getFullTypeName();
178
    }
179
}
180