DateHelper::getDefaultRange()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 2018-09-27
6
 */
7
8
namespace app\components\visualizations;
9
10
11
use app\components\ArrayHelper;
12
use Carbon\Carbon;
13
use Yii;
14
use yii\helpers\StringHelper;
15
16
class DateHelper
17
{
18
    public static $dateFormat = 'd.m.Y';
19
    public static $rangeSeparator = ' - ';
20
21
    public static function getDefaultRange()
22
    {
23
        return sprintf('%s%s%s',
24
            Carbon::now()->startOfMonth()->format(static::$dateFormat),
25
            static::$rangeSeparator,
26
            Carbon::now()->endOfMonth()->format(static::$dateFormat)
27
        );
28
    }
29
30
    public static function getRangeFromUrl(string $getParamName = 'date_range')
31
    {
32
        $dateRangeDefaultValue = static::getDefaultRange();
33
34
        return Yii::$app->request->get($getParamName, $dateRangeDefaultValue);
35
    }
36
37
    /**
38
     * @param string $dateRange
39
     * @return array|[Carbon, Carbon]  [start, end]
0 ignored issues
show
Documentation Bug introduced by
The doc comment array|[Carbon, Carbon] at position 2 could not be parsed: Unknown type name '[' at position 2 in array|[Carbon, Carbon].
Loading history...
40
     */
41
    public static function normalizeRange(string $dateRange)
42
    {
43
        $dateRange = StringHelper::explode($dateRange, static::$rangeSeparator, true);
44
        $start = ArrayHelper::getValue($dateRange, '0');
45
        if ($start) {
46
            $start = Carbon::createFromFormat(static::$dateFormat, $start)->startOfDay();
47
        }
48
        $end = ArrayHelper::getValue($dateRange, '1');
49
        if ($end) {
50
            $end = Carbon::createFromFormat(static::$dateFormat, $end)->endOfDay();
51
        }
52
53
        return [$start, $end];
54
    }
55
}