Completed
Push — master ( 5b3717...8aa5e7 )
by
unknown
12s
created

Datium::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 20
rs 9.4285
1
<?php
2
/**
3
 * Main Datium class
4
 *
5
 * @category Core
6
 * @package  OpenCafe\Datium
7
 * @author   Mehdi Hosseini <[email protected]>
8
 * @license  License 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\SimpleDiff;
22
23
/**
24
 * Main Datium class
25
 *
26
 * @category Core
27
 * @package  OpenCafe\Datium
28
 * @author   Mehdi Hosseini <[email protected]>
29
 * @license  icense https://opensource.org/licenses/MIT
30
 * @link     https://github.com/opencafe/datium
31
 * @since    Aug 17, 2015
32
 */
33
class Datium
34
{
35
36
    /**
37
     * Store DateTime object
38
     */
39
    protected $date_time;
40
41
    protected static $static_date_time;
42
43
    /**
44
     * Store config file statements
45
     *
46
     * @param array
47
     */
48
    protected $config;
49
50
    protected $date_interval_expression;
51
52
    protected static $date_start;
53
54
    protected static $date_end;
55
56
    protected $translate_from;
57
58
    protected $translate_to;
59
60
    protected $toConfig;
61
62
    protected $fromConfig;
63
64
    /**
65
     * Return store day number
66
     *
67
     * @param integer
68
     */
69
    protected $day_of;
70
71
    protected $leap;
72
73
    protected $events;
74
75
    protected $translate;
76
77
    protected $gregorian_DayofWeek;
78
79
    protected $convert_calendar;
80
81
    protected $calendar_type;
82
83
    protected static $array_date;
84
85
    protected static $call_type;
86
87
    protected $translate_from_file;
88
89
    protected $translate_to_file;
90
91
    protected $language;
92
93
    protected static $timestamp;
94
95
    /**
96
     * SimpleDiff
97
     *
98
     * @param integer
99
     */
100
    protected $simpleDiff;
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
        $this->setConfig();
114
115
        $this->calendar_type = $this->config['default_calendar'];
116
117
        $this->calendar_type = 'gregorian';
118
119
        switch (Datium::$call_type) {
120
121
122
            case 'create-timestamp':
123
124
                $this->date_time = new DateTime();
125
126
                $this->date_time->setTimestamp(self::$timestamp);
127
128
                break;
129
130
            case 'now':
131
132
                $this->date_time = new DateTime('now');
133
134
                break;
135
136
            case 'make':
137
138
                $this->date_time = new DateTime('now');
139
140
                $this->date_time->setDate(
141
                    self::$array_date['year'],
142
                    self::$array_date['month'],
143
                    self::$array_date['day']
144
                );
145
146
                $this->date_time->setTime(
147
                    self::$array_date['hour'],
148
                    self::$array_date['minute'],
149
                    self::$array_date['second']
150
                );
151
152
                break;
153
154
            case 'set':
155
156
                $this->date_time = Datium::$static_date_time;
157
158
        }
159
160
        $this->gregorian_DayofWeek = $this->date_time->format('w');
161
162
        $this->convert_calendar = new Convert();
163
    }
164
165
    /**
166
     * Return all datetime parts as an object
167
     *
168
     * @return object
169
     */
170
    public function all()
171
    {
172
173
        return ( object )array(
174
175
            'second' => $this->date_time->format('s'),
176
177
            'minute' => $this->date_time->format('m'),
178
179
            'hour' => $this->date_time->format('H'),
180
181
            'day' => $this->date_time->format('d'),
182
183
            'month' => $this->date_time->format('m'),
184
185
            'year' => $this->date_time->format('Y')
186
187
        );
188
189
    }
190
191
    /**
192
     * Get current datetime
193
     *
194
     * @since  Aug 17 2015
195
     * @return object
196
     */
197
    public static function now()
198
    {
199
200
        self::$call_type = 'now';
201
202
        return new Datium();
203
204
    }
205
206
    /**
207
     * Set config
208
     *
209
     * @since June 28 2016
210
     *
211
     * @param array $config
212
     *
213
     * @return $this
214
     */
215
    public function setConfig($config = [])
216
    {
217
        $defaults     = include(__DIR__.'/Config.php');
218
        $this->config = array_merge($defaults, $config);
219
        date_default_timezone_set($this->config['timezone']);
220
221
        return $this;
222
    }
223
224
    /**
225
     * Create new date time
226
     *
227
     * @param integer $year   Year number
228
     * @param integer $month  month number
229
     * @param integer $day    day number
230
     * @param integer $hour   hour number
231
     * @param integer $minute minute number
232
     * @param integer $second second number
233
     *
234
     * @return object
235
     */
