1
|
|
|
<?php |
2
|
|
|
/******************************************************************************** |
3
|
|
|
* Apache License, Version 2.0 * |
4
|
|
|
* * |
5
|
|
|
* Copyright [2020] [Nurlan Mukhanov <[email protected]>] * |
6
|
|
|
* * |
7
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); * |
8
|
|
|
* you may not use this file except in compliance with the License. * |
9
|
|
|
* You may obtain a copy of the License at * |
10
|
|
|
* * |
11
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 * |
12
|
|
|
* * |
13
|
|
|
* Unless required by applicable law or agreed to in writing, software * |
14
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, * |
15
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * |
16
|
|
|
* See the License for the specific language governing permissions and * |
17
|
|
|
* limitations under the License. * |
18
|
|
|
* * |
19
|
|
|
********************************************************************************/ |
20
|
|
|
|
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
namespace DBD\Entity; |
24
|
|
|
|
25
|
|
|
use DBD\Entity\Common\EntityException; |
26
|
|
|
use DBD\Entity\Primitives\NumericPrimitives; |
27
|
|
|
use DBD\Entity\Primitives\StringPrimitives; |
28
|
|
|
use DBD\Entity\Primitives\TimePrimitives; |
29
|
|
|
use MyCLabs\Enum\Enum; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class Primitive |
33
|
|
|
* |
34
|
|
|
* @see https://docs.oasis-open.org/odata/odata-csdl-xml/v4.01/csprd05/odata-csdl-xml-v4.01-csprd05.html#sec_PrimitiveTypes |
35
|
|
|
* @method static Primitive Binary() |
36
|
|
|
* @method static Primitive Boolean() |
37
|
|
|
* @method static Primitive Byte() |
38
|
|
|
* @method static Primitive Date() |
39
|
|
|
* @method static Primitive DateTimeOffset() |
40
|
|
|
* @method static Primitive Decimal() |
41
|
|
|
* @method static Primitive Double() |
42
|
|
|
* @method static Primitive Duration() |
43
|
|
|
* @method static Primitive Guid() |
44
|
|
|
* @method static Primitive Int16() |
45
|
|
|
* @method static Primitive Int32() |
46
|
|
|
* @method static Primitive Int64() |
47
|
|
|
* @method static Primitive SByte() |
48
|
|
|
* @method static Primitive Single() |
49
|
|
|
* @method static Primitive Stream() |
50
|
|
|
* @method static Primitive String() |
51
|
|
|
* @method static Primitive TimeOfDay() |
52
|
|
|
* @package DBD\Entity |
53
|
|
|
*/ |
54
|
|
|
class Primitive extends Enum implements StringPrimitives, NumericPrimitives, TimePrimitives |
55
|
|
|
{ |
56
|
|
|
public const BOOLEAN = "boolean"; |
57
|
|
|
|
58
|
|
|
/** @var string Binary-valued logic */ |
59
|
|
|
public const Boolean = "Boolean"; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $type |
63
|
|
|
* |
64
|
|
|
* @return Primitive |
65
|
|
|
* @throws EntityException |
66
|
|
|
*/ |
67
|
|
|
public static function fromType(string $type): Primitive |
68
|
|
|
{ |
69
|
|
|
switch (strtolower(trim($type))) { |
70
|
|
|
|
71
|
|
|
case 'bytea': |
72
|
|
|
return Primitive::Binary(); |
73
|
|
|
|
74
|
|
|
case 'boolean': |
75
|
|
|
case 'bool': |
76
|
|
|
return Primitive::Boolean(); |
77
|
|
|
|
78
|
|
|
case 'date': |
79
|
|
|
case 'timestamp': |
80
|
|
|
return Primitive::Date(); |
81
|
|
|
|
82
|
|
|
case 'time': |
83
|
|
|
case 'timetz': |
84
|
|
|
return Primitive::TimeOfDay(); |
85
|
|
|
|
86
|
|
|
case 'timestamptz': |
87
|
|
|
return Primitive::DateTimeOffset(); |
88
|
|
|
case 'numeric': |
89
|
|
|
case 'decimal': |
90
|
|
|
return Primitive::Decimal(); |
91
|
|
|
|
92
|
|
|
case 'float8': |
93
|
|
|
return Primitive::Double(); |
94
|
|
|
|
95
|
|
|
case 'interval': |
96
|
|
|
return Primitive::Duration(); |
97
|
|
|
|
98
|
|
|
case 'uuid': |
99
|
|
|
return Primitive::Guid(); |
100
|
|
|
|
101
|
|
|
case 'int2': |
102
|
|
|
case 'smallint': |
103
|
|
|
case 'smallserial': |
104
|
|
|
case 'serial2': |
105
|
|
|
return Primitive::Int16(); |
106
|
|
|
|
107
|
|
|
case 'int': |
108
|
|
|
case 'int4': |
109
|
|
|
case 'integer': |
110
|
|
|
case 'serial4': |
111
|
|
|
case 'serial': |
112
|
|
|
return Primitive::Int32(); |
113
|
|
|
|
114
|
|
|
case 'int8': |
115
|
|
|
case 'bigint': |
116
|
|
|
case 'bigserial': |
117
|
|
|
case 'serial8': |
118
|
|
|
return Primitive::Int64(); |
119
|
|
|
|
120
|
|
|
case 'float4': |
121
|
|
|
case 'real': |
122
|
|
|
return Primitive::Single(); |
123
|
|
|
|
124
|
|
|
case 'varchar': |
125
|
|
|
case 'text': |
126
|
|
|
case 'cidr': |
127
|
|
|
case 'inet': |
128
|
|
|
case 'json': |
129
|
|
|
case 'jsonb': |
130
|
|
|
case 'macaddr': |
131
|
|
|
case 'macaddr8': |
132
|
|
|
case 'char': |
133
|
|
|
case 'tsquery': |
134
|
|
|
case 'tsvector': |
135
|
|
|
case 'xml': |
136
|
|
|
case 'bpchar': |
137
|
|
|
return Primitive::String(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
throw new EntityException("Not described type found: $type"); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* If you need to set your own types - just extend this class and override this method |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
public function getPhpVarType(): string |
149
|
|
|
{ |
150
|
|
|
switch ($this->value) { |
151
|
|
|
case self::Int16: |
152
|
|
|
case self::Int32: |
153
|
|
|
case self::Int64: |
154
|
|
|
return self::INTEGER; |
155
|
|
|
|
156
|
|
|
case self::Boolean; |
157
|
|
|
return self::BOOLEAN; |
158
|
|
|
|
159
|
|
|
case self::Double: |
160
|
|
|
case self::Single; |
161
|
|
|
return self::FLOAT; |
162
|
|
|
default: |
163
|
|
|
return self::STRING; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|