Test Failed
Push — main ( b6e144...1a2341 )
by Rafael
05:34
created

RegionalInfo::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
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
 * @author  Rafael San José Tovar <[email protected]>
16
 *
17
 * @package Alxarafe\Core\Singletons
18
 */
19
abstract class RegionalInfo
20
{
21
    /**
22
     * Contiene la información regional obtenida del fichero de configuración,
23
     * o en su defecto, los valores por defecto de la aplicación.
24
     *
25
     * @var array
26
     */
27
    public static array $config;
28
29
    /**
30
     * Carga los datos de configuración regional en $config
31
     *
32
     * @author Rafael San José Tovar <[email protected]>
33
     *
34
     */
35
    public static function load()
36
    {
37
        $default = self::getDefaultValues();
38
        $config = Config::getModuleVar('RegionalInfo');
39
        foreach ($default as $var => $value) {
40
            self::$config[$var] = $config[$var] ?? $value;
41
        }
42
    }
43
44
    /**
45
     * Obtiene los valores por defecto para las variables regionales
46
     *
47
     * @author Rafael San José Tovar <[email protected]>
48
     *
49
     * @return string[]
50
     */
51
    private static function getDefaultValues(): array
52
    {
53
        return [
54
            'dateFormat' => 'Y-m-d',
55
            'timeFormat' => 'H:i:s',
56
            'datetimeFormat' => 'Y-m-d H:i:s',
57
            'timezone' => 'Europe/Madrid',
58
        ];
59
    }
60
61
    /**
62
     * Obtiene un listado de formatos de fecha
63
     *
64
     * @author Rafael San José Tovar <[email protected]>
65
     *
66
     * @return array
67
     */
68
    private static function getDateFormats(): array
0 ignored issues
show
Unused Code introduced by
The method getDateFormats() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
69
    {
70
        $styles = [
71
            'Y-m-d',
72
            'Y-m-j',
73
            'Y-M-d',
74
            'Y-M-j',
75
            'd-m-Y',
76
            'j-m-Y',
77
            'd-M-Y',
78
            'j-M-Y',
79
            'm-d-Y',
80
            'm-j-Y',
81
            'M-d-Y',
82
            'M-j-Y',
83
        ];
84
        return self::fillList($styles);
85
    }
86
87
    /**
88
     * Retorna un array asociativo de estilos con el texto formateado como valor.
89
     *
90
     * @author Rafael San José Tovar <[email protected]>
91
     *
92
     * @param $styles
93
     *
94
     * @return array
95
     */
96
    private static function fillList($styles): array
97
    {
98
        $result = [];
99
        foreach ($styles as $style) {
100
            $result[$style] = self::getFormatted($style);
101
        }
102
        return $result;
103
    }
104
105
    /**
106
     * Retorna una cadena formateada para un instante dado
107
     *
108
     * @author Rafael San José Tovar <[email protected]>
109
     *
110
     * @param string $style
111
     * @param string $time
112
     *
113
     * @return false|string
114
     */
115
    private static function getFormatted(string $style, string $time = '2011-01-07 09:08:07')
116
    {
117
        //        return FormatUtils::getFormatted($style, $time);
118
        try {
119
            $time = ($time === '') ? 'now' : $time;
120
            $date = (new DateTime($time))->format($style);
121
        } catch (Exception $e) {
122
            $time = ($time === '') ? 'time()' : $time;
123
            $date = date($style, strtotime($time));
124
        }
125
        return $date;
126
    }
127
128
    /**
129
     * Retorna distintos formatos de tiempo
130
     *
131
     * @author Rafael San José Tovar <[email protected]>
132
     *
133
     * @return array
134
     */
135
    private static function getTimeFormats(): array
0 ignored issues
show
Unused Code introduced by
The method getTimeFormats() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
136
    {
137
        $styles = [
138
            'H:i',
139
            'H:i:s',
140
            'h:i A',
141
            'h:i:s A',
142
        ];
143
        return self::fillList($styles);
144
    }
145
}
146