Completed
Push — master ( 3848cb...1f8425 )
by mehdi
02:54
created

Datium::add()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 36
Code Lines 19

Duplication

Lines 36
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 36
loc 36
rs 8.8571
cc 2
eloc 19
nc 2
nop 1
1
<?php
2
/**
3
 * Main Datium class
4
 *
5
 * @category Core
6
 * @package  OpenCafe\Datium
7
 * @author   Mehdi Hosseini <[email protected]>
8
 * @license  icense https://opensource.org/licenses/MIT
9
 * @link     https://github.com/opencafe/datium
10
 * @since    Aug 17, 2015
11
 */
12
13
namespace OpenCafe;
14
15
use DateTime;
16
use DateInterval;
17
use OpenCafe\Tools\Convert;
18
use OpenCafe\Tools\Leap;
19
use OpenCafe\Tools\DayOf;
20
use OpenCafe\Tools\Lang;
21
use OpenCafe\Tools\TimeAgo;
22
23
use OpenCafe\Datium;
24
25
/**
26
 * Main Datium class
27
 *
28
 * @category Core
29
 * @package  OpenCafe\Datium
30
 * @author   Mehdi Hosseini <[email protected]>
31
 * @license  icense https://opensource.org/licenses/MIT
32
 * @link     https://github.com/opencafe/datium
33
 * @since    Aug 17, 2015
34
 */
