1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace POData\Providers\Metadata\Type; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class StringType |
7
|
|
|
* @package POData\Providers\Metadata\Type |
8
|
|
|
*/ |
9
|
|
|
class StringType implements IType |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Gets the type code |
13
|
|
|
* Note: implementation of IType::getTypeCode |
14
|
|
|
* |
15
|
|
|
* @return TypeCode |
16
|
|
|
*/ |
17
|
|
|
public function getTypeCode() |
18
|
|
|
{ |
19
|
|
|
return TypeCode::STRING; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Checks this type (String) is compatible with another type |
24
|
|
|
* Note: implementation of IType::isCompatibleWith |
25
|
|
|
* |
26
|
|
|
* @param IType $type Type to check compatibility |
27
|
|
|
* |
28
|
|
|
* @return boolean |
29
|
|
|
*/ |
30
|
|
|
public function isCompatibleWith(IType $type) |
31
|
|
|
{ |
32
|
|
|
return ($type->getTypeCode() == TypeCode::STRING); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Validate a value in Astoria uri is in a format for this type |
37
|
|
|
* Note: implementation of IType::validate |
38
|
|
|
* |
39
|
|
|
* @param string $value The value to validate |
40
|
|
|
* @param string &$outValue The stripped form of $value that can |
41
|
|
|
* be used in PHP expressions |
42
|
|
|
* |
43
|
|
|
* @return boolean |
44
|
|
|
*/ |
45
|
|
|
public function validate($value, &$outValue) |
46
|
|
|
{ |
47
|
|
|
if (!is_string($value)) { |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$outValue = $value; |
52
|
|
|
return true; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Gets full name of this type in EDM namespace |
57
|
|
|
* Note: implementation of IType::getFullTypeName |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function getFullTypeName() |
62
|
|
|
{ |
63
|
|
|
return 'Edm.String'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Converts the given string value to string type. |
68
|
|
|
* |
69
|
|
|
* @param string $stringValue value to convert. |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function convert($stringValue) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
//Consider the odata url option |
76
|
|
|
//$filter=ShipName eq 'Antonio%20Moreno%20Taquer%C3%ADa' |
77
|
|
|
//WebOperationContext will do urldecode, so the clause become |
78
|
|
|
//$filter=ShipName eq 'Antonio Moreno Taquería', the lexer will |
79
|
|
|
//give the token as |
80
|
|
|
//Token {Text string(25):'Antonio Moreno Taquería', Id: String}, |
|
|
|
|
81
|
|
|
//this function is used to remove the pre-post quotes from Token::Text |
82
|
|
|
//i.e. 'Antonio Moreno Taquería' |
83
|
|
|
//to Antonio Moreno Taquería |
84
|
|
|
$len = strlen($stringValue); |
85
|
|
|
if ($len < 2) { |
86
|
|
|
return $stringValue; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return substr($stringValue, 1, $len - 2); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Convert the given value to a form that can be used in OData uri. |
94
|
|
|
* Note: The calling function should not pass null value, as this |
95
|
|
|
* function will not perform any check for nullability |
96
|
|
|
* |
97
|
|
|
* @param mixed $value value to convert. |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function convertToOData($value) |
102
|
|
|
{ |
103
|
|
|
return '\'' . str_replace('%27', "''", urlencode(utf8_encode($value))) . '\''; |
104
|
|
|
} |
105
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.