1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace POData\Providers\Metadata\Type; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Guid. |
7
|
|
|
*/ |
8
|
|
|
class Guid implements IType |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Gets the type code |
12
|
|
|
* Note: implementation of IType::getTypeCode. |
13
|
|
|
* |
14
|
|
|
* @return TypeCode |
15
|
|
|
*/ |
16
|
|
|
public function getTypeCode() |
17
|
|
|
{ |
18
|
|
|
return TypeCode::GUID; |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Checks this type is compatible with another type |
23
|
|
|
* Note: implementation of IType::isCompatibleWith. |
24
|
|
|
* |
25
|
|
|
* @param IType $type Type to check compatibility |
26
|
|
|
* |
27
|
|
|
* @return bool |
28
|
|
|
*/ |
29
|
|
|
public function isCompatibleWith(IType $type) |
30
|
|
|
{ |
31
|
|
|
return TypeCode::GUID == $type->getTypeCode(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Validate a value in Astoria uri is in a format for this type |
36
|
|
|
* Note: implementation of IType::validate. |
37
|
|
|
* |
38
|
|
|
* @param string $value The value to validate |
39
|
|
|
* @param string &$outValue The stripped form of $value that can be |
40
|
|
|
* used in PHP expressions |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
|
|
public function validate($value, &$outValue) |
45
|
|
|
{ |
46
|
|
|
////The GUID value present in the $filter option should have one of the following pattern. |
47
|
|
|
//1. '/^guid\'([0-9a-fA-F]{32}\')?$/'; |
|
|
|
|
48
|
|
|
//2. '/^guid\'([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\')?$/'; |
|
|
|
|
49
|
|
|
//3. '/^guid\'\{?([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\}?\')?$/'; |
|
|
|
|
50
|
|
|
//4. '/^guid\'\(?([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\)?\')?$/'; |
|
|
|
|
51
|
|
|
|
52
|
|
|
$length = strlen($value); |
53
|
|
|
if (38 != $length && 42 != $length && 44 != $length) { |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (0 !== strpos($value, 'guid\'') && '\'' != $value[$length - 1]) { |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$value = substr($value, 4, $length - 4); |
62
|
|
|
if (!self::validateWithoutPrefix($value, true)) { |
|
|
|
|
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$outValue = $value; |
67
|
|
|
|
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Gets full name of this type in EDM namespace |
73
|
|
|
* Note: implementation of IType::getFullTypeName. |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getFullTypeName() |
78
|
|
|
{ |
79
|
|
|
return 'Edm.Guid'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Converts the given string value to guid type. |
84
|
|
|
* |
85
|
|
|
* @param string $stringValue value to convert |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function convert($stringValue) |
90
|
|
|
{ |
91
|
|
|
$len = strlen($stringValue); |
92
|
|
|
if (2 > $len) { |
93
|
|
|
return $stringValue; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return substr($stringValue, 1, $len - 2); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Convert the given value to a form that can be used in OData uri. |
101
|
|
|
* Note: The calling function should not pass null value, as this |
102
|
|
|
* function will not perform any check for nullability. |
103
|
|
|
* |
104
|
|
|
* @param mixed $value value to convert |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function convertToOData($value) |
109
|
|
|
{ |
110
|
|
|
return 'guid\'' . urlencode($value) . '\''; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Validates guid. |
115
|
|
|
* |
116
|
|
|
* @param string $guid The guid to validate |
117
|
|
|
* @param bool $withQuotes Whether the above guid have quote as delimiter |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public static function validateWithoutPrefix($guid, $withQuotes = false) |
122
|
|
|
{ |
123
|
|
|
if ($withQuotes) { |
124
|
|
|
$patt = ['/^(\'[0-9a-fA-F]{32}\')?$/', |
125
|
|
|
'/^(\'[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\')?$/', |
126
|
|
|
'/^\'\{?([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\}?\')?$/', |
127
|
|
|
'/^\'\(?([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\)?\')?$/', ]; |
128
|
|
|
} else { |
129
|
|
|
$patt = ['/^([0-9a-fA-F]{32})?$/', |
130
|
|
|
'/^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})?$/', |
131
|
|
|
'/^\{?([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\}?)?$/', |
132
|
|
|
'/^\(?([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\)?)?$/', ]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
foreach ($patt as $pattern) { |
136
|
|
|
if (1 == preg_match($pattern, $guid)) { |
137
|
|
|
return true; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Check the equality of two guids. This function will not validate the |
146
|
|
|
* guids one should use validate or validateWithoutPrefix to validate the |
147
|
|
|
* guids before using them with this function. |
148
|
|
|
* |
149
|
|
|
* @param string $guid1 First guid |
150
|
|
|
* @param string $guid2 Second guid |
151
|
|
|
* |
152
|
|
|
* @return bool True if both guids are same else false |
153
|
|
|
*/ |
154
|
|
|
public static function guidEqual($guid1, $guid2) |
155
|
|
|
{ |
156
|
|
|
$guid1 = str_replace(['{', '}', '(', ')', '-'], '', $guid1); |
157
|
|
|
$guid2 = str_replace(['{', '}', '(', ')', '-'], '', $guid2); |
158
|
|
|
|
159
|
|
|
return 0 === strcasecmp($guid1, $guid2); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Gets full name of the type implementing this interface in EDM namespace |
164
|
|
|
* Note: implementation of IType::getFullTypeName. |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
public function getName() |
169
|
|
|
{ |
170
|
|
|
return $this->getFullTypeName(); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|