|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the skidata-library package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2017 WEBEWEB |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WBW\Library\SkiData\Parser; |
|
13
|
|
|
|
|
14
|
|
|
use DateTime; |
|
15
|
|
|
use WBW\Library\SkiData\Entity\SkiDataStartRecordFormat; |
|
16
|
|
|
use WBW\Library\SkiData\Exception\SkiDataMissingStartRecordFormatException; |
|
17
|
|
|
use WBW\Library\SkiData\Exception\SkiDataTooLongDataException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Abstract SkiData parser. |
|
21
|
|
|
* |
|
22
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
23
|
|
|
* @package WBW\Library\SkiData\Parser |
|
24
|
|
|
* @abstract |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract class AbstractSkiDataParser implements SkiDataParserInterface { |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Start record format. |
|
30
|
|
|
* |
|
31
|
|
|
* @var SkiDataStartRecordFormat |
|
32
|
|
|
*/ |
|
33
|
|
|
private $startRecordFormat; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Constructor. |
|
37
|
|
|
*/ |
|
38
|
|
|
protected function __construct() { |
|
39
|
|
|
// NOTHING TO DO. |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Decode a date string. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $str The string. |
|
46
|
|
|
* @return DateTime Returns the decoded string into DateTime. |
|
47
|
|
|
*/ |
|
48
|
|
|
final protected function decodeDate($str) { |
|
49
|
|
|
return "" === $str ? null : DateTime::createFromFormat("!" . self::DATE_FORMAT, $str); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Decode a datetime string. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $str The string. |
|
56
|
|
|
* @return DateTime Returns the decoded string into DateTime. |
|
57
|
|
|
*/ |
|
58
|
|
|
final protected function decodeDateTime($str) { |
|
59
|
|
|
return "" === $str ? null : DateTime::createFromFormat(self::DATETIME_FORMAT, $str); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Decode a integer string. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $str The string. |
|
66
|
|
|
* @return integer Returns the decoded string into integer. |
|
67
|
|
|
*/ |
|
68
|
|
|
final protected function decodeInteger($str) { |
|
69
|
|
|
return "" === $str ? null : intval($str); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Decode a string. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $str The string. |
|
76
|
|
|
* @return string Returns the decoded string into string. |
|
77
|
|
|
*/ |
|
78
|
|
|
final protected function decodeString($str) { |
|
79
|
|
|
return ("" === $str || 2 === strlen($str)) ? "" : substr($str, 1, strlen($str) - 2); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Encode a boolean value. |
|
84
|
|
|
* |
|
85
|
|
|
* @param boolean $value The value. |
|
86
|
|
|
* @return string Returns the encoded boolean value. |
|
87
|
|
|
*/ |
|
88
|
|
|
final protected function encodeBoolean($value) { |
|
89
|
|
|
return true === $value ? "1" : "0"; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Encode a date value. |
|
94
|
|
|
* |
|
95
|
|
|
* @param DateTime $value The value. |
|
96
|
|
|
* @return string Returns the encoded datetime value. |
|
97
|
|
|
*/ |
|
98
|
|
|
final protected function encodeDate(DateTime $value = null) { |
|
99
|
|
|
return null !== $value ? $value->format(self::DATE_FORMAT) : ""; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Encode a datetime value. |
|
104
|
|
|
* |
|
105
|
|
|
* @param DateTime $value The value. |
|
106
|
|
|
* @return string Returns the encoded datetime value. |
|
107
|
|
|
*/ |
|
108
|
|
|
final protected function encodeDateTime(DateTime $value = null) { |
|
109
|
|
|
return null !== $value ? $value->format(self::DATETIME_FORMAT) : ""; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Encode an integer value. |
|
114
|
|
|
* |
|
115
|
|
|
* @param integer $value The value. |
|
116
|
|
|
* @param integer $length The length. |
|
117
|
|
|
* @return string Returns the encoded integer. |
|
118
|
|
|
* @throws SkiDataTooLongDataException Throws a SkiData too long data exception if the value exceeds the length. |
|
119
|
|
|
*/ |
|
120
|
|
|
final protected function encodeInteger($value, $length) { |
|
121
|
|
|
if (null === $value) { |
|
|
|
|
|
|
122
|
|
|
return ""; |
|
123
|
|
|
} |
|
124
|
|
|
$format = "%'.0" . $length . "d"; |
|
125
|
|
|
$output = sprintf($format, $value); |
|
126
|
|
|
if ($length < strlen($output)) { |
|
127
|
|
|
throw new SkiDataTooLongDataException($value, $length); |
|
128
|
|
|
} |
|
129
|
|
|
return $output; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Encode a string value. |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $value The value. |
|
136
|
|
|
* @param integer $length The length. |
|
137
|
|
|
* @return string Returns the encoded string. |
|
138
|
|
|
* @throws SkiDataTooLongDataException Throws a SkiData too long data exception if the value exceeds the length. |
|
139
|
|
|
*/ |
|
140
|
|
|
final protected function encodeString($value, $length = -1) { |
|
141
|
|
|
if (-1 !== $length && $length < strlen($value)) { |
|
142
|
|
|
throw new SkiDataTooLongDataException($value, $length); |
|
143
|
|
|
} |
|
144
|
|
|
return "\"" . substr($value, 0, (-1 === $length ? strlen($value) : $length)) . "\""; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Get the start record format. |
|
149
|
|
|
* |
|
150
|
|
|
* @return SkiDataStartRecordFormat Returns the start record format. |
|
151
|
|
|
*/ |
|
152
|
|
|
final public function getStartRecordFormat() { |
|
153
|
|
|
return $this->startRecordFormat; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Determines if the version of record structure is less or equal than $versionRecordStructure. |
|
158
|
|
|
* |
|
159
|
|
|
* @param integer $versionRecordStructure The version of record structure. |
|
160
|
|
|
* @return boolean Returns true in case of success, false otherwise. |
|
161
|
|
|
* @throws SkiDataMissingStartRecordFormatException Throws a SkiData missing start record format exception if the start record format is missing. |
|
162
|
|
|
*/ |
|
163
|
|
|
final protected function isVersionRecordStructure($versionRecordStructure) { |
|
164
|
|
|
if (null === $this->startRecordFormat) { |
|
165
|
|
|
throw new SkiDataMissingStartRecordFormatException(); |
|
166
|
|
|
} |
|
167
|
|
|
return $versionRecordStructure <= $this->startRecordFormat->getVersionRecordStructure(); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Set the start record format. |
|
172
|
|
|
* |
|
173
|
|
|
* @param SkiDataStartRecordFormat $startRecordFormat The start record format. |
|
174
|
|
|
* @return AbstractSkiDataParser Returns the SkiData parser. |
|
175
|
|
|
*/ |
|
176
|
|
|
final public function setStartRecordFormat(SkiDataStartRecordFormat $startRecordFormat) { |
|
177
|
|
|
$this->startRecordFormat = $startRecordFormat; |
|
178
|
|
|
return $this; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.