Completed
Push — master ( dd6000...fb0568 )
by
unknown
02:20
created

Datium::object()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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