1
|
|
|
<?php |
2
|
|
|
namespace Fisharebest\ExtCalendar; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class ArabicCalendar - calculations for the Arabic (Hijri) calendar. |
8
|
|
|
* |
9
|
|
|
* @author Greg Roach <[email protected]> |
10
|
|
|
* @copyright (c) 2014-2017 Greg Roach |
11
|
|
|
* @license This program is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* This program is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU General Public License |
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
23
|
|
|
*/ |
24
|
|
|
class ArabicCalendar implements CalendarInterface { |
25
|
|
|
public function daysInMonth($year, $month) { |
26
|
|
|
if ($month === 2) { |
27
|
|
|
return 28; |
28
|
|
|
} elseif ($month % 2 === 1 || $month === 12 && $this->isLeapYear($year)) { |
29
|
|
|
return 30; |
30
|
|
|
} else { |
31
|
|
|
return 29; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function daysInWeek() { |
36
|
|
|
return 7; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function gedcomCalendarEscape() { |
40
|
|
|
return '@#DHIJRI@'; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function isLeapYear($year) { |
44
|
|
|
return ((11 * $year + 14) % 30) < 11; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function jdEnd() { |
48
|
|
|
return PHP_INT_MAX; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function jdStart() { |
52
|
|
|
return 1948440; // 1 Muharram 1 AH, 16 July 622 AD |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function jdToYmd($julian_day) { |
56
|
|
|
$year = (int) ((30 * ($julian_day - 1948440) + 10646) / 10631); |
57
|
|
|
$month = (int) ((11 * ($julian_day - $year * 354 - (int) ((3 + 11 * $year) / 30) - 1948086) + 330) / 325); |
58
|
|
|
$day = $julian_day - 29 * ($month - 1) - (int) ((6 * $month - 1) / 11) - $year * 354 - (int) ((3 + 11 * $year) / 30) - 1948085; |
59
|
|
|
|
60
|
|
|
return array($year, $month, $day); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function monthsInYear() { |
64
|
|
|
return 12; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function ymdToJd($year, $month, $day) { |
68
|
|
View Code Duplication |
if ($month < 1 || $month > $this->monthsInYear()) { |
|
|
|
|
69
|
|
|
throw new InvalidArgumentException('Month ' . $month . ' is invalid for this calendar'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $day + 29 * ($month - 1) + (int) ((6 * $month - 1) / 11) + $year * 354 + (int) ((3 + 11 * $year) / 30) + 1948085; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* The algorithm used above require a mod() function that always returns a positive |
77
|
|
|
* modules. The native PHP function returns a negative modulus for a negative dividend. |
78
|
|
|
* |
79
|
|
|
* @param number $dividend |
80
|
|
|
* @param number $divisor |
81
|
|
|
* @return number |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public static function mod($dividend, $divisor) { |
|
|
|
|
84
|
|
|
if ($divisor === 0) return 0; |
85
|
|
|
|
86
|
|
|
$modulus = $dividend % $divisor; |
87
|
|
|
if($modulus < 0) { |
88
|
|
|
$modulus += $divisor; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $modulus; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.