Completed
Push — master ( bc8fac...f29534 )
by WEBEWEB
01:55
created

AbstractSkiDataParser::setStartRecordFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
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\SkiDataMissingStartRecordFormatException;
17
use WBW\Library\Core\Exception\SkiData\SkiDataTooLongDataException;
18
use WBW\Library\Core\SkiData\API\SkiDataParserInterface;
19
use WBW\Library\Core\SkiData\Entity\SkiDataStartRecordFormat;
20
21
/**
22
 * Abstract SkiData parser.
23
 *
24
 * @author webeweb <https://github.com/webeweb/>
25
 * @package WBW\Library\Core\SkiData\Parser
26
 * @abstract
27
 */
28
abstract class AbstractSkiDataParser implements SkiDataParserInterface {
29
30
    /**
31
     * Start record format.
32
     *
33
     * @var SkiDataStartRecordFormat
34
     */
35
    private $startRecordFormat;
36
37
    /**
38
     * Constructor.
39
     */
40
    protected function __construct() {
41
        // NOTHING TO DO.
42
    }
43
44
    /**
45
     * Decode a date string.
46
     *
47
     * @param string $str The string.
48
     * @return DateTime Returns the decoded string into DateTime.
49
     */
50
    protected function decodeDate($str) {
51
        $date = DateTime::createFromFormat("!" . self::DATE_FORMAT, $str);
52
        if (false === $date) {
53
            return null;
54
        }
55
        return $date;
56
    }
57
58
    /**
59
     * Decode a datetime string.
60
     *
61
     * @param string $str The string.
62
     * @return DateTime Returns the decoded string into DateTime.
63
     */
64
    protected function decodeDateTime($str) {
65
        $date = DateTime::createFromFormat(self::DATETIME_FORMAT, $str);
66
        if (false === $date) {
67
            return null;
68
        }
69
        return $date;
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
    protected function decodeString($str) {
79
        if ("" === $str || 2 === strlen($str)) {
80
            return "";
81
        }
82
        return substr($str, 1, strlen($str) - 2);
83
    }
84
85
    /**
86
     * Encode a boolean value.
87
     *
88
     * @param boolean $value The value.
89
     * @return string Returns the encoded boolean value.
90
     */
91
    protected function encodeBoolean($value) {
92
        return "" . IntegerHelper::parseBoolean($value);
93
    }
94
95
    /**
96
     * Encode a date value.
97
     *
98
     * @param DateTime $value The value.
99
     * @return string Returns the encoded datetime value.
100
     */
101
    protected function encodeDate(DateTime $value = null) {
102
        if (null === $value) {
103
            return "";
104
        }
105
        return $value->format(self::DATE_FORMAT);
106
    }
107
108
    /**
109
     * Encode a datetime value.
110
     *
111
     * @param DateTime $value The value.
112
     * @return string Returns the encoded datetime value.
113
     */
114
    protected function encodeDateTime(DateTime $value = null) {
115
        if (null === $value) {
116
            return "";
117
        }
118
        return $value->format(self::DATETIME_FORMAT);
119
    }
120
121
    /**
122
     * Encode an integer value.
123
     *
124
     * @param integer $value The value.
125
     * @param integer $length The length.
126
     * @return string Returns the encoded integer.
127
     * @throws SkiDataTooLongDataException Throws a too long data exception if the value exceeds the length.
128
     */
129
    protected function encodeInteger($value, $length) {
130
        if (null === $value) {
131
            return "";
132
        }
133
        $format = "%'.0" . $length . "d";
134
        $output = sprintf($format, $value);
135
        if ($length < strlen($output)) {
136
            throw new SkiDataTooLongDataException($value, $length);
137
        }
138
        return $output;
139
    }
140
141
    /**
142
     * Encode a string value.
143
     *
144
     * @param string $value The value.
145
     * @param integer $length The length.
146
     * @return string Returns the encoded string.
147
     * @throws SkiDataTooLongDataException Throws a too long data exception if the value exceeds the length.
148
     */
149
    protected function encodeString($value, $length = -1) {
150
        if (-1 !== $length && $length < strlen($value)) {
151
            throw new SkiDataTooLongDataException($value, $length);
152
        }
153
        return "\"" . substr($value, 0, (-1 === $length ? strlen($value) : $length)) . "\"";
154
    }
155
156
    /**
157
     * Get the start record format.
158
     *
159
     * @return SkiDataStartRecordFormat Returns the start record format.
160
     */
161
    public function getStartRecordFormat() {
162
        return $this->startRecordFormat;
163
    }
164
165
    /**
166
     * Determines if the version of record structure is less or equal than $versionRecordStructure.
167
     *
168
     * @param integer $versionRecordStructure The version of record structure.
169
     * @return boolean Returns true in case of success, false otherwise.
170
     * @throws SkiDataMissingStartRecordFormatException Throws a missing start record format exception if the start record format is missing.
171
     */
172
    protected function isVersionRecordStructure($versionRecordStructure) {
173
        if (null === $this->startRecordFormat) {
174
            throw new SkiDataMissingStartRecordFormatException();
175
        }
176
        return $versionRecordStructure <= $this->startRecordFormat->getVersionRecordStructure();
177
    }
178
179
    /**
180
     * Set the start record format.
181
     *
182
     * @param SkiDataStartRecordFormat $startRecordFormat The start record format.
183
     * @return AbstractSkiDataParser Returns the parser.
184
     */
185
    public function setStartRecordFormat(SkiDataStartRecordFormat $startRecordFormat) {
186
        $this->startRecordFormat = $startRecordFormat;
187
        return $this;
188
    }
189
190
}
191