Completed
Push — master ( 106f43...23f28a )
by mehdi
02:19
created

Datium::translate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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