Datium::dayOf()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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