Passed
Push — main ( d5b4d9...0df0a9 )
by PRATIK
04:50 queued 01:52
created

DateConverter::calculateTotalEnglishDays()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 16
c 1
b 0
f 0
nc 15
nop 3
dl 0
loc 28
rs 8.8333
1
<?php
2
3
namespace Pratiksh\Nepalidate\Services;
4
5
abstract class DateConverter
6
{
7
    public $year;
8
    public $month;
9
    public $day;
10
11
    protected $calendarData = [
12
        0 => [2000, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
13
        1 => [2001, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
14
        2 => [2002, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
15
        3 => [2003, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
16
        4 => [2004, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
17
        5 => [2005, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
18
        6 => [2006, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
19
        7 => [2007, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
20
        8 => [2008, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31],
21
        9 => [2009, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
22
        10 => [2010, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
23
        11 => [2011, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
24
        12 => [2012, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30],
25
        13 => [2013, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
26
        14 => [2014, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
27
        15 => [2015, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
28
        16 => [2016, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30],
29
        17 => [2017, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
30
        18 => [2018, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30],
31
        19 => [2019, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
32
        20 => [2020, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
33
        21 => [2021, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
34
        22 => [2022, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
35
        23 => [2023, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
36
        24 => [2024, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
37
        25 => [2025, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
38
        26 => [2026, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
39
        27 => [2027, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
40
        28 => [2028, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
41
        29 => [2029, 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30],
42
        30 => [2030, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
43
        31 => [2031, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
44
        32 => [2032, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
45
        33 => [2033, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
46
        34 => [2034, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
47
        35 => [2035, 30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31],
48
        36 => [2036, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
49
        37 => [2037, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
50
        38 => [2038, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
51
        39 => [2039, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30],
52
        40 => [2040, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
53
        41 => [2041, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
54
        42 => [2042, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
55
        43 => [2043, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30],
56
        44 => [2044, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
57
        45 => [2045, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30],
58
        46 => [2046, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
59
        47 => [2047, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
60
        48 => [2048, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
61
        49 => [2049, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
62
        50 => [2050, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
63
        51 => [2051, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
64
        52 => [2052, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
65
        53 => [2053, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
66
        54 => [2054, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
67
        55 => [2055, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
68
        56 => [2056, 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30],
69
        57 => [2057, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
70
        58 => [2058, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
71
        59 => [2059, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
72
        60 => [2060, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
73
        61 => [2061, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
74
        62 => [2062, 30, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31],
75
        63 => [2063, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
76
        64 => [2064, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
77
        65 => [2065, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
78
        66 => [2066, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31],
79
        67 => [2067, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
80
        68 => [2068, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
81
        69 => [2069, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
82
        70 => [2070, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30],
83
        71 => [2071, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
84
        72 => [2072, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30],
85
        73 => [2073, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
86
        74 => [2074, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
87
        75 => [2075, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
88
        76 => [2076, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
89
        77 => [2077, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
90
        78 => [2078, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
91
        79 => [2079, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
92
        80 => [2080, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
93
        81 => [2081, 31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 29, 31],
94
        82 => [2082, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
95
        83 => [2083, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30],
96
        84 => [2084, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30],
97
        85 => [2085, 31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30],
98
        86 => [2086, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30],
99
        87 => [2087, 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30],
100
        88 => [2088, 30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30],
101
        89 => [2089, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30],
102
        90 => [2090, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30],
103
    ];
104
105
    private $bsMonthInEnglish = [
106
        1 => 'Baisakh',
107
        2 => 'Jestha',
108
        3 => 'Ashar',
109
        4 => 'Shrawan',
110
        5 => 'Bhadra',
111
        6 => 'Ashoj',
112
        7 => 'Kartik',
113
        8 => 'Mangsir',
114
        9 => 'Poush',
115
        10 => 'Magh',
116
        11 => 'Falgun',
117
        12 => 'Chaitra',
118
    ];
119
120
    private $dayOfWeekInEnglish = [
121
        1 => 'Sunday',
122
        2 => 'Monday',
123
        3 => 'Tuesday',
124
        4 => 'Wednesday',
125
        5 => 'Thursday',
126
        6 => 'Friday',
127
        7 => 'Saturday',
128
    ];
129
130
    private $bsMonthInNepali = [
131
        1 => 'वैशाख',
132
        2 => 'जेठ',
133
        3 => 'असार',
134
        4 => 'साउन',
135
        5 => 'भदौ',
136
        6 => 'असोज',
137
        7 => 'कार्तिक',
138
        8 => 'मंसिर',
139
        9 => 'पुष',
140
        10 => 'माघ',
141
        11 => 'फागुन',
142
        12 => 'चैत',
143
    ];
144
145
    private $adMonthInNepali = [
146
        1 => 'जनवरी',
147
        2 => 'फेब्रुवरी',
148
        3 => 'मार्च',
149
        4 => 'अप्रिल',
150
        5 => 'मई',
151
        6 => 'जून',
152
        7 => 'जुलाई',
153
        8 => 'अगष्ट',
154
        9 => 'सेप्टेम्बर',
155
        10 => 'अक्टोबर',
156
        11 => 'नवेम्बर',
157
        12 => 'दिसेम्बर',
158
    ];
159
160
    private $dayOfWeekInNepali = [
161
        1 => 'आइतवार',
162
        2 => 'सोमवार',
163
        3 => 'मङ्गलवार',
164
        4 => 'बुधवार',
165
        5 => 'बिहिवार',
166
        6 => 'शुक्रवार',
167
        7 => 'शनिवार',
168
    ];
169
170
    private $numbersInNepali = [
171
        0 => '०',
172
        1 => '१',
173
        2 => '२',
174
        3 => '३',
175
        4 => '४',
176
        5 => '५',
177
        6 => '६',
178
        7 => '७',
179
        8 => '८',
180
        9 => '९',
181
    ];
182
183
    protected $normalMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
184
    protected $leapMonths = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
185
186
    protected $dayOfWeek = 6; // 7 - 1, 0 for sunday, 6 for saturday
187
188
    public function getBSMonthInEnglish($month): string
189
    {
190
        return $this->bsMonthInEnglish[$month];
191
    }
192
193
    public function getBSMonthInNepali($month): string
194
    {
195
        return $this->bsMonthInNepali[$month];
196
    }
197
198
    public function getADMonthInNepali($month): string
199
    {
200
        return $this->adMonthInNepali[$month];
201
    }
202
203
    public function getDayOfWeekInNepali($day): string
204
    {
205
        return $this->dayOfWeekInNepali[$day];
206
    }
207
208
    public function getDayOfWeekInEnglish($day): string
209
    {
210
        return $this->dayOfWeekInEnglish[$day];
211
    }
212
213
    public function getNumbersInNepali($day): string
214
    {
215
        return $this->numbersInNepali[$day];
216
    }
217
218
    public function formattedNepaliNumber($value)
219
    {
220
        $numbers = str_split($value);
221
222
        foreach ($numbers as $key => $number) {
223
            $numbers[$key] = $this->numbersInNepali[$number];
224
        }
225
226
        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

226
        return implode('', /** @scrutinizer ignore-type */ $numbers);
Loading history...
227
    }
228
229
    /**
230
     * Check if date range is in english.
231
     *
232
     * @param  int  $yy
233
     * @param  int  $mm
234
     * @param  int  $dd
235
     * @return bool
236
     */
237
    protected function is_in_range_eng($yy, $mm, $dd)
238
    {
239
        if ($yy < 1944 || $yy > 2033) {
240
            throw new \Exception('Supported only between 1944-2022');
241
        }
242
243
        if ($mm < 1 || $mm > 12) {
244
            throw new \Exception('Error! month value can be between 1-12 only');
245
        }
246
247
        if ($dd < 1 || $dd > 31) {
248
            throw new \Exception('Error! day value can be between 1-31 only');
249
        }
250
    }
251
252
    /**
253
     * Check if date is with in nepali data range.
254
     *
255
     * @param  int  $yy
256
     * @param  int  $mm
257
     * @param  int  $dd
258
     * @return bool
259
     */
260
    protected function is_in_range_nep($yy, $mm, $dd)
261
    {
262
        if ($yy < 2000 || $yy > 2089) {
263
            return 'Supported only between 2000-2089';
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'Supported only between 2000-2089' returns the type string which is incompatible with the documented return type boolean.
Loading history...
264
        }
265
266
        if ($mm < 1 || $mm > 12) {
267
            return 'Error! month value can be between 1-12 only';
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'Error! month val...n be between 1-12 only' returns the type string which is incompatible with the documented return type boolean.
Loading history...
268
        }
269
270
        if ($dd < 1 || $dd > 32) {
271
            return 'Error! day value can be between 1-31 only';
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'Error! day value...n be between 1-31 only' returns the type string which is incompatible with the documented return type boolean.
Loading history...
272
        }
273
274
        return true;
275
    }
276
277
    /**
278
     * Calculates wheather english year is leap year or not.
279
     *
280
     * @param  int  $year
281
     * @return bool
282
     */
283
    protected function is_leap_year($year)
284
    {
285
        $a = $year;
286
        if ($a % 100 == 0) {
287
            if ($a % 400 == 0) {
288
                return true;
289
            } else {
290
                return false;
291
            }
292
        } else {
293
            if ($a % 4 == 0) {
294
                return true;
295
            } else {
296
                return false;
297
            }
298
        }
299
    }
300
301
    protected function calculateTotalEnglishDays($year, $month, $day): int
302
    {
303
        $total_ad_days = 0;
304
        $initial_english_year = 1944;
305
306
        for ($i = 0; $i < ($year - $initial_english_year); $i++) {
307
            if ($this->is_leap_year($initial_english_year + $i)) {
308
                for ($j = 0; $j < 12; $j++) {
309
                    $total_ad_days += $this->leapMonths[$j];
310
                }
311
            } else {
312
                for ($j = 0; $j < 12; $j++) {
313
                    $total_ad_days += $this->normalMonths[$j];
314
                }
315
            }
316
        }
317
318
        for ($i = 0; $i < ($month - 1); $i++) {
319
            if ($this->is_leap_year($year)) {
320
                $total_ad_days += $this->leapMonths[$i];
321
            } else {
322
                $total_ad_days += $this->normalMonths[$i];
323
            }
324
        }
325
326
        $total_ad_days += $day;
327
328
        return $total_ad_days;
329
    }
330
331
    /**
332
     * Calculate total days from a reference Nepali date to the specified Nepali date.
333
     *
334
     * @param  int  $year  Nepali year
335
     * @param  int  $month  Nepali month (1-12)
336
     * @param  int  $day  Nepali day
337
     * @return int Total number of days
338
     */
339
    protected function calculateTotalNepaliDays($year, $month, $day): int
340
    {
341
        $total_bs_days = 0;
342
        $initial_nepali_year = 2000;
343
344
        // Add days for complete years
345
        for ($i = $initial_nepali_year; $i < $year; $i++) {
346
            for ($j = 1; $j <= 12; $j++) {
347
                $yearIndex = $i - $initial_nepali_year;
348
                $total_bs_days += $this->calendarData[$yearIndex][$j];
349
            }
350
        }
351
352
        // Add days for complete months in the target year
353
        $yearIndex = $year - $initial_nepali_year;
354
        for ($j = 1; $j < $month; $j++) {
355
            $total_bs_days += $this->calendarData[$yearIndex][$j];
356
        }
357
358
        // Add remaining days in the final month
359
        $total_bs_days += $day;
360
361
        return $total_bs_days;
362
    }
363
}
364