1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
namespace AlgoWeb\ODataMetadata\Csdl\Internal; |
7
|
|
|
|
8
|
|
|
use AlgoWeb\ODataMetadata\EdmUtil; |
9
|
|
|
use AlgoWeb\ODataMetadata\Enums\ValueKind; |
10
|
|
|
use AlgoWeb\ODataMetadata\Exception\NotSupportedException; |
11
|
|
|
use AlgoWeb\ODataMetadata\Interfaces\Values\IBinaryValue; |
12
|
|
|
use AlgoWeb\ODataMetadata\Interfaces\Values\IBooleanValue; |
13
|
|
|
use AlgoWeb\ODataMetadata\Interfaces\Values\IDateTimeOffsetValue; |
14
|
|
|
use AlgoWeb\ODataMetadata\Interfaces\Values\IDateTimeValue; |
15
|
|
|
use AlgoWeb\ODataMetadata\Interfaces\Values\IDecimalValue; |
16
|
|
|
use AlgoWeb\ODataMetadata\Interfaces\Values\IFloatingValue; |
17
|
|
|
use AlgoWeb\ODataMetadata\Interfaces\Values\IGuidValue; |
18
|
|
|
use AlgoWeb\ODataMetadata\Interfaces\Values\IIntegerValue; |
19
|
|
|
use AlgoWeb\ODataMetadata\Interfaces\Values\IPrimitiveValue; |
20
|
|
|
use AlgoWeb\ODataMetadata\Interfaces\Values\IStringValue; |
21
|
|
|
use AlgoWeb\ODataMetadata\Interfaces\Values\ITimeValue; |
22
|
|
|
use AlgoWeb\ODataMetadata\StringConst; |
23
|
|
|
use AlgoWeb\ODataMetadata\Util\XmlConvert; |
24
|
|
|
use DateTime; |
25
|
|
|
|
26
|
|
|
abstract class EdmValueWriter |
27
|
|
|
{ |
28
|
|
|
private static $Hex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']; |
29
|
|
|
|
30
|
|
|
public static function PrimitiveValueAsXml(IPrimitiveValue $v): string |
31
|
|
|
{ |
32
|
|
|
if (method_exists($v, 'getValue')) { |
33
|
|
|
EdmUtil::checkArgumentNull($v->getValue(), 'v->getValue'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
switch ($v->getValueKind()) { |
37
|
|
|
case ValueKind::Boolean(): |
38
|
|
|
assert($v instanceof IBooleanValue); |
39
|
|
|
return self::BooleanAsXml($v->getValue()); |
40
|
|
|
case ValueKind::Integer(): |
41
|
|
|
assert($v instanceof IIntegerValue); |
42
|
|
|
return self::LongAsXml($v->getValue()); |
43
|
|
|
case ValueKind::Floating(): |
44
|
|
|
assert($v instanceof IFloatingValue); |
45
|
|
|
return self::FloatAsXml($v->getValue()); |
46
|
|
|
case ValueKind::Guid(): |
47
|
|
|
assert($v instanceof IGuidValue); |
48
|
|
|
return self::GuidAsXml($v->getValue()); |
49
|
|
|
case ValueKind::Binary(): |
50
|
|
|
assert($v instanceof IBinaryValue); |
51
|
|
|
return self::BinaryAsXml(/** @scrutinizer ignore-type */$v->getValue()); |
52
|
|
|
case ValueKind::Decimal(): |
53
|
|
|
assert($v instanceof IDecimalValue); |
54
|
|
|
return self::DecimalAsXml($v->getValue()); |
55
|
|
|
case ValueKind::String(): |
56
|
|
|
assert($v instanceof IStringValue); |
57
|
|
|
return self::StringAsXml(/** @scrutinizer ignore-type */$v->getValue()); |
58
|
|
|
case ValueKind::DateTime(): |
59
|
|
|
assert($v instanceof IDateTimeValue); |
60
|
|
|
return self::DateTimeAsXml($v->getValue()); |
61
|
|
|
case ValueKind::DateTimeOffset(): |
62
|
|
|
assert($v instanceof IDateTimeOffsetValue); |
63
|
|
|
return self::DateTimeOffsetAsXml($v->getValue()); |
64
|
|
|
case ValueKind::Time(): |
65
|
|
|
assert($v instanceof ITimeValue); |
66
|
|
|
return self::TimeAsXml($v->getValue()); |
67
|
|
|
default: |
68
|
|
|
/* @noinspection PhpUnhandledExceptionInspection */ |
69
|
|
|
throw new NotSupportedException(StringConst::ValueWriter_NonSerializableValue($v->getValueKind()->getKey())); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function StringAsXml(string $s): string |
74
|
|
|
{ |
75
|
|
|
return $s; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public static function BinaryAsXml(array $binary): string |
79
|
|
|
{ |
80
|
|
|
$chars = []; |
81
|
|
|
$numElements = count($binary); |
82
|
|
|
for ($i = 0; $i < $numElements; ++$i) { |
83
|
|
|
$chars[$i << 1] = self::$Hex[$binary[$i] >> 4]; |
84
|
|
|
$chars[$i << 1 | 1] = self::$Hex[$binary[$i] & 0x0F]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return implode('', $chars); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public static function BooleanAsXml(bool $b): string |
91
|
|
|
{ |
92
|
|
|
return XmlConvert::boolToString($b); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
public static function IntAsXml(int $i): string |
97
|
|
|
{ |
98
|
|
|
return XmlConvert::intToString($i); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
public static function LongAsXml(int $l): string |
103
|
|
|
{ |
104
|
|
|
return XmlConvert::intToString($l); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public static function FloatAsXml(float $f): string |
108
|
|
|
{ |
109
|
|
|
return XmlConvert::floatToString($f) . 'F'; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public static function DecimalAsXml(float $d): string |
113
|
|
|
{ |
114
|
|
|
return XmlConvert::floatToString($d) . 'M'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public static function DateTimeAsXml(DateTime $d): string |
118
|
|
|
{ |
119
|
|
|
return $d->format('Y-m-d\TH:i:s.u') . '000'; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public static function TimeAsXml(DateTime $d): string |
123
|
|
|
{ |
124
|
|
|
return $d->format("h\:i\:s\.u"); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public static function DateTimeOffsetAsXml(DateTime $d): string |
128
|
|
|
{ |
129
|
|
|
return $d->format('Y-m-d\TH:i:s.v\ZP'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public static function GuidAsXml(string $g): string |
133
|
|
|
{ |
134
|
|
|
return $g; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|