|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the core-library package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2018 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\Core\SkiData\Parser; |
|
13
|
|
|
|
|
14
|
|
|
use DateTime; |
|
15
|
|
|
use WBW\Library\Core\Argument\IntegerHelper; |
|
16
|
|
|
use WBW\Library\Core\Exception\SkiData\SkiDataTooLongDataException; |
|
17
|
|
|
use WBW\Library\Core\SkiData\API\SkiDataParserInterface; |
|
18
|
|
|
use WBW\Library\Core\SkiData\Entity\SkiDataStartRecordFormat; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Abstract SkiData parser. |
|
22
|
|
|
* |
|
23
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
24
|
|
|
* @package WBW\Library\Core\SkiData\Parser |
|
25
|
|
|
* @abstract |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class AbstractSkiDataParser implements SkiDataParserInterface { |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Start record format. |
|
31
|
|
|
* |
|
32
|
|
|
* @var SkiDataStartRecordFormat |
|
33
|
|
|
*/ |
|
34
|
|
|
private $startRecordFormat; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Constructor. |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function __construct() { |
|
40
|
|
|
// NOTHING TO DO. |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Decode a date string. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $str The string. |
|
47
|
|
|
* @return DateTime Returns the decoded string into DateTime. |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function decodeDate($str) { |
|
50
|
|
|
$date = DateTime::createFromFormat("!" . self::DATE_FORMAT, $str); |
|
51
|
|
|
return false === $date ? null : $date; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Decode a datetime string. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $str The string. |
|
58
|
|
|
* @return DateTime Returns the decoded string into DateTime. |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function decodeDateTime($str) { |
|
61
|
|
|
$date = DateTime::createFromFormat(self::DATETIME_FORMAT, $str); |
|
62
|
|
|
return false === $date ? null : $date; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Decode a string. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $str The string. |
|
69
|
|
|
* @return string Returns the decoded string into string. |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function decodeString($str) { |
|
72
|
|
|
return ("" === $str || 2 === strlen($str)) ? "" : substr($str, 1, strlen($str) - 2); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Encode a boolean value. |
|
77
|
|
|
* |
|
78
|
|
|
* @param boolean $value The value. |
|
79
|
|
|
* @return string Returns the encoded boolean value. |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function encodeBoolean($value) { |
|
82
|
|
|
return "" . IntegerHelper::parseBoolean($value); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Encode a date value. |
|
87
|
|
|
* |
|
88
|
|
|
* @param DateTime $value The value. |
|
89
|
|
|
* @return string Returns the encoded datetime value. |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function encodeDate(DateTime $value = null) { |
|
92
|
|
|
return null === $value ? "" : $value->format(self::DATE_FORMAT); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Encode a datetime value. |
|
97
|
|
|
* |
|
98
|
|
|
* @param DateTime $value The value. |
|
99
|
|
|
* @return string Returns the encoded datetime value. |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function encodeDateTime(DateTime $value = null) { |
|
102
|
|
|
return null === $value ? "" : $value->format(self::DATETIME_FORMAT); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Encode an integer value. |
|
107
|
|
|
* |
|
108
|
|
|
* @param integer $value The value. |
|
109
|
|
|
* @param integer $length The length. |
|
110
|
|
|
* @return string Returns the encoded integer. |
|
111
|
|
|
* @throws SkiDataTooLongDataException Throws a too long data exception if the value exceeds the length. |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function encodeInteger($value, $length) { |
|
114
|
|
|
$format = "%'.0" . $length . "d"; |
|
115
|
|
|
$output = null === $value ? "" : sprintf($format, $value); |
|
116
|
|
|
if ($length < strlen($output)) { |
|
117
|
|
|
throw new SkiDataTooLongDataException($value, $length); |
|
118
|
|
|
} |
|
119
|
|
|
return $output; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Encode a string value. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $value The value. |
|
126
|
|
|
* @param integer $length The length. |
|
127
|
|
|
* @return string Returns the encoded string. |
|
128
|
|
|
* @throws SkiDataTooLongDataException Throws a too long data exception if the value exceeds the length. |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function encodeString($value, $length = -1) { |
|
131
|
|
|
if (-1 !== $length && $length < strlen($value)) { |
|
132
|
|
|
throw new SkiDataTooLongDataException($value, $length); |
|
133
|
|
|
} |
|
134
|
|
|
return "\"" . substr($value, 0, (-1 === $length ? strlen($value) : $length)) . "\""; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Get the start record format. |
|
139
|
|
|
* |
|
140
|
|
|
* @return SkiDataStartRecordFormat Returns the start record format. |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getStartRecordFormat() { |
|
143
|
|
|
return $this->startRecordFormat; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Set the start record format. |
|
148
|
|
|
* |
|
149
|
|
|
* @param SkiDataStartRecordFormat $startRecordFormat The start record format. |
|
150
|
|
|
* @return AbstractSkiDataParser Returns the parser. |
|
151
|
|
|
*/ |
|
152
|
|
|
public function setStartRecordFormat(SkiDataStartRecordFormat $startRecordFormat) { |
|
153
|
|
|
$this->startRecordFormat = $startRecordFormat; |
|
154
|
|
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
|