Completed
Push — master ( d5f8b0...0fe70c )
by mehdi
02:20
created

Datium::from()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 6

Duplication

Lines 16
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 16
loc 16
rs 9.4286
cc 1
eloc 6
nc 1
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
  protected $translate_from;
38
39
  protected $translate_to;
40
41
  /**
42
   * return store day number
43
   * @param integer
44
   */
45
  protected $day_of;
46
47
  protected $leap;
48
49
  protected $events;
50
51
  protected $translate;
52
53
  protected $geregorian_DayofWeek;
54
55
  protected $convert_calendar;
56
57
  protected $calendar_type;
58
59
  protected static $array_date;
60
61
  protected static $call_type;
62
63
  public function __construct() {
64
65
    $this->translate_from = 'gregorian';
66
67
    $this->translate_to = 'gregorian';
68
69
    $this->config = include('Config.php');
70
71
    $this->calendar_type = $this->config[ 'default_calendar' ];
72
73
    date_default_timezone_set( $this->config['timezone'] );
74
75
    $this->calendar_type = 'gregorian';
76
77
    switch( Datium::$call_type ) {
78
79
      case 'now':
80
81
        $this->date_time = new DateTime( 'now' );
82
83
        $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...
84
85
        break;
86
87
      case 'make':
88
89
        $this->date_time = new DateTime( 'now' );
90
91
        $this->date_time->setDate( self::$array_date['year'], self::$array_date['month'], self::$array_date['day'] );
92
93
        $this->date_time->setTime( self::$array_date['hour'], self::$array_date['minute'], self::$array_date['second'] );
94
95
        $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...
96
97
        break;
98
99
      case 'set':
100
101
        $this->date_time = Datium::$static_date_time;
102
103
        $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...
104
105
    }
106
107
    $this->convert_calendar = new Convert();
108
109
  }
110
111
  /************************************************************
112
   * return all datetime parts as an object
113
   ************************************************************
114
   *
115
   * @return object
116
   *
117
   */
118
  public function all() {
119
120
    return (object) array(
121
122
      'second' => $this->date_time->format( 's' ),
123
124
      'minute' => $this->date_time->format( 'm' ),
125
126
      'hour' => $this->date_time->format( 'H' ),
127
128
      'day' => $this->date_time->format( 'd' ),
129
130
      'month' => $this->date_time->format( 'm' ),
131
132
      'year' => $this->date_time->format( 'Y' )
133
134
    );
135
136
  }
137
138
  /**
139
   * Get current datetime
140
   * @since Aug 17 2015
141
   * @return object
142
   */
143
  public static function now() {
144
145
    self::$call_type = 'now';
146
147
    return new Datium();
148
149
  }
150
151
  /**
152
   * Create new date time
153
   * @param $year integer
154
   * @param $month integer
155
   * @param $day integer
156
   * @param $hour integer
157
   * @param $minute integer
158
   * @param $second integer
159
   * @return object
160
   */
161
  public static function create( $year = 2000, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0 ) {
162
163
      /**
164
       * When we want to set a Datetime object to Datium
165
       */
166
      if( func_num_args() === 1 ) {
167
168
        self::$static_date_time = func_get_arg(0);
169
170
        self::$call_type = 'set';
171
172
      } else {
173
174
        self::$array_date = array( 'year' => $year, 'month' => $month, 'day' => $day, 'hour' => $hour, 'minute' => $minute, 'second' => $second );
175
176
        self::$call_type = 'make';
177
178
      }
179
180
      return new Datium();
181
182
  }
183
184
  public static function between( $date_start, $date_end ) {
185
186
    self::$date_start = $date_start;
187
188
    self::$date_end = $date_end;
189
190
    self::$call_type = 'between';
191
192
    return new Datium();
193
194
  }
195
196
  /**
197
   * Convert from current calendar, what type is current calendar?
198
   */