35
class Datium
36
{
37
38
    /**
39
   * Store DateTime object
40
   */
41
    protected $date_time;
42
43
    protected static $static_date_time;
44
45
    /**
46
   * Store config file statements
47
   *
48
   * @param array
49
   */
50
    protected $config;
51
52
    protected $date_interval_expression;
53
54
    protected static $date_start;
55
56
    protected static $date_end;
57
58
    protected $translate_from;
59
60
    protected $translate_to;
61
62
    protected $toConfig;
63
64
    protected $fromConfig;
65
66
    /**
67
   * Return store day number
68
   *
69
   * @param integer
70
   */
71
    protected $day_of;
72
73
    protected $leap;
74
75
    protected $events;
76
77
    protected $translate;
78
79
    protected $gregorian_DayofWeek;
80
81
    protected $convert_calendar;
82
83
    protected $calendar_type;
84
85
    protected static $array_date;
86
87
    protected static $call_type;
88
89
    protected $translate_from_file;
90
91
    protected $translate_to_file;
92
93
    protected $language;
94
95
    /**
96
    * Timeago
97
    *
98
    * @param integer
99
    */
100
    protected $ago;
101
102
    /**
103
    * Datium class constructure
104
    */
105
    public function __construct()
106
    {
107
108
        $this->language = 'en';
109
110
        $this->translate_from = 'gregorian';
111
112
        $this->translate_to = 'gregorian';
113
114
        $this->config = include 'Config.php';
115
116
        $this->calendar_type = $this->config[ 'default_calendar' ];
117
118
        date_default_timezone_set($this->config[ 'timezone' ]);
119
120
        $this->calendar_type = 'gregorian';
121
122
        switch (Datium::$call_type) {
123
        case 'now':
124
            $this->date_time = new DateTime('now');
125
126
            $this->gregorian_DayofWeek = $this->date_time->format('w');
127
128
            break;
129
130
        case 'make':
131
            $this->date_time = new DateTime('now');
132
133
            $this->date_time->setDate(
134
                self::$array_date[ 'year' ],
135
                self::$array_date[ 'month' ],
136
                self::$array_date[ 'day' ]
137
            );
138
139
            $this->date_time->setTime(
140
                self::$array_date[ 'hour' ],
141
                self::$array_date[ 'minute' ],
142
                self::$array_date[ 'second' ]
143
            );
144
145
            $this->gregorian_DayofWeek = $this->date_time->format('w');
146
147
            break;
148
149
        case 'set':
150
            $this->date_time = Datium::$static_date_time;
151
152
            $this->gregorian_DayofWeek = $this->date_time->format('w');
153
        }
154
155
        $this->convert_calendar = new Convert();
156
157
    }
158
159
    /**
160
    * Return all datetime parts as an object
161
    *
162
    * @return object
163
    */
164
    public function all()
165
    {
166
167
        return ( object ) array(
168
169
        'second' => $this->date_time->format('s'),
170
171
        'minute' => $this->date_time->format('m'),
172
173
        'hour' => $this->date_time->format('H'),
174
175
        'day' => $this->date_time->format('d'),
176
177
        'month' => $this->date_time->format('m'),
178
179
        'year' => $this->date_time->format('Y')
180
181
        );
182
183
    }
184
185
    /**
186
   * Get current datetime
187
   *
188
   * @since  Aug 17 2015
189
   * @return object
190
   */
191
    public static function now()
192
    {
193
194
        self::$call_type = 'now';
195
196
        return new Datium();
197
198
    }
199
200
    /**
201
   * Create new date time
202
   *
203
   * @param integer $year   Year number
204
   * @param integer $month  month number
205
   * @param integer $day    day number
206
   * @param integer $hour   hour number
207
   * @param integer $minute minute number
208
   * @param integer $second second number
209
   *
210
   * @return object
211
   */
212
    public static function create(
213
        $year = 2000,
214
        $month = 1,
215
        $day = 1,
216
        $hour = 0,
217
        $minute = 0,
218
        $second = 0
219
    ) {
220
221
222
        /**
223
       * When we want to set a Datetime object to Datium
224
       */
225
        if (func_num_args() === 1) {
226
            self::$static_date_time = func_get_arg(0);
227
228
            self::$call_type = 'set';
229
        } else {
230
            self::$array_date = array(
231
              'year' => $year,
232
              'month' => $month,
233
              'day' => $day,
234
              'hour' => $hour,
235
              'minute' => $minute,
236
              'second' => $second
237
            );
238
239
            self::$call_type = 'make';
240
        }
241
242
          return new Datium();
243
244
    }
245
246
    /**
247
    * Select The range between two date object
248
    *
249
    * @param object $date_start Start of the DateTime
250
    * @param object $date_end   End of the DateTime
251
    *
252
    * @return object
253
    */
254
    public static function between($date_start, $date_end)
255
    {
256
257
        self::$date_start = $date_start;
258
259
        self::$date_end = $date_end;
260
261
        self::$call_type = 'between';
262
263
        return new Datium();
264
265
    }
266
267
    /**
268
   * Convert from current calendar, what type is current calendar?
269
   *
270
   * @param object $calendar Assigned calendar to start from
271
   *
272
   * @return $object
0 ignored issues
show
Documentation introduced by
The doc-type $object could not be parsed: Unknown type name "$object" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
273
   */
274 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...
275
    {
276
277
        $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...
278
279
        $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...
280
281
282
        /**
283
     * We need this part for DayOf class
284
     */
285
        $this->calendar_type = $calendar;
286
287
        $this->translate_to = $calendar;
288
289
        return $this;
290
291
    }
292
293
    /**
294
    * Convert date to current Date
295
    *
296
    * @param object $calendar Assigned calendar to when calendar should start.
297
    *
298
    * @return object
299
    */
300 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...
301
    {
302
303
        $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...
304
305
        $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...
306
307
        /**
308
     * We need this part for DayOf class
309
     */
310
        $this->calendar_type = $calendar;
311
312
        $this->translate_to = $calendar;
313
314
        return $this;
315
316
    }
317
318
    /**
319
   * Difference between two time
320
   *
321
   * @param DateTime $start Start of the date
322
   * @param DateTime $end   End of the date
323
   *
324
   * @return object
325
   */
326
    public static function diff($start, $end)
327
    {
328
329
        return date_diff($start, $end);
330
331
    }
332
333
    /**
334
   * Add new date value to current date
335
   *
336
   * @param string $value How much date should be added to current date
337
   *
338
   * @return object
339
   */
340 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...
341
    {
342
343
        $this->date_interval_expression = str_replace(
344
            $this->config[ 'date_simple' ],
345
            $this->config[ 'date_interval' ],
346
            $value
347
        );
348
349
        $unit = 'P';
350
351
        if( strpos($this->date_interval_expression, 'T') ) {
352
353
          $this->date_interval_expression= str_replace(
354
            'T',
355
            '',
356
            $this->date_interval_expression
357
          );
358
359
          $unit = 'PT';
360
361
        }
362
363
        $this->date_interval_expression = str_replace(
364
            ' ',
365
            '',
366
            $unit . $this->date_interval_expression
367
        );
368
369
        $this->date_time->add(
370
            new DateInterval($this->date_interval_expression)
371
        );
372
373
        return $this;
374
375
    }
376
377
    /**
378
   * Sub date from current date
379
   *
380
   * @param string $value How much date should increase from current date
381
   *
382
   * @return obejct
383
   */
384 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...
385
    {
386
387
        $this->date_interval_expression = str_replace(
388
            $this->config[ 'date_simple' ],
389
            $this->config[ 'date_interval' ],
390
            $value
391
        );
392
393
        $unit = 'P';
394
395
        if( strpos($this->date_interval_expression, 'T') ) {
396
397
          $this->date_interval_expression= str_replace(
398
            'T',
399
            '',
400
            $this->date_interval_expression
401
          );
402
403
          $unit = 'PT';
404
405
        }
406
407
        $this->date_interval_expression = str_replace(
408
            ' ',
409
            '',
410
            $unit . $this->date_interval_expression
411
        );
412
413
        $this->date_time->sub(
414
            new DateInterval($this->date_interval_expression)
415
        );
416
417
        return $this;
418
419
    }
420
421
    /**
422
   * Check if current year is leap or not
423
   *
424
   * @param string $type Name of the calendar to caculate leap year
0 ignored issues
show
Bug introduced by
There is no parameter named $type. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
425
   *
426
   * @return boolean
427
   */
428
    public function leap()
429
    {
430
431
        $this->leap = new Leap($this->date_time->format('Y'), $this->calendar_type);
0 ignored issues
show
Bug introduced by
It seems like $this->calendar_type can also be of type object; however, OpenCafe\Tools\Leap::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
432
433
        return $this->leap;
434
435
    }
436
437
    /**
438
    * Calculate how many time ago datetime happens
439
    *
440
    * @return string
441
    */
442
    public function ago()
443
    {
444
445
      $this->ago = new TimeAgo( $this->date_time, $this->language );
446
447
      return $this->ago->get();
448
449
    }
450
451
    /**
452
    * Caculate day of year or week
453
    *
454
    * @since Aug, 22 2015
455
    *
456
    * @return integer
457
    */
458
    public function dayOf()
459
    {
460
461
        $this->day_of = new DayOf($this->date_time, $this->calendar_type);
0 ignored issues
show
Bug introduced by
It seems like $this->calendar_type can also be of type object; however, OpenCafe\Tools\DayOf::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
462
463
        return $this->day_of;
464
465
    }
466
467
    // public function events()
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...
468
    // {
469
    //
470
    //     if (Datium::$call_type == 'between' ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% 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...
471
    //
472
    //         $this->events = new Events(Datium::$date_start, Datium::$date_end);
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% 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...
473
    //
474
    //     } else {
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...
475
    //
476
    //         $this->events = new Events($this->date_time);
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...
477
    //
478
    //     }
479
    //
480
    //     return $this->events;
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...
481
    //
482
    // }
483
484
    /**
485
    * Return Datetime as a original object
486
    *
487
    * @since Oct 22, 2015
488
    *
489
    * @return object
490
    */
491
    public function object()
492
    {
493
494
        return $this->date_time;
495
496
    }
497
498
    /**
499
    * Translate current date string to selected language
500
    *
501
    * @param string $language language short name fa, en, ar ...
502
    *
503
    * @return object
504
    */
505
    public function lang($language = 'fa')
506
    {
507
508
        $this->language = $language;
509
510
        $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...
511
512
        $this->result->setConfig($this->language);
513
514
        $this->toConfig = $this->result->getConfig();
515
516
        return $this;
517
518
    }
519
520
    /**
521
    * Return object as timestamp
522
    *
523
    * @return timestamp
524
    */
525
    public function timestamp()
526
    {
527
528
        return strtotime($this->date_time->format('Y-m-d H:i:s'));
529
530
    }
531
532
    /**
533
   * Return fainal result
534
   *
535
   * @param string $format Date format
536
   *
537
   * @since Aug 17 2015
538
   *
539
   * @return string
540
   */
541
    public function get($format = 'Y-m-d H:i:s')
542
    {
543
544
        // $this->translate_from_file = include( 'Lang/en/general.php' );
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...
545
        //
546
        // $this->translate_to_file = include( 'Lang/' . $this->language . '/general.php' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% 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...
547
548
        if (is_null($this->fromConfig)) {
549
            $this->fromConfig = include 'CalendarSettings/' .
550
                                ucfirst($this->translate_from) . '.php';
551
        }
552
553
554
        if (is_null($this->toConfig)) {
555
            $this->toConfig = include 'CalendarSettings/' .
556
                                       ucfirst($this->translate_to) . '.php';
557
        }
558
559
        $string_date = $this->date_time->format($format);
560
561
        if ($this->translate_to != 'gregorian') {
562
            $string_date = str_replace(
563
                $this->fromConfig[ 'month' ],
564
                $this->toConfig[ 'month' ],
565
                $string_date
566
            );
567
568
            $string_date = str_replace(
569
                $this->fromConfig[ 'days_of_week' ],
570
                $this->toConfig[ 'days_of_week' ][ $this->gregorian_DayofWeek ],
571
                $string_date
572
            );
573
574
            $string_date = str_replace(
575
                $this->fromConfig[ 'numbers' ],
576
                $this->toConfig[ 'numbers' ],
577
                $string_date
578
            );
579
580
            $string_date = str_replace(
581
                $this->fromConfig[ 'am_time' ],
582
                $this->toConfig[ 'am_time' ],
583
                $string_date
584
            );
585
586
            $string_date = str_replace(
587
                $this->fromConfig[ 'pm_time' ],
588
                $this->toConfig[ 'pm_time' ],
589
                $string_date
590
            );
591
592
            $string_date = str_replace(
593
                $this->fromConfig[ 'end_of_days' ],
594
                $this->toConfig[ 'end_of_days' ],
595
                $string_date
596
            );
597
        }
598
599
        // foreach( $this->translate_to_file as $key => $value ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% 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...
600
        //
601
          // $string_date = str_replace(
602
          //   $this->translate_from_file[ $key ],
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...
603
          //   $this->translate_to_file[ $key ],
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...
604
          //   $string_date
605
          // );
606
        //
607
        // }
608
609
        return $string_date;
610
611
    }
612
}
613