Completed
Push — master ( 821eef...e5b389 )
by mehdi
02:43
created

Datium::toGregorian()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 12

Duplication

Lines 19
Ratio 70.37 %

Importance

Changes 9
Bugs 0 Features 8
Metric Value
c 9
b 0
f 8
dl 19
loc 27
rs 8.5806
cc 4
eloc 12
nc 4
nop 1
1
<?php namespace Datium;
2
3
use DateTime;
4
use DateInterval;
5
use Datium\Tools\Convert;
6
use Datium\Tools\Leap;
7
use Datium\Tools\DayOf;
8
use Datium\Tools\Translate;
9
10
/**
11
 *
12
 * @since Aug 17, 2015
13
 *
14
 *\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
15
 */
16
class Datium {
17
18
  /**
19
   * Store DateTime object
20
   */
21
  protected $date_time;
22
23
  protected static $static_date_time;
24
25
  /**
26
   * Store config file statements
27
   * @param array
28
   */
29
  protected $config;
30
31
  protected $date_interval_expression;
32
33
  protected static $date_start;
34
35
  protected static $date_end;
36
37
  /**
38
   * return store day number
39
   * @param integer
40
   */
41
  protected $day_of;
42
43
  protected $leap;
44
45
  protected $events;
46
47
  protected $translate;
48
49
  protected $geregorian_DayofWeek;
50
51
  protected $convert_calendar;
52
53
  protected $calendar_type;
54
55
  protected static $array_date;
56
57
  protected static $call_type;
58
59
  public function __construct() {
60
61
    $this->config = include('Config.php');
62
63
    $this->calendar_type = $this->config[ 'default_calendar' ];
64
65
    date_default_timezone_set( $this->config['timezone'] );
66
67
    switch( Datium::$call_type ) {
68
69
      case 'now':
70
71
        $this->date_time = new DateTime( 'now' );
72
73
        $this->gregorian_DayofWeek = $this->date_time->format('w');
0 ignored issues
show
Bug introduced by
The property gregorian_DayofWeek does not seem to exist. Did you mean geregorian_DayofWeek?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
74
75
        $this->calendar_type = 'gr';
76
77
        break;
78
79
      case 'make':
80
81
        $this->date_time = new DateTime( 'now' );
82
83
        $this->date_time->setDate( self::$array_date['year'], self::$array_date['month'], self::$array_date['day'] );
84
85
        $this->date_time->setTime( self::$array_date['hour'], self::$array_date['minute'], self::$array_date['second'] );
86
87
        $this->gregorian_DayofWeek = $this->date_time->format('w');
0 ignored issues
show
Bug introduced by
The property gregorian_DayofWeek does not seem to exist. Did you mean geregorian_DayofWeek?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
88
89
        $this->calendar_type = 'gr';
90
91
92
        break;
93
94
      case 'set':
95
96
        $this->date_time = Datium::$static_date_time;
97
98
        $this->gregorian_DayofWeek = $this->date_time->format('w');
0 ignored issues
show
Bug introduced by
The property gregorian_DayofWeek does not seem to exist. Did you mean geregorian_DayofWeek?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
99
100
        $this->calendar_type = 'gr';
101
102
    }
103
104
    $this->convert_calendar = new Convert();
105
106
  }
107
108
  /************************************************************
109
   * return all datetime parts as an object
110
   ************************************************************
111
   *
112
   * @return object
113
   *
114
   */
115
  public function all() {
116
117
    return (object) array(
118
119
      'second' => $this->date_time->format( 's' ),
120
121
      'minute' => $this->date_time->format( 'm' ),
122
123
      'hour' => $this->date_time->format( 'H' ),
124
125
      'day' => $this->date_time->format( 'd' ),
126
127
      'month' => $this->date_time->format( 'm' ),
128
129
      'year' => $this->date_time->format( 'Y' )
130
131
    );
132
133
  }
134
135
  /**
136
   * Get current datetime
137
   * @since Aug 17 2015
138
   * @return object
139
   */
140
  public static function now() {
141
142
    self::$call_type = 'now';
143
144
    return new Datium();
145
146
  }
147
148
  /**
149
   * Create new date time
150
   * @param $year integer
151
   * @param $month integer
152
   * @param $day integer
153
   * @param $hour integer
154
   * @param $minute integer
155
   * @param $second integer
156
   * @return object
157
   */
158
  public static function create( $year = 2000, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0 ) {
159
160
      /**
161
       * When we want to set a Datetime object to Datium
162
       */
163
      if( func_num_args() === 1 ) {
164
165
        self::$static_date_time = func_get_arg(0);
166
167
        self::$call_type = 'set';
168
169
      } else {
170
171
        self::$array_date = array( 'year' => $year, 'month' => $month, 'day' => $day, 'hour' => $hour, 'minute' => $minute, 'second' => $second );
172
173
        self::$call_type = 'make';
174
175
      }
176
177
      return new Datium();
178
179
  }
180
181
  public static function between( $date_start, $date_end ) {
182
183
    self::$date_start = $date_start;
184
185
    self::$date_end = $date_end;
186
187
    self::$call_type = 'between';
188
189
    return new Datium();
190
191
  }
192
193
  public function to( $calendar ) {
194
195
    $this->convert = new Convert( $this->date_time );
0 ignored issues
show
Bug introduced by
The property convert does not seem to exist. Did you mean convert_calendar?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
196
197
    $this->date_time = $this->convert->to( $calendar );
0 ignored issues
show
Bug introduced by
The property convert does not seem to exist. Did you mean convert_calendar?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
198
199
    return $this;
200
201
  }
202
203
204
  /**
205
   * Difference between two time
206
   * @param $start datetime
207
   * @param $end datetime
208
   */
209
  public static function diff( $start, $end ) {
210
211
    return date_diff( $start, $end );
212
213
  }
214
215
  /**
216
   * Add new date value to current date
217
   * @param $value string
218
   * @return object
219
   */
220 View Code Duplication
  public function add( $value ) {
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...
221
222
    $this->date_interval_expression = str_replace( $this->config['date_simple'], $this->config['date_interval'], $value );
223
224
    $this->date_interval_expression = str_replace( ' ', '', 'P' . $this->date_interval_expression );
225
226
    $this->date_time->add( new DateInterval( $this->date_interval_expression ) );
227
228
    return $this;
229
230
  }
231
232
  /**
233
   * Sub date from current date
234
   * @param $value
235
   * @return obejct
236
   */
237 View Code Duplication
  public function sub( $value ) {
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...
238
239
    $this->date_interval_expression = str_replace( $this->config['date_simple'], $this->config['date_interval'], $value );
240
241
    $this->date_interval_expression = str_replace( ' ', '', 'P' . $this->date_interval_expression );
242
243
    $this->date_time->sub( new DateInterval( $this->date_interval_expression ) );
244
245
    return $this;
246
247
  }
248
249
  /**
250
   * Check if current year is leap or not
251
   * @return boolean
252
   */
253
  public function leap( $type = 'gr') {
254
255
    $this->leap = new Leap( $this->date_time->format( 'Y' ), $type );
256
257
    return $this->leap;
258
259
  }
260
261
  /**
262
   * @since Aug, 22 2015
263
   */
264
  public function dayOf() {
265
266
    $this->day_of = new DayOf( $this->date_time, $this->calendar_type );
267
268
    return $this->day_of;
269
270
  }
271
272
  /**
273
   * @since Sept, 7 2015
274
   */
275
  public function events() {
276
277
    if ( Datium::$call_type == 'between' ) {
278
279
      $this->events = new Events( Datium::$date_start, Datium::$date_end );
280
281
    } else {
282
283
      $this->events = new Events( $this->date_time );
284
285
    }
286
287
    return $this->events;
288
289
  }
290
291
  public function translate( $calendar, $format ) {
292
293
    $this->date_time = new Translate( $this->date_time, $calendar, $format, $this->gregorian_DayofWeek );
0 ignored issues
show
Bug introduced by
The property gregorian_DayofWeek does not seem to exist. Did you mean geregorian_DayofWeek?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
294
295
    return $this->date_time;
296
297
  }
298
  
299
  /************************************************************
300
   * Return Datetime as a original object
301
   ************************************************************
302
   *
303
   * @since Oct 22, 2015
304
   * @return object
305
   *
306
   *\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
307
   */
308
  public function object(){
309
310
    return $this->date_time;
311
312
  }
313
314
  /**
315
   * Get output
316
   * @since Aug 17 2015
317
   * @param $calendar string
318
   * @param $format string
319
   */
320
  public function get( $format = 'Y-m-d H:i:s' ) {
321
322
    if( in_array( $this->calendar_type, $this->config[ 'calendar' ] ) ){
323
324
        return $this->translate( $this->calendar_type, $format )->get();
325
326
    }
327
328
}
329
330
}
331