Test Failed
Push — main ( ad999b...e26c51 )
by Rafael
06:04
created

RegionalInfo::getFormatted()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
nc 6
nop 2
dl 0
loc 11
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Alxarafe. Development of PHP applications in a flash!
4
 * Copyright (C) 2018-2020 Alxarafe <[email protected]>
5
 */
6
7
namespace Alxarafe\Core\Singletons;
8
9
use DateTime;
10
use Exception;
11
12
/**
13
 * Class RegionalInfo
14
 *
15
 * @package Alxarafe\Core\Providers
16
 */
17
class RegionalInfo
18
{
19
    public static $config;
20
21
    public function __construct()
22
    {
23
        self::getConfig();
24
    }
25
26
    private static function getConfig()
27
    {
28
        $default = self::getDefaultValues();
29
        foreach ($default as $var => $value) {
30
            self::$config[$var] = Config::getVar('RegionalInfo', 'main', $var) ?? $value;
31
        }
32
    }
33
34
    /**
35
     * Returns a list of date formats
36
     */
37
    public static function getDateFormats(): array
38
    {
39
        $styles = [
40
            'Y-m-d',
41
            'Y-m-j',
42
            'Y-M-d',
43
            'Y-M-j',
44
            'd-m-Y',
45
            'j-m-Y',
46
            'd-M-Y',
47
            'j-M-Y',
48
            'm-d-Y',
49
            'm-j-Y',
50
            'M-d-Y',
51
            'M-j-Y',
52
        ];
53
        return self::fillList($styles);
54
    }
55
56
    /**
57
     * Fill list with key => value, where key is style and value a sample.
58
     *
59
     * @param array $styles
60
     *
61
     * @return array
62
     */
63
    private static function fillList($styles): array
64
    {
65
        $result = [];
66
        foreach ($styles as $style) {
67
            $result[$style] = self::getFormatted($style);
68
        }
69
        return $result;
70
    }
71
72
    /**
73
     * Return formatted string.
74
     *
75
     * @param string $style
76
     * @param string $time
77
     *
78
     * @return false|string
79
     */
80
    private static function getFormatted(string $style, string $time = '2011-01-07 09:08:07')
81
    {
82
        //        return FormatUtils::getFormatted($style, $time);
83
        try {
84
            $time = ($time === '') ? 'now' : $time;
85
            $date = (new DateTime($time))->format($style);
86
        } catch (Exception $e) {
87
            $time = ($time === '') ? 'time()' : $time;
88
            $date = date($style, strtotime($time));
89
        }
90
        return $date;
91
    }
92
93
    /**
94
     * Returns a list of time formats
95
     */
96
    public static function getTimeFormats(): array
97
    {
98
        $styles = [
99
            'H:i',
100
            'H:i:s',
101
            'h:i A',
102
            'h:i:s A',
103
        ];
104
        return self::fillList($styles);
105
    }
106
107
    /**
108
     * Return default values
109
     *
110
     * @return array
111
     */
112
    public static function getDefaultValues(): array
113
    {
114
        return [
115
            'dateFormat' => 'Y-m-d',
116
            'timeFormat' => 'H:i:s',
117
            'datetimeFormat' => 'Y-m-d H:i:s',
118
            'timezone' => 'Europe/Madrid',
119
        ];
120
    }
121
}
122