Completed
Push — master ( 4ac624...f91529 )
by mehdi
06:08
created

Leap::gregorinLeapYear()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 3
eloc 2
nc 3
nop 0
1
<?php namespace Datium\Tools;
2
3
/**
4
 * @since Aug, 21 2015
5
 */
6
class Leap {
7
8
  /**
9
   * @param integer store year value
10
   */
11
	protected $year;
12
13
  /**
14
   * @param string store type of year value
15
   */
16
  protected $type;
17
18
  /**
19
   * @param boolean store result of leap functions
20
   */
21
  protected $result;
22
23
24
  /**
25
   * @param $year integer
26
   * @since Aug, 21 2015
27
   */
28
	public function __construct( $year, $type = 'gregorian' ) {
29
30
			$this->year = $year;
31
32
      $this->type = $type;
33
34
      return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
35
36
	}
37
38
  /**
39
   * check the gregorian year is leap or not
40
   * @since Oct, 24 2015
41
   * @return boolean
42
   */
43
  public function gregorinLeapYear() {
44
45
  return ( ( ( $this->year % 4 ) == 0 ) && ( ( ( $this->year % 100 ) != 0 ) || ( ( $this->year % 400 ) == 0 ) ) );
46
47
  }
48
49
  /**
50
   * check the jalali year is leap or not
51
   * @since Oct, 24 2015
52
   * @return boolean
53
   */
54
  public function jalaliLeapYear() {
55
56
		$jalali_years = 0;
57
58
    while ( $jalali_years < ( $this->year - 128 ) ) {
59
60
      $jalali_years += 128;
61
62
    }
63
64
    //check for leap year after 5 years
65
    $this->result = $this->year - 1;
66
67
    $this->result -= $jalali_years;
68
69
    if ( $this->result >= 33 ) {
70
71
    $this->result = $this->result % 33;
72
73
   }
74
75
    if ( ( $this->result == 28 ) || ( $this->result == 27 ) ) {
76
77
      return $this->result;
78
79
    }
80
81
    //check for leap year after 4 years
82
    $this->result = $this->year;
83
84
    $this->result -= $jalali_years;
85
86
    if ( $this->result >= 33 ) {
87
88
    $this->result = $this->result % 33;
89
90
   }
91
92
    if ( ( ( $this->result % 4 ) == 0 ) && ( $this->result != 28 ) ) {
93
94
      return $this->result;
95
96
    }
97
  }
98
99
  /**
100
   * check the hijri year is leap or not
101
   * @since Oct, 24 2015
102
   * @return boolean
103
   */
104
  public function hijriLeapYear() {
105
106
    $this->result = $this->year % 30;
107
108
    if ( ( 2 == $this->result ) || ( 5 == $this->result ) || ( 7 == $this->result ) || ( 10 == $this->result ) || ( 13 == $this->result ) || ( 16 == $this->result ) || ( 18 == $this->result ) || ( 21 == $this->result ) || ( 24 == $this->year ) || ( 26 == $this->result ) || ( 29 == $this->result ) ) {
109
110
      return $this->result;
111
112
    }
113
114
  }
115
116
  /**
117
   * return the year is leap or not
118
   * @since Aug, 21 2015
119
   * @return boolean
120
   */
121
  public function get() {
122
123
    switch ( $this->type ) {
124
125
      case 'gregorian':
126
127
      $this->result = $this->gregorinLeapYear();
128
129
        break;
130
131
      case 'jalali':
132
133
       $this->result = $this->jalaliLeapYear();
134
135
       break;
136
137
      case 'hijri':
138
139
        $this->result = $this->hijriLeapYear();
140
141
        break;
142
    }
143
144
    return $this->result;
145
146
  }
147
148
}
149