Completed
Push — master ( 95bc3b...9413fa )
by mehdi
02:10
created

Datium::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 8

Duplication

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

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
354
355
    $this->result->setConfig( $this->language );
356
357
    $this->toConfig = $this->result->getConfig();
358
359
    return $this;
360
361
  }
362
363
  /**
364
   * Get output
365
   * @since Aug 17 2015
366
   * @param $calendar string
367
   * @param $format string
368
   */
369
  public function get( $format = 'Y-m-d H:i:s' ) {
370
371
    $this->translate_from_file = include( 'Lang/en/general.php' );
372
373
    $this->translate_to_file = include( 'Lang/' . $this->language . '/general.php' );
374
375
    if ( is_null( $this->fromConfig ) ) {
376
377
      $this->fromConfig = include( 'CalendarSettings/' . ucfirst( $this->translate_from ) . '.php' );
378
379
    }
380
381
382
    if ( is_null( $this->toConfig ) ) {
383
384
      $this->toConfig = include( 'CalendarSettings/' . ucfirst( $this->translate_to ) . '.php' );
385
386
    }
387
388
      $string_date = $this->date_time->format( $format );
389
390
      if ( $this->translate_to != 'gregorian' ) {
391
392
      $string_date = str_replace( $this->fromConfig['month'], $this->toConfig['month'],  $string_date );
393
394
      $string_date = str_replace( $this->fromConfig['days_of_week'], $this->toConfig['days_of_week'][$this->gregorian_DayofWeek], $string_date );
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...
395
396
      $string_date = str_replace( $this->fromConfig['numbers'], $this->toConfig['numbers'], $string_date );
397
398
      $string_date = str_replace( $this->fromConfig['am_time'], $this->toConfig['am_time'], $string_date );
399
400
      $string_date = str_replace( $this->fromConfig['pm_time'], $this->toConfig['pm_time'], $string_date );
401
402
      $string_date = str_replace( $this->fromConfig['end_of_days'], $this->toConfig['end_of_days'], $string_date );
403
404
    }
405
406
    foreach( $this->translate_to_file as $key => $value ) {
407
408
      $string_date = str_replace( $this->translate_from_file[ $key ], $this->translate_to_file[ $key ], $string_date );
409
410
    }
411
412
    return $string_date;
413
414
  }
415
416
}
417