Passed
Push — master ( af185e...b32849 )
by Plamen
01:30
created

trait_config::error()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
class trait_config
4
{
5
    /** @param array Re-settable values for current table call */
6
    protected static $config = [
7
        'APP' => 'App',
8
        'DB_COLLATION_CI' => 'utf8mb4_general_ci', //UTF8_GENERAL_CI
9
        'EMPTY_TABLE_MSG' => 'No results found.',
10
        'EXPORT_FILE_NAME' => ':app - :items export (:datetime)',
11
        'FILTER_ACTIVE' => true,
12
        'FILTER_CASE_SENSITIVE' => false,
13
        'SAVES' => ['CSV', 'Excel'],
14
        'UTF8_ASC_SYMBOL' => '&#9650;',
15
        'UTF8_DESC_SYMBOL' => '&#9660;',
16
        'UTF8_LEFT_SYMBOL' => '&lsaquo;',
17
        'UTF8_RIGHT_SYMBOL' => '&rsaquo;'
18
    ];
19
20
    /** Set/Get config value
21
     * @param mixed $value (string) Get if exists, (array) Set if valid
22
     * @return mixed */
23
    public static function config($value)
24
    {
25
        try {
26
            switch (gettype($value)) {
27
                case 'array':
28
                    foreach ($value as $k => $v) {
29
                        self::$config[$k] = self::valid((string) $k, $v);
30
                    }
31
                    break;
32
                case 'string':
33
                    return self::valid($value);
34
                default:
35
                    throw new Exception("Invalid value type.");
36
            }
37
        } catch (Exception $e) {
38
            self::error('ERROR: ' . $e->getMessage());
39
        }
40
    }
41
42
    protected static function error($message = null)
43
    {
44
        if (isset($message)) {
45
            self::$errors[] = $message;
0 ignored issues
show
Bug Best Practice introduced by
The property errors does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46
        } else {
47
            return empty(self::$errors) ? null :
48
                '<p class="tbl-err">' . implode('</br>', self::$errors) . '</p>';
49
        }
50
    }
51
52
    private static function valid($key, $val = null)
53
    {
54
        if (!array_key_exists($key, self::$config)) {
55
            throw new Exception('Request to undefined value: ' . $key);
56
        }
57
58
        if ($val !== null) {
59
            if (empty($val) || gettype($val) !== gettype(self::$config[$key])) {
60
                throw new Exception("Setting invalid value: $val (:$key)");
61
            }
62
        }
63
64
        return $val === null ? self::$config[$key] : $val;
65
    }
66
}
67