199 View Code Duplication
  public function from( $calendar ) {
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...
200
201
    $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...
202
203
    $this->date_time = $this->convert->from( $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...
204
205
    /**
206
     * We need this part for DayOf class
207
     */
208
    $this->calendar_type = $calendar;
209
210
    $this->translate_to = $calendar;
211
212
    return $this;
213
214
  }
215
216 View Code Duplication
  public function to( $calendar ) {
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...
217
218
    $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...
219
220
    $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...
221
222
    /**
223
     * We need this part for DayOf class
224
     */
225
    $this->calendar_type = $calendar;
226
227
    $this->translate_to = $calendar;
228
229
    return $this;
230
231
  }
232
233
234
  /**
235
   * Difference between two time
236
   * @param $start datetime
237
   * @param $end datetime
238
   */
239
  public static function diff( $start, $end ) {
240
241
    return date_diff( $start, $end );
242
243
  }
244
245
  /**
246
   * Add new date value to current date
247
   * @param $value string
248
   * @return object
249
   */
250 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...
251
252
    $this->date_interval_expression = str_replace( $this->config['date_simple'], $this->config['date_interval'], $value );
253
254
    $this->date_interval_expression = str_replace( ' ', '', 'P' . $this->date_interval_expression );
255
256
    $this->date_time->add( new DateInterval( $this->date_interval_expression ) );
257
258
    return $this;
259
260
  }
261
262
  /**
263
   * Sub date from current date
264
   * @param $value
265
   * @return obejct
266
   */
267 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...
268
269
    $this->date_interval_expression = str_replace( $this->config['date_simple'], $this->config['date_interval'], $value );
270
271
    $this->date_interval_expression = str_replace( ' ', '', 'P' . $this->date_interval_expression );
272
273
    $this->date_time->sub( new DateInterval( $this->date_interval_expression ) );
274
275
    return $this;
276
277
  }
278
279
  /**
280
   * Check if current year is leap or not
281
   * @return boolean
282
   */
283
  public function leap( $type = 'gregorian') {
284
285
    $this->leap = new Leap( $this->date_time->format( 'Y' ), $type );
286
287
    return $this->leap;
288
289
  }
290
291
  /**
292
   * @since Aug, 22 2015
293
   */
294
  public function dayOf() {
295
296
    $this->day_of = new DayOf( $this->date_time, $this->calendar_type );
297
298
    return $this->day_of;
299
300
  }
301
302
  /**
303
   * @since Sept, 7 2015
304
   */
305
  public function events() {
306
307
    if ( Datium::$call_type == 'between' ) {
308
309
      $this->events = new Events( Datium::$date_start, Datium::$date_end );
310
311
    } else {
312
313
      $this->events = new Events( $this->date_time );
314
315
    }
316
317
    return $this->events;
318
319
  }
320
321
  /************************************************************
322
   * Return Datetime as a original object
323
   ************************************************************
324
   *
325
   * @since Oct 22, 2015
326
   * @return object
327
   *
328
   *\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
329
   */
330
  public function object(){
331
332
    return $this->date_time;
333
334
  }
335
336
  /**
337
   * Get output
338
   * @since Aug 17 2015
339
   * @param $calendar string
340
   * @param $format string
341
   */
342
  public function get( $format = 'Y-m-d H:i:s' ) {
343
344
      $fromConfig = include( 'CalendarSettings/' . ucfirst( $this->translate_from ) . '.php' );
345
346
      $toConfig = include( 'CalendarSettings/' . ucfirst( $this->translate_to ) . '.php' );
347
348
      $string_date = $this->date_time->format( $format );
349
350
      $string_date = str_replace( $fromConfig['month'], $toConfig['month'],  $string_date );
351
352
      $string_date = str_replace( $fromConfig['days_of_week'], $toConfig['days_of_week'],  $string_date );
353
354
      return $string_date;
355
356
  }
357
358
}
359