Completed
Push — develop ( b6d6fd...b7f617 )
by Greg
01:53
created

ArabicCalendar::jdToYmd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
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()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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