Passed
Branch master (d7957a)
by Samson
04:01
created

Formatter::getTimeZoneString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Andegna\Operations;
4
5
use Andegna\Constants;
6
use Geezify\Geezify;
7
8
/**
9
 * class DateFormatter.
10
 *
11
 * This class deals with the date time formatting issue
12
 */
13
trait Formatter
14
{
15
    /**
16
     * Accepts the same format as the date() function.
17
     *
18
     * @see date(), \DateTime
19
     *
20
     * @param $format
21
     *
22
     * @return string
23
     */
24 5
    public function format(string $format)
25
    {
26 5
        $result = '';
27 5
        $length = \mb_strlen($format);
28
29 5
        $skip_next = false;
30
31
        // iterate for each character
32 5
        for ($index = 0; $index < $length; $index++) {
33 5
            $format_char = mb_substr($format, $index, 1);
34
35 5
            $result .= $this->getValueOfFormatCharacter($format_char, $skip_next);
36
        }
37
38
        // remove null bits if they exist
39 5
        return str_replace("\0", '', $result);
40
    }
41
42
    /**
43
     * Return the value of the format character.
44
     *
45
     * @param string $name of the field
46
     * @param bool   $skip
47
     *
48
     * @return string
49
     */
50 5
    protected function getValueOfFormatCharacter(string $name, bool &$skip = false): string
51
    {
52 5
        if (($r = $this->shouldWeSkip($name, $skip)) !== false) {
53 1
            return ''.$r;
54
        }
55
56 5
        if ($this->isOverrideFormatCharacter($name)) {
57 5
            return ''.$this->{Constants::FORMAT_MAPPER[$name]}();
58
        }
59
60 4
        return $this->dateTime->format($name);
0 ignored issues
show
Bug introduced by
The property dateTime does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
61
    }
62
63
    /**
64
     * (01-30) Day of the month, 2 digits with leading zeros.
65
     *
66
     * @return string
67
     */
68 4
    public function getDayTwoDigit(): string
69
    {
70 4
        $day = $this->getValueOfFormatCharacter('j');
71
72 4
        return strlen($day) === 1 ? "0$day" : $day;
73
    }
74
75
    /**
76
     * (ሰኞ-እሑድ) A full textual representation of the day of the week.
77
     *
78
     * return string
79
     */
80 4
    public function getTextualDay(): string
81
    {
82 4
        return Constants::WEEK_NAME[$this->getDayOfWeek()];
0 ignored issues
show
Bug introduced by
It seems like getDayOfWeek() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
83
    }
84
85
    /**
86
     * (ሰኞ-እሑ) A textual representation of a day, two letters.
87
     *
88
     * return string
89
     */
90 2
    public function getTextualDayShort(): string
91
    {
92 2
        $week = $this->getValueOfFormatCharacter('l');
93
94 2
        return mb_substr($week, 0, 2, 'UTF-8');
95
    }
96
97
    /**
98
     * (መስከረም-ጳጉሜን) A full textual representation of a month.
99
     *
100
     * @return string
101
     */
102 4
    public function getTextualMonth(): string
103
    {
104 4
        return Constants::MONTHS_NAME[$this->getMonth()];
0 ignored issues
show
Bug introduced by
The method getMonth() does not exist on Andegna\Operations\Formatter. Did you maybe mean getMonthTwoDigit()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
105
    }
106
107
    /**
108
     * (መስ - ጳጉ) A short textual representation of a month, two letters.
109
     *
110
     * @return string
111
     */
112 3
    public function getTextualMonthShort(): string
113
    {
114 3
        $F = $this->getValueOfFormatCharacter('F');
115
116 3
        return mb_substr($F, 0, 2, 'UTF-8');
117
    }
118
119
    /**
120
     * (01-13) Numeric representation of a month, with leading zeros.
121
     *
122
     * @return string
123
     */
124 2
    public function getMonthTwoDigit(): string
125
    {
126 2
        $n = $this->getValueOfFormatCharacter('n');
127
128 2
        return (strlen($n) === 1) ? "0$n" : "$n";
129
    }
130
131
    /**
132
     * (1 or 0) Whether it's a leap year.
133
     *
134
     * @return string
135
     */
136 1
    public function getLeapYearString(): string
137
    {
138 1
        return $this->isLeapYear() ? '1' : '0';
0 ignored issues
show
Bug introduced by
It seems like isLeapYear() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
139
    }
140
141
    /**
142
     * returns 97 for the year 1997.
143
     *
144
     * @return string
145
     */
146 1
    public function getYearShort(): string
147
    {
148 1
        $Y = $this->getValueOfFormatCharacter('Y');
149
150 1
        return mb_substr($Y, strlen($Y) - 2, 2);
151
    }
152
153
    /**
154
     * Return the amharic equivalent of AM & PM.
155
     *
156
     * (እኩለ፡ሌሊት-ምሽት)
157
     *
158
     * It suppose to format 'Ante meridiem' and 'Post meridiem'
159
     * But we Ethiopians classify the day in <b>ten</b> parts
160
     * and we don't have Uppercase and Lowercase letters
161
     *
162
     * @link http://web.archive.org/web/20140331152859/http://ethiopic.org/Calendars/
163
     *
164
     * @return string
165
     */
166 4
    public function getTimeOfDay(): string
167
    {
168
        $array = [
169 4
            'እኩለ፡ሌሊት'  => [23, 0],
170
            'ውደቀት'     => [1, 2, 3],
171
            'ንጋት'      => [4, 5],
172
            'ጡዋት'      => [6, 7, 8],
173
            'ረፋድ'      => [9, 10, 11],
174
            'እኩለ፡ቀን'   => [12],
175
            'ከሰዓት፡በኋላ' => [13, 14, 15],
176
            'ወደማታ'     => [16, 17],
177
            'ሲደነግዝ'    => [18, 19],
178
            'ምሽት'      => [20, 21, 22],
179
        ];
180
181 4
        $hour = $this->getHour();
0 ignored issues
show
Bug introduced by
It seems like getHour() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
182
183 4
        $result = array_filter($array, function ($value) use ($hour) {
184 4
            return in_array($hour, $value, true);
185 4
        });
186
187 4
        return key($result);
188
    }
189
190
    /**
191
     * 1 (for 'ልደታ'), 2 (for አባ፡ጉባ), ... 30 (for ማርቆስ).
192
     *
193
     * @return string the ethiopian orthodox day name
194
     */
195 2
    public function getOrthodoxDay(): string
196
    {
197 2
        return Constants::ORTHODOX_DAY_NAME[$this->getDay()];
0 ignored issues
show
Bug introduced by
The method getDay() does not exist on Andegna\Operations\Formatter. Did you maybe mean getDayTwoDigit()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
198
    }
199
200
    /**
201
     * ዓ/ም or ዓ/ዓ.
202
     *
203
     * @return string
204
     */
205 3
    public function getTextualEra(): string
206
    {
207 3
        return $this->getYear() > 0 ? 'ዓ/ም' : 'ዓ/ዓ';
0 ignored issues
show
Bug introduced by
The method getYear() does not exist on Andegna\Operations\Formatter. Did you maybe mean getYearShort()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
208
    }
209
210
    /**
211
     * ማቴዎስ, ማርቆስ, ሉቃስ or ዮሐንስ.
212
     *
213
     * @return string the ethiopian orthodox year name
214
     */
215 3
    public function getOrthodoxYear(): string
216
    {
217 3
        return Constants::ORTHODOX_YEAR_NAME[$this->getYear() % 4];
0 ignored issues
show
Bug introduced by
The method getYear() does not exist on Andegna\Operations\Formatter. Did you maybe mean getYearShort()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
218
    }
219
220
    /**
221
     * Return the year in geez number.
222
     *
223
     * @throws \Geezify\Exception\NotAnIntegerArgumentException
224
     *
225
     * @return string
226
     */
227 2
    public function getYearInGeez(): string
228
    {
229 2
        return Geezify::create()->toGeez($this->getYear());
0 ignored issues
show
Bug introduced by
The method getYear() does not exist on Andegna\Operations\Formatter. Did you maybe mean getYearShort()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
230
    }
231
232
    /**
233
     * Return the day in geez number.
234
     *
235
     * @return string
236
     */
237 2
    public function getDayInGeez(): string
238
    {
239 2
        return Geezify::create()->toGeez($this->getDay());
0 ignored issues
show
Bug introduced by
The method getDay() does not exist on Andegna\Operations\Formatter. Did you maybe mean getDayTwoDigit()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
240
    }
241
242
    /**
243
     * Return an empty string.
244
     *
245
     * @return string
246
     */
247 4
    public function getTimeZoneString(): string
248
    {
249 4
        $name = $this->getTimezone()->getName();
0 ignored issues
show
Bug introduced by
The method getTimezone() does not exist on Andegna\Operations\Formatter. Did you maybe mean getTimeZoneString()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
250
251 4
        return Constants::TIME_ZONES[$name] ?? sprintf('ጂ ኤም ቲ%s', $name);
252
    }
253
254
    /**
255
     * Return `ኛው` rather than st,nd,rd, and th.
256
     *
257
     * @return string
258
     */
259 2
    public function getOrdinalSuffix(): string
260
    {
261 2
        return 'ኛው';
262
    }
263
264
    /**
265
     * RFC 2822 formatted date.
266
     *
267
     * @return string
268
     */
269 1
    public function getFormattedDate(): string
270
    {
271 1
        return $this->format(DATE_RFC2822);
272
    }
273
274
    /**
275
     * @param string $name
276
     *
277
     * @return bool
278
     */
279 5
    protected function isOverrideFormatCharacter($name): bool
280
    {
281 5
        return array_key_exists($name, Constants::FORMAT_MAPPER);
282
    }
283
284
    /**
285
     * @param string $name
286
     * @param bool   $skip
287
     *
288
     * @return string|false
289
     */
290 5
    protected function shouldWeSkip($name, &$skip)
291
    {
292 5
        if ($this->shouldWeSkip4Real($name, $skip)) {
293 1
            return $this->skipCharacter($name, $skip);
294
        }
295
296 5
        return false;
297
    }
298
299
    /**
300
     * @param $name
301
     * @param $skip
302
     *
303
     * @return bool
304
     */
305 5
    protected function shouldWeSkip4Real($name, &$skip): bool
306
    {
307 5
        return $this->isSkipCharacter($name) || $skip;
308
    }
309
310
    /**
311
     * @param $name
312
     * @param $skip
313
     *
314
     * @return string
315
     */
316 1
    protected function skipCharacter($name, &$skip): string
317
    {
318 1
        if ($skip) {
319 1
            $skip = false;
320
321 1
            return $name;
322
        }
323
324 1
        if ($this->isSkipCharacter($name)) {
325 1
            $skip = true;
326
        }
327
328 1
        return '';
329
    }
330
331
    /**
332
     * @param $name
333
     *
334
     * @return bool
335
     */
336 5
    protected function isSkipCharacter($name): bool
337
    {
338 5
        return $name === '\\';
339
    }
340
}
341