236
    public static function create(
237
        $year = 2000,
238
        $month = 1,
239
        $day = 1,
240
        $hour = 0,
241
        $minute = 0,
242
        $second = 0
243
    ) {
244
245
        /**
246
         * When we want to set a Datetime object to Datium
247
         */
248
        if (func_num_args() === 1) {
249
            self::$static_date_time = func_get_arg(0);
250
251
            self::$call_type = 'set';
252
        } else {
253
            self::$array_date = array(
254
                'year'   => $year,
255
                'month'  => $month,
256
                'day'    => $day,
257
                'hour'   => $hour,
258
                'minute' => $minute,
259
                'second' => $second
260
            );
261
262
            self::$call_type = 'make';
263
        }
264
265
        return new Datium();
266
267
    }
268
269
    /**
270
     * Accept Timestamp as Datium initializion
271
     *
272
     * @param  timestamp $timestamp Input timestamp value
273
     *
274
     * @return object
275
     */
276
    public static function createTimestamp($timestamp)
277
    {
278
279
        if ($timestamp != null && is_int($timestamp)) {
280
281
            self::$call_type = 'create-timestamp';
282
283
            self::$timestamp = $timestamp;
284
285
            return new Datium();
286
287
        } else {
288
289
            throw new \Exception("Error timestamp is not entered in true format", 1);
290
291
        }
292
293
294
    }
295
296
    /**
297
     * Select The range between two date object
298
     *
299
     * @param object $date_start Start of the DateTime
300
     * @param object $date_end   End of the DateTime
301
     *
302
     * @return object
303
     */
304
    public static function between($date_start, $date_end)
305
    {
306
307
        self::$date_start = $date_start;
308
309
        self::$date_end = $date_end;
310
311
        self::$call_type = 'between';
312
313
        return new Datium();
314
315
    }
316
317
    /**
318
     * Convert from current calendar, what type is current calendar?
319
     *
320
     * @param object $calendar Assigned calendar to start from
321
     *
322
     * @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...
323
     */
324 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...
325
    {
326
327
        $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...
328
329
        $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...
330
331
332
        /**
333
         * We need this part for DayOf class
334
         */
335
        $this->calendar_type = $calendar;
336
337
        $this->translate_to = $calendar;
338
339
        return $this;
340
341
    }
342
343
    /**
344
     * Convert date to current Date
345
     *
346
     * @param object $calendar Assigned calendar to when calendar should start.
347
     *
348
     * @return object
349
     */
350 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...
351
    {
352
353
        $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...
354
355
        $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...
356
357
        /**
358
         * We need this part for DayOf class
359
         */
360
        $this->calendar_type = $calendar;
361
362
        $this->translate_to = $calendar;
363
364
        return $this;
365
366
    }
367
368
    /**
369
     * Difference between two time
370
     *
371
     * @param DateTime $start Start of the date
372
     * @param DateTime $end   End of the date
373
     *
374
     * @return object
375
     */
376
    public static function diff($start, $end)
377
    {
378
379
        $difference = date_diff($start, $end);
380
381
        $difference->second = $difference->s;
382
383
        $difference->minute = $difference->i;
384
385
        $difference->hour = $difference->h;
386
387
        $difference->day = $difference->d;
388
389
        $difference->month = $difference->m;
390
391
        $difference->year = $difference->y;
392
393
        $difference->simple = ( new SimpleDiff( $start, $end, $difference ) );
394
395
        return $difference;
396
397
    }
398
399
    /**
400
     * Add new date value to current date
401
     *
402
     * @param string $value How much date should be added to current date
403
     *
404
     * @return object
405
     */
406 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...
407
    {
408
409
        $this->date_interval_expression = str_replace(
410
            $this->config['date_simple'],
411
            $this->config['date_interval'],
412
            $value
413
        );
414
415
        $unit = 'P';
416
417
        if (strpos($this->date_interval_expression, 'T')) {
418
            $this->date_interval_expression = str_replace(
419
                'T',
420
                '',
421
                $this->date_interval_expression
422
            );
423
424
            $unit = 'PT';
425
        }
426
427
        $this->date_interval_expression = str_replace(
428
            ' ',
429
            '',
430
            $unit.$this->date_interval_expression
431
        );
432
433
        $this->date_time->add(
434
            new DateInterval($this->date_interval_expression)
435
        );
436
437
        $this->gregorian_DayofWeek = $this->date_time->format('w');
438
439
        return $this;
440
441
    }
442
443
    /**
444
     * Sub date from current date
445
     *
446
     * @param string $value How much date should increase from current date
447
     *
448
     * @return obejct
449
     */
450 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...
451
    {
452
453
        $this->date_interval_expression = str_replace(
454
            $this->config['date_simple'],
455
            $this->config['date_interval'],
456
            $value
457
        );
458
459
        $unit = 'P';
460
461
        if (strpos($this->date_interval_expression, 'T')) {
462
            $this->date_interval_expression = str_replace(
463
                'T',
464
                '',
465
                $this->date_interval_expression
466
            );
467
468
            $unit = 'PT';
469
        }
470
471
        $this->date_interval_expression = str_replace(
472
            ' ',
473
            '',
474
            $unit.$this->date_interval_expression
475
        );
476
477
        $this->date_time->sub(
478
            new DateInterval($this->date_interval_expression)
479
        );
480
481
        $this->gregorian_DayofWeek = $this->date_time->format('w');
482
        
483
        return $this;
484
485
    }
486
487
    /**
488
     * Check if current year is leap or not
489
     *
490
     * @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...
491
     *
492
     * @return boolean
493
     */
494
    public function leap()
495
    {
496
497
        $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...
498
499
        return $this->leap;
500
501
    }
502
503
    /**
504
     * Caculate day of year or week
505
     *
506
     * @since Aug, 22 2015
507
     *
508
     * @return integer
509
     */
510
    public function dayOf()
511
    {
512
513
        $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...
514
515
        return $this->day_of;
516
517
    }
518
519
    // 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...
520
    // {
521
    //
522
    //     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...
523
    //
524
    //         $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...
525
    //
526
    //     } 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...
527
    //
528
    //         $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...
529
    //
530
    //     }
531
    //
532
    //     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...
533
    //
534
    // }
535
536
    /**
537
     * Return Datetime as a original object
538
     *
539
     * @since Oct 22, 2015
540
     *
541
     * @return object
542
     */
543
    public function object()
544
    {
545
546
        return $this->date_time;
547
548
    }
549
550
    /**
551
     * Translate current date string to selected language
552
     *
553
     * @param string $language language short name fa, en, ar ...
554
     *
555
     * @return object
556
     */
557
    public function lang($language = 'fa')
558
    {
559
560
        $this->language = $language;
561
562
        $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...
563
564
        $this->result->setConfig($this->language);
565
566
        $this->toConfig = $this->result->getConfig();
567
568
        return $this;
569
570
    }
571
572
    /**
573
     * Return object as timestamp
574
     *
575
     * @return timestamp
576
     */
577
    public function timestamp()
578
    {
579
580
        return strtotime($this->date_time->format('Y-m-d H:i:s'));
581
582
    }
583
584
    /**
585
     * Return fainal result
586
     *
587
     * @param string $format Date format
588
     *
589
     * @since Aug 17 2015
590
     *
591
     * @return string
592
     */
593
    public function get($format = 'Y-m-d H:i:s')
594
    {
595
596
        if ($format === 'timestamp') {
597
598
          return $this->timestamp();
599
600
        }
601
602
        if (is_null($this->fromConfig)) {
603
            $this->fromConfig = include __DIR__.'/CalendarSettings/'.
604
                                        ucfirst($this->translate_from).'.php';
605
        }
606
607
608
        if (is_null($this->toConfig)) {
609
            $this->toConfig = include __DIR__.'/CalendarSettings/'.
610
                                      ucfirst($this->translate_to).'.php';
611
        }
612
613
        $string_date = $this->date_time->format($format);
614
615
        if ($this->translate_to != 'gregorian') {
616
            $string_date = str_replace(
617
                $this->fromConfig['month'],
618
                $this->toConfig['month'],
619
                $string_date
620
            );
621
622
            $string_date = str_replace(
623
                $this->fromConfig['days_of_week'],
624
                $this->toConfig['days_of_week'][$this->gregorian_DayofWeek],
625
                $string_date
626
            );
627
628
            $string_date = str_replace(
629
                $this->fromConfig['numbers'],
630
                $this->toConfig['numbers'],
631
                $string_date
632
            );
633
634
            $string_date = str_replace(
635
                $this->fromConfig['am_time'],
636
                $this->toConfig['am_time'],
637
                $string_date
638
            );
639
640
            $string_date = str_replace(
641
                $this->fromConfig['pm_time'],
642
                $this->toConfig['pm_time'],
643
                $string_date
644
            );
645
646
            $string_date = str_replace(
647
                $this->fromConfig['end_of_days'],
648
                $this->toConfig['end_of_days'],
649
                $string_date
650
            );
651
        }
652
653
        return $string_date;
654
655
    }
656
}
657