Test Failed
Branch develop (09cda0)
by Samson
03:06
created

Formatter::getYearShort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Andegna\Operations;
4
5
use Andegna\Constants;
6
7
/**
8
 * class DateFormatter.
9
 *
10
 * This class deals with the date time formatting issue
11
 */
12
trait Formatter
13
{
14
    /**
15
     * Accepts the same format as the date() function.
16
     *
17
     * @see date(), \DateTime
18
     *
19
     * @param $format
20
     *
21
     * @return string
22
     */
23
    public function format($format)
24
    {
25
        $result = '';
26
        $length = \mb_strlen($format);
27
28
        // iterate for each character
29
        for ($index = 0; $index < $length; $index++) {
30
            $result .= $this->getValueOfFormatCharacter(mb_substr($format, $index, 1));
31
        }
32
33
        return $result;
34
    }
35
36
    /**
37
     * Return the value of the format character.
38
     *
39
     * @param string $name of the field
40
     *
41
     * @return string
42
     */
43
    protected function getValueOfFormatCharacter($name)
44
    {
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()];
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
     * (1-13) Numeric representation of a month, without leading zeros
111
     *
112
     * @return string
113
     */
114
    public function getMonth()
115
    {
116
        return "{$this->getMonth()}";
117
    }
118
119
    /**
120
     * (01-13) Numeric representation of a month, with leading zeros
121
     *
122
     * @return string
123
     */
124
    public function getMonthTwoDigit()
125
    {
126
        $n = $this->getValueOfFormatCharacter('n');
127
128
        return (strlen($n) == 1) ? "0$n" : "$n";
129
    }
130
131
    /**
132
     * (5,6 or 30) Number of days in the given month
133
     *
134
     * @return string
135
     */
136
    public function getDaysInMonth()
137
    {
138
        return "{$this->getDaysInMonth()}";
139
    }
140
141
    /**
142
     * (1 or 0) Whether it's a leap year
143
     *
144
     * @return string
145
     */
146
    public function getLeapYearString()
147
    {
148
        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...
149
    }
150
151
    /**
152
     * (1000-9999) A full numeric representation of a year, 4 digits mostly
153
     *
154
     * @return string
155
     */
156
    public function getYear()
157
    {
158
        return "{$this->getYear()}";
159
    }
160
161
    public function getYearShort()
162
    {
163
        $Y = $this->getValueOfFormatCharacter('Y');
164
165
        return mb_substr($Y, strlen($Y) - 2, 2);
166
    }
167
168
    /**
169
     * Return the amharic equivalent of AM & PM.
170
     *
171
     * (እኩለ፡ሌሊት-ምሽት)
172
     *
173
     * It suppose to format 'Ante meridiem' and 'Post meridiem'
174
     * But we Ethiopians classify the day in <b>ten</b> parts
175
     * and we don't have Uppercase and Lowercase letters
176
     *
177
     * @link http://web.archive.org/web/20140331152859/http://ethiopic.org/Calendars/
178
     *
179
     * @return string
180
     */
181
    public function getTimeOfDay()
182
    {
183
        switch ($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...
184
            case 23:
185
            case 0:
186
                return 'እኩለ፡ሌሊት';
187
            case 1:
188
            case 2:
189
            case 3:
190
                return 'ውደቀት';
191
            case 4:
192
            case 5:
193
                return 'ንጋት';
194
            case 6:
195
            case 7:
196
            case 8:
197
                return 'ጡዋት';
198
            case 9:
199
            case 10:
200
            case 11:
201
                return 'ረፋድ';
202
            case 12:
203
                return 'እኩለ፡ቀን';
204
            case 13:
205
            case 14:
206
            case 15:
207
                return 'ከሰዓት፡በኋላ';
208
            case 16:
209
            case 17:
210
                return 'ወደማታ';
211
            case 18:
212
            case 19:
213
                return 'ሲደነግዝ';
214
            case 20:
215
            case 21:
216
            case 22:
217
                return 'ምሽት';
218
        }
219
220
        // We shouldn't get to this :(
221
        // Something must been wrong
222
223
        // Lets just keep quite about it ;)
224
        return '';
225
    }
226
227
    public function getOrthodoxDay()
228
    {
229
        return Constants::ORTHODOX_DAY_NAME[$this->getMonth()];
230
    }
231
232
    public function getTextualEra()
233
    {
234
        return $this->getYear() > 0 ? 'ዓ/ም' : 'ዓ/ዓ';
235
    }
236
237
    public function getOrthodoxYear()
238
    {
239
        return Constants::ORTHODOX_YEAR_NAME[$this->getYear() % 4];
240
    }
241
242
}
243