Passed
Branch develop (6275f6)
by Samson
02:24
created

Formatter::getTextualMonth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
    public function format($format)
25
    {
26
        $result = '';
27
        $length = \mb_strlen($format);
28
29
        // iterate for each character
30
        for ($index = 0; $index < $length; $index++) {
31
            $result .= $this->getValueOfFormatCharacter(mb_substr($format, $index, 1));
32
        }
33
34
        return $result;
35
    }
36
37
    /**
38
     * Return the value of the format character.
39
     *
40
     * @param string $name of the field
41
     *
42
     * @return string
43
     */
44
    protected function getValueOfFormatCharacter($name)
45
    {
46
        if (array_key_exists($name, Constants::FORMAT_MAPPER)) {
47
            return '' . $this->{Constants::FORMAT_MAPPER[$name]}();
48
        }
49
50
        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...
51
    }
52
53
    /**
54
     * (01-30) Day of the month, 2 digits with leading zeros.
55
     *
56
     * @return string
57
     */
58
    public function getDayTwoDigit()
59
    {
60
        $day = $this->getValueOfFormatCharacter('j');
61
62
        return strlen($day) === 1 ? "0$day" : $day;
63
    }
64
65
    /**
66
     * (ሰኞ-እሑድ) A full textual representation of the day of the week.
67
     *
68
     * return string
69
     */
70
    public function getTextualDay()
71
    {
72
        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...
73
    }
74
75
    /**
76
     * (ሰኞ-እሑ) A textual representation of a day, two letters.
77
     *
78
     * return string
79
     */
80
    public function getTextualDayShort()
81
    {
82
        $week = $this->getValueOfFormatCharacter('l');
83
84
        return mb_substr($week, 0, 2, 'UTF-8');
85
    }
86
87
    /**
88
     * (መስከረም-ጳጉሜን) A full textual representation of a month.
89
     *
90
     * @return string
91
     */
92
    public function getTextualMonth()
93
    {
94
        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...
95
    }
96
97
    /**
98
     * (መስ - ጳጉ) A short textual representation of a month, two letters.
99
     *
100
     * @return string
101
     */
102
    public function getTextualMonthShort()
103
    {
104
        $F = $this->getValueOfFormatCharacter('F');
105
106
        return mb_substr($F, 0, 2, 'UTF-8');
107
    }
108
109
    /**
110
     * (01-13) Numeric representation of a month, with leading zeros.
111
     *
112
     * @return string
113
     */
114
    public function getMonthTwoDigit()
115
    {
116
        $n = $this->getValueOfFormatCharacter('n');
117
118
        return (strlen($n) == 1) ? "0$n" : "$n";
119
    }
120
121
    /**
122
     * (1 or 0) Whether it's a leap year.
123
     *
124
     * @return string
125
     */
126
    public function getLeapYearString()
127
    {
128
        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...
129
    }
130
131
    /**
132
     * returns 97 for the year 1997.
133
     *
134
     * @return string
135
     */
136
    public function getYearShort()
137
    {
138
        $Y = $this->getValueOfFormatCharacter('Y');
139
140
        return mb_substr($Y, strlen($Y) - 2, 2);
141
    }
142
143
    /**
144
     * Return the amharic equivalent of AM & PM.
145
     *
146
     * (እኩለ፡ሌሊት-ምሽት)
147
     *
148
     * It suppose to format 'Ante meridiem' and 'Post meridiem'
149
     * But we Ethiopians classify the day in <b>ten</b> parts
150
     * and we don't have Uppercase and Lowercase letters
151
     *
152
     * @link http://web.archive.org/web/20140331152859/http://ethiopic.org/Calendars/
153
     *
154
     * @return string
155
     */
156
    public function getTimeOfDay()
157
    {
158
        $array = [
159
            'እኩለ፡ሌሊት' => [23, 0],
160
            'ውደቀት' => [1, 2, 3],
161
            'ንጋት' => [4, 5],
162
            'ጡዋት' => [6, 7, 8],
163
            'ረፋድ' => [9, 10, 11],
164
            'እኩለ፡ቀን' => [12],
165
            'ከሰዓት፡በኋላ' => [13, 14, 15],
166
            'ወደማታ' => [16, 17],
167
            'ሲደነግዝ' => [18, 19],
168
            'ምሽት' => [20, 21, 22],
169
        ];
170
171
        $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...
172
173
        $result = array_filter($array, function ($value) use ($hour) {
174
            return false !== array_search($hour, $value, true);
175
        });
176
177
        return key($result);
178
    }
179
180
    /**
181
     * 1 (for 'ልደታ'), 2 (for አባ፡ጉባ), ... 30 (for ማርቆስ).
182
     *
183
     * @return string the ethiopian orthodox day name
184
     */
185
    public function getOrthodoxDay()
186
    {
187
        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...
188
    }
189
190
    /**
191
     * ዓ/ም or ዓ/ዓ.
192
     *
193
     * @return string
194
     */
195
    public function getTextualEra()
196
    {
197
        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...
198
    }
199
200
    /**
201
     * ማቴዎስ, ማርቆስ, ሉቃስ or ዮሐንስ.
202
     *
203
     * @return string the ethiopian orthodox year name
204
     */
205
    public function getOrthodoxYear()
206
    {
207
        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...
208
    }
209
210
    /**
211
     * Return the year in geez number.
212
     *
213
     * @return string
214
     */
215
    public function getYearInGeez()
216
    {
217
        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...
218
    }
219
220
    /**
221
     * Return the day in geez number.
222
     *
223
     * @return string
224
     */
225
    public function getDayInGeez()
226
    {
227
        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...
228
    }
229
}
230