AbstractParser::encodeString()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 10
cc 4
nc 2
nop 2
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\ThirdParty\SkiData\Parser;
13
14
use DateTime;
15
use WBW\Library\Core\Argument\Helper\IntegerHelper;
16
use WBW\Library\Core\ThirdParty\SkiData\API\ParserInterface;
17
use WBW\Library\Core\ThirdParty\SkiData\Exception\TooLongDataException;
18
use WBW\Library\Core\ThirdParty\SkiData\Model\StartRecordFormat;
19
20
/**
21
 * Abstract parser.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Library\Core\ThirdParty\SkiData\Parser
25
 * @abstract
26
 */
27
abstract class AbstractParser implements ParserInterface {
28
29
    /**
30
     * Start record format.
31
     *
32
     * @var StartRecordFormat|null
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|null Returns the decoded string in case of success, null otherwise.
48
     */
49
    protected function decodeDate(string $str): ?DateTime {
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|null Returns the decoded string in case of success, null otherwise.
59
     */
60
    protected function decodeDateTime(string $str): ?DateTime {
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 in case of success, "" otherwise.
70
     */
71
    protected function decodeString(string $str): string {
72
        return ("" === $str || 2 === strlen($str)) ? "" : substr($str, 1, strlen($str) - 2);
73
    }
74
75
    /**
76
     * Encode a boolean value.
77
     *
78
     * @param bool|null $value The value.
79
     * @return string Returns the encoded boolean value.
80
     */
81
    protected function encodeBoolean(?bool $value): string {
82
        return "" . IntegerHelper::parseBoolean($value);
83
    }
84
85
    /**
86
     * Encode a date value.
87
     *
88
     * @param DateTime|null $value The value.
89
     * @return string Returns the encoded datetime value.
90
     */
91
    protected function encodeDate(?DateTime $value): string {
92
        return null === $value ? "" : $value->format(self::DATE_FORMAT);
93
    }
94
95
    /**
96
     * Encode a datetime value.
97
     *
98
     * @param DateTime|null $value The value.
99
     * @return string Returns the encoded datetime value.
100
     */
101
    protected function encodeDateTime(?DateTime $value): string {
102
        return null === $value ? "" : $value->format(self::DATETIME_FORMAT);
103
    }
104
105
    /**
106
     * Encode an integer value.
107
     *
108
     * @param int|null $value The value.
109
     * @param int $length The length.
110
     * @return string Returns the encoded integer.
111
     * @throws TooLongDataException Throws a too long data exception if the value exceeds the length.
112
     */
113
    protected function encodeInteger(?int $value, int $length): string {
114
        $format = "%'.0{$length}d";
115
        $output = null === $value ? "" : sprintf($format, $value);
116
        if ($length < strlen($output)) {
117
            throw new TooLongDataException($value, $length);
118
        }
119
        return $output;
120
    }
121
122
    /**
123
     * Encode a string value.
124
     *
125
     * @param string $value The value.
126
     * @param int $length The length.
127
     * @return string Returns the encoded string.
128
     * @throws TooLongDataException Throws a too long data exception if the value exceeds the length.
129
     */
130
    protected function encodeString(string $value, int $length = -1): string {
131
        if (-1 !== $length && $length < strlen($value)) {
132
            throw new TooLongDataException($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 StartRecordFormat|null Returns the start record format.
141
     */
142
    public function getStartRecordFormat(): ?StartRecordFormat {
143
        return $this->startRecordFormat;
144
    }
145
146
    /**
147
     * Set the start record format.
148
     *
149
     * @param StartRecordFormat|null $startRecordFormat The start record format.
150
     * @return AbstractParser Returns the parser.
151
     */
152
    public function setStartRecordFormat(?StartRecordFormat $startRecordFormat): AbstractParser {
153
        $this->startRecordFormat = $startRecordFormat;
154
        return $this;
155
    }
156
}
157