Completed
Push — master ( 5aa9e7...387997 )
by mehdi
03:34
created

Datium::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.4286
cc 1
eloc 8
nc 1
nop 0
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
   * @since Aug, 22 2015
301
   */
302
  // protected function format( $calendar, $format ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
303
  //
304
  //   $this->date_time = $this->date_time->format( $format );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
305
  //
306
  //   if( in_array( $calendar, $this->config['calendar'] ) ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
307
  //
308
  //   switch( $calendar ){
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
309
  //
310
  //     case 'ir':
311
  //
312
  //         $this->date_time = str_replace( $this->config['month']['english'], $this->config['month']['persian'], $this->date_time );
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
313
  //
314
  //         $this->date_time = str_replace( $this->config['week_days_name']['english'], $this->config['week_days_name']['persian'][$this->geregorian_DayofWeek], $this->date_time );
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
315
  //
316
  //         break;
317
  //
318
  //     case 'gh':
319
  //
320
  //         $this->date_time = str_replace( $this->config['month']['english'], $this->config['month']['islamic'], $this->date_time );
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
321
  //
322
  //         $this->date_time = str_replace( $this->config['week_days_name']['english'], $this->config['week_days_name']['islamic'][$this->geregorian_DayofWeek], $this->date_time );
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
323
  //
324
  //         break;
325
  //
326
  //     case 'gr':
327
  //
328
  //         $this->date_time = $this->date_time;
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
329
  //
330
  //       break;
331
  //
332
  //   }
333
  //
334
  // }
335
  //
336
  //   return $this->date_time;
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
337
  //
338
  // }
339
340 View Code Duplication
  public function toShamsi( $type = 'gr' ) {
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...
341
342
    switch ( $type ) {
343
344
      case 'gr':
345
346
           $this->date_time = $this->convert_calendar->gregorianToShamsi( $this->date_time );
0 ignored issues
show
Bug introduced by
The method gregorianToShamsi() does not seem to exist on object<Datium\Tools\Convert>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
347
348
        break;
349
350
      case 'gh':
351
352
            $this->date_time = $this->convert_calendar->ghamariToShamsi( $this->date_time );
353
354
        break;
355
    }
356
357
    $this->calendar_type = 'ir';
358
359
    return $this;
360
361
  }
362
363 View Code Duplication
  public function toGhamari( $type = 'gr') {
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...
364
365
switch ( $type  ) {
366
367
  case 'ir':
368
369
        $this->date_time = $this->convert_calendar->shamsiToGhamari( $this->date_time );
370
371
    break;
372
373
  case 'gr':
374
375
    $this->date_time = $this->convert_calendar->gregorianToGhamari( $this->date_time );
0 ignored issues
show
Bug introduced by
The method gregorianToGhamari() does not seem to exist on object<Datium\Tools\Convert>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
376
377
    break;
378
379
}
380
381
   $this->calendar_type = 'gh';
382
383
    return $this;
384
385
  }
386
387
  public function toGregorian( $type = 'gr' ) {
388
389 View Code Duplication
    switch ( $type ) {
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...
390
391
      case 'ir':
392
393
        $this->date_time = $this->convert_calendar->shamsiToGregorian( $this->date_time );
394
395
        break;
396
397
      case 'gh':
398
399
        $this->date_time = $this->convert_calendar->ghamariToGregorian( $this->date_time );
400
401
      case 'gr':
402
403
        $this->date_time = $this->date_time;
404
405
        break;
406
407
    }
408
409
    $this->calendar_type = 'gr';
410
411
    return $this;
412
413
  }
414
415
  /************************************************************
416
   * Return Datetime as a original object
417
   ************************************************************
418
   *
419
   * @since Oct 22, 2015
420
   * @return object
421
   *
422
   *\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
423
   */
424
  public function object(){
425
426
    return $this->date_time;
427
428
  }
429
430
  /**
431
   * Get output
432
   * @since Aug 17 2015
433
   * @param $calendar string
434
   * @param $format string
435
   */
436
  public function get( $format = 'Y-m-d H:i:s' ) {
437
438
    if( in_array( $this->calendar_type, $this->config[ 'calendar' ] ) ){
439
440
        return $this->translate( $this->calendar_type, $format )->get();
441
442
    }
443
444
}
445
446
}
447