Passed
Branch develop (cdaa67)
by Samson
04:39 queued 02:31
created

Formatter::getYearInGeez()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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 4
    public function format($format)
25
    {
26 4
        $result = '';
27 4
        $length = \mb_strlen($format);
28
29 4
        $skip_next = false;
30
31
        // iterate for each character
32 4
        for ($index = 0; $index < $length; $index++) {
33 4
            $format_char = mb_substr($format, $index, 1);
34
35 4
            $result .= $this->getValueOfFormatCharacter($format_char, $skip_next);
36 4
        }
37
38
        // remove null bits if they exist
39 4
        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
     *
47
     * @param bool $skip
48
     * @return string
49
     */
50 4
    protected function getValueOfFormatCharacter($name, &$skip = false)
51
    {
52 4
        if (($r = $this->shouldWeSkip($name, $skip)) !== false) {
53 1
            return '' . $r;
54
        }
55
56 4
        if ($this->isOverrideFormatCharacter($name)) {
57 4
            return '' . $this->{Constants::FORMAT_MAPPER[$name]}();
58
        }
59
60 3
        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()
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()
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()
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()
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()
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()
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()
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()
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()
167
    {
168
        $array = [
169 4
            'እኩለ፡ሌሊት' => [23, 0],
170 4
            'ውደቀት' => [1, 2, 3],
171 4
            'ንጋት' => [4, 5],
172 4
            'ጡዋት' => [6, 7, 8],
173 4
            'ረፋድ' => [9, 10, 11],
174 4
            'እኩለ፡ቀን' => [12],
175 4
            'ከሰዓት፡በኋላ' => [13, 14, 15],
176 4
            'ወደማታ' => [16, 17],
177 4
            'ሲደነግዝ' => [18, 19],
178 4
            'ምሽት' => [20, 21, 22],
179 4
        ];
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 false !== array_search($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()
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()
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 2
    public function getOrthodoxYear()
216
    {
217 2
        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
     * @return string
224
     */
225 2
    public function getYearInGeez()
226
    {
227 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...
228
    }
229
230
    /**
231
     * Return the day in geez number.
232
     *
233
     * @return string
234
     */
235 2
    public function getDayInGeez()
236
    {
237 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...
238
    }
239
240
    /**
241
     * @param $name
242
     * @return bool
243
     */
244 4
    protected function isOverrideFormatCharacter($name)
245
    {
246 4
        return array_key_exists($name, Constants::FORMAT_MAPPER);
247
    }
248
249
    /**
250
     * @param $name
251
     * @param $skip
252
     *
253
     * @return bool
254
     */
255 4
    protected function shouldWeSkip($name, &$skip)
256
    {
257 4
        if ($this->shouldWeSkip4Real($name, $skip)) {
258 1
            return $this->skipCharacter($name, $skip);
259
        }
260
261 4
        return false;
262
    }
263
264
    /**
265
     * @param $name
266
     * @param $skip
267
     * @return bool
268
     */
269 4
    protected function shouldWeSkip4Real($name, &$skip)
270
    {
271 4
        return $this->isSkipCharacter($name) || $skip;
272
    }
273
274
    /**
275
     * @param $name
276
     *
277
     * @param $skip
278
     * @return string
279
     */
280 1
    protected function skipCharacter($name, &$skip)
281
    {
282 1
        if ($skip) {
283 1
            $skip = false;
284 1
            return $name;
285
        }
286
287 1
        if ($this->isSkipCharacter($name)) {
288 1
            $skip = true;
289 1
        }
290
291 1
        return '';
292
    }
293
294
    /**
295
     * @param $name
296
     * @return bool
297
     */
298 4
    protected function isSkipCharacter($name)
299
    {
300 4
        return $name === "\\";
301
    }
302
303
}
304