Passed
Branch main (d45280)
by PRATIK
06:37 queued 03:34
created

NepaliDate::formattedNepaliMonth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Pratiksh\Nepalidate\Services;
4
5
use Carbon\Carbon;
6
7
class NepaliDate
8
{
9
    public $year;
10
    public $month;
11
    public $day;
12
13
    private $calendarData = [
14
        0 => [2000, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
15
        1 => [2001, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
16
        2 => [2002, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
17
        3 => [2003, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
18
        4 => [2004, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
19
        5 => [2005, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
20
        6 => [2006, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
21
        7 => [2007, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
22
        8 => [2008, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31],
23
        9 => [2009, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
24
        10 => [2010, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
25
        11 => [2011, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
26
        12 => [2012, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30],
27
        13 => [2013, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
28
        14 => [2014, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
29
        15 => [2015, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
30
        16 => [2016, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30],
31
        17 => [2017, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
32
        18 => [2018, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30],
33
        19 => [2019, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
34
        20 => [2020, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
35
        21 => [2021, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
36
        22 => [2022, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
37
        23 => [2023, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
38
        24 => [2024, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
39
        25 => [2025, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
40
        26 => [2026, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
41
        27 => [2027, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
42
        28 => [2028, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
43
        29 => [2029, 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30],
44
        30 => [2030, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
45
        31 => [2031, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
46
        32 => [2032, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
47
        33 => [2033, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
48
        34 => [2034, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
49
        35 => [2035, 30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31],
50
        36 => [2036, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
51
        37 => [2037, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
52
        38 => [2038, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
53
        39 => [2039, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30],
54
        40 => [2040, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
55
        41 => [2041, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
56
        42 => [2042, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
57
        43 => [2043, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30],
58
        44 => [2044, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
59
        45 => [2045, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30],
60
        46 => [2046, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
61
        47 => [2047, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
62
        48 => [2048, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
63
        49 => [2049, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
64
        50 => [2050, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
65
        51 => [2051, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
66
        52 => [2052, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
67
        53 => [2053, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
68
        54 => [2054, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
69
        55 => [2055, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
70
        56 => [2056, 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30],
71
        57 => [2057, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
72
        58 => [2058, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
73
        59 => [2059, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
74
        60 => [2060, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
75
        61 => [2061, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
76
        62 => [2062, 30, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31],
77
        63 => [2063, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
78
        64 => [2064, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
79
        65 => [2065, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
80
        66 => [2066, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31],
81
        67 => [2067, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
82
        68 => [2068, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
83
        69 => [2069, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
84
        70 => [2070, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30],
85
        71 => [2071, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
86
        72 => [2072, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30],
87
        73 => [2073, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
88
        74 => [2074, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
89
        75 => [2075, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
90
        76 => [2076, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
91
        77 => [2077, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
92
        78 => [2078, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
93
        79 => [2079, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
94
        80 => [2080, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
95
        81 => [2081, 31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30],
96
        82 => [2082, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30],
97
        83 => [2083, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30],
98
        84 => [2084, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30],
99
        85 => [2085, 31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30],
100
        86 => [2086, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30],
101
        87 => [2087, 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30],
102
        88 => [2088, 30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30],
103
        89 => [2089, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30],
104
        90 => [2090, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30],
105
    ];
106
107
    private $monthsInBS = [
108
        1 => 'Baisakh',
109
        2 => 'Jestha',
110
        3 => 'Ashar',
111
        4 => 'Shrawan',
112
        5 => 'Bhadra',
113
        6 => 'Ashoj',
114
        7 => 'Kartik',
115
        8 => 'Manghir',
116
        9 => 'Poush',
117
        10 => 'Magh',
118
        11 => 'Falgun',
119
        12 => 'Chaitra',
120
    ];
121
122
    private $dayOfWeekInBS = [
123
        1 => 'Sunday',
124
        2 => 'Monday',
125
        3 => 'Tuesday',
126
        4 => 'Wednesday',
127
        5 => 'Thurday',
128
        6 => 'Friday',
129
        7 => 'Saturrday',
130
    ];
131
132
    private $monthsInNepali = [
133
        1 => 'वैशाख',
134
        2 => 'जेठ',
135
        3 => 'असार',
136
        4 => 'साउन',
137
        5 => 'भदौ',
138
        6 => 'असोज',
139
        7 => 'कार्तिक',
140
        8 => 'मंसिर',
141
        9 => 'पुष',
142
        10 => 'माघ',
143
        11 => 'फागुन',
144
        12 => 'चैत',
145
    ];
146
147
    private $dayOfWeekInNepali = [
148
        1 => 'आइतवार',
149
        2 => 'सोमवार',
150
        3 => 'मङ्गलवार',
151
        4 => 'बुधवार',
152
        5 => 'बिहिवार',
153
        6 => 'शुक्रवार',
154
        7 => 'शनिवार',
155
    ];
156
157
    private $numbersInNepali = [
158
        0 => '०',
159
        1 => '१',
160
        2 => '२',
161
        3 => '३',
162
        4 => '४',
163
        5 => '५',
164
        6 => '६',
165
        7 => '७',
166
        8 => '८',
167
        9 => '९',
168
    ];
169
170
    private $normalMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
171
    private $leapMonths = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
172
173
    /*These are initial values for nepali date.*/
174
    public $nepaliYear = 2000;
175
    public $nepaliMonth = 9;
176
    public $nepaliDay = 16; // 17 - 1
177
    public $dayOfWeek = 6; // 7 - 1, 0 for sunday, 6 for saturday
178
    /*These are initial values for nepali date.*/
179
180
    public function create(Carbon $date)
181
    {
182
        $this->year = $date->year;
183
        $this->month = $date->month;
184
        $this->day = $date->day;
185
186
        $this->setNepaliDate($date->year, $date->month, $date->day);
187
188
        return $this;
189
    }
190
191
    public function toBS()
192
    {
193
        return $this->nepaliYear . '-' . $this->nepaliMonth . '-' . $this->nepaliDay;
194
    }
195
196
    public function toFormattedBSDate()
197
    {
198
        return $this->nepaliDay . ' ' .
199
            $this->formattedBSMonth($this->nepaliMonth) . ' ' .
200
            $this->nepaliYear . ',' .
201
            ' ' .
202
            $this->formattedBSDateOfWeek($this->dayOfWeek);
203
    }
204
205
    public function toFormattedNepaliDate()
206
    {
207
        return $this->formattedNepaliNumber($this->nepaliDay) . ' ' .
208
            $this->formattedNepaliMonth($this->nepaliMonth) . ' ' .
209
            $this->formattedNepaliNumber($this->nepaliYear) . ',' .
210
            ' ' .
211
            $this->formattedNepaliDateOfWeek($this->dayOfWeek);
212
    }
213
214
    public function toBSArray()
215
    {
216
        return [
217
            'year' => $this->nepaliYear,
218
            'month' => $this->nepaliMonth,
219
            'day' => $this->nepaliDay,
220
            'day_of_week' => $this->dayOfWeek,
221
        ];
222
    }
223
224
    public function toDetailBS()
225
    {
226
        $bs_array = $this->toBSArray();
227
        $to_detail_bs = null;
228
        if (!is_null($bs_array)) {
0 ignored issues
show
introduced by
The condition is_null($bs_array) is always false.
Loading history...
229
            if (is_array($bs_array)) {
0 ignored issues
show
introduced by
The condition is_array($bs_array) is always true.
Loading history...
230
                if (count($bs_array) > 0) {
231
                    $to_detail_bs = json_decode(json_encode($bs_array));
232
                }
233
            }
234
        }
235
        return $to_detail_bs;
236
    }
237
238
    public function toFormattedBSDateArray()
239
    {
240
        $bsDateArray = $this->toBSArray();
0 ignored issues
show
Unused Code introduced by
The assignment to $bsDateArray is dead and can be removed.
Loading history...
241
242
        return [
243
            'year' => $this->formattedNepaliNumber($this->nepaliYear),
244
            'month' => $this->formattedNepaliMonth($this->nepaliMonth),
245
            'day' => $this->formattedNepaliNumber($this->nepaliDay),
246
            'day_of_week' => $this->formattedNepaliDateOfWeek($this->dayOfWeek),
247
        ];
248
    }
249
250
    public function isLeapYear($year)
251
    {
252
        if ($year % 100 == 0) {
253
            if ($year % 400 == 0) {
254
                return true;
255
            } else {
256
                return false;
257
            }
258
        } else {
259
            if ($year % 4 == 0) {
260
                return true;
261
            } else {
262
                return false;
263
            }
264
        }
265
    }
266
267
    public function calculateTotalEnglishDays($year, $month, $day)
268
    {
269
        $totalEngilishDays = 0;
270
271
        for ($i = 0; $i < ($year - 1944); $i++) {
272
            if ($this->isLeapYear(1944 + $i)) {
273
                for ($j = 0; $j < 12; $j++) {
274
                    $totalEngilishDays += $this->leapMonths[$j];
275
                }
276
            } else {
277
                for ($j = 0; $j < 12; $j++) {
278
                    $totalEngilishDays += $this->normalMonths[$j];
279
                }
280
            }
281
        }
282
283
        for ($i = 0; $i < ($month - 1); $i++) {
284
            if ($this->isLeapYear($year)) {
285
                $totalEngilishDays += $this->leapMonths[$i];
286
            } else {
287
                $totalEngilishDays += $this->normalMonths[$i];
288
            }
289
        }
290
291
        $totalEngilishDays += $day;
292
293
        return $totalEngilishDays;
294
    }
295
296
    public function performCalculationbasedOn($totalEngilishDays)
297
    {
298
        $i = 0;
299
        $j = $this->nepaliMonth;
300
301
        $nepaliYear = 2000;
302
        $nepaliMonth = 9;
303
304
        $nepaliDay = 16;
305
        $dayOfWeek = 6;
306
307
        while ($totalEngilishDays != 0) {
308
            $lastDayOfMonth = $this->calendarData[$i][$j];
309
310
            $nepaliDay++;
311
            $dayOfWeek++;
312
313
            if ($nepaliDay > $lastDayOfMonth) {
314
                $nepaliMonth++;
315
                $nepaliDay = 1;
316
                $j++;
317
            }
318
319
            if ($dayOfWeek > 7) {
320
                $dayOfWeek = 1;
321
            }
322
323
            if ($nepaliMonth > 12) {
324
                $nepaliYear++;
325
                $nepaliMonth = 1;
326
            }
327
328
            if ($j > 12) {
329
                $j = 1;
330
                $i++;
331
            }
332
333
            $this->nepaliYear = $nepaliYear;
334
            $this->nepaliMonth = $nepaliMonth;
335
            $this->nepaliDay = $nepaliDay;
336
            $this->dayOfWeek = $dayOfWeek;
337
338
            $totalEngilishDays--;
339
        }
340
    }
341
342
    public function formattedBSMonth($month)
343
    {
344
        return $this->monthsInBS[$month];
345
    }
346
347
    public function formattedBSDateOfWeek($dayOfWeek)
348
    {
349
        return $this->dayOfWeekInBS[$dayOfWeek];
350
    }
351
352
    public function formattedNepaliMonth($month)
353
    {
354
        return $this->monthsInNepali[$month];
355
    }
356
357
    public function formattedNepaliDateOfWeek($dayOfWeek)
358
    {
359
        return $this->dayOfWeekInNepali[$dayOfWeek];
360
    }
361
362
    public function formattedNepaliNumber($value)
363
    {
364
        $numbers = str_split($value);
365
366
        foreach ($numbers as $key => $number) {
367
            $numbers[$key] = $this->numbersInNepali[$number];
368
        }
369
370
        return implode('', $numbers);
0 ignored issues
show
Bug introduced by
It seems like $numbers can also be of type true; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

370
        return implode('', /** @scrutinizer ignore-type */ $numbers);
Loading history...
371
    }
372
373
    private function setNepaliDate($year, $month, $day)
374
    {
375
        $totalEngilishDays = $this->calculateTotalEnglishDays(
376
            $year,
377
            $month,
378
            $day
379
        );
380
381
        $this->performCalculationbasedOn($totalEngilishDays);
382
    }
383
}
384