Passed
Branchmaster (1bd863)
by Plamen
01:24
created

trait_tfoot_setter::config()   B

Complexity

Conditions 11
Paths 8

Size

Total Lines 44
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 31
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 44
rs 7.3166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
trait trait_tfoot_setter
4
{
5
    /** @param array Columns (config) */
6
    public static $cols;
7
    /** @param array Query result, allows altering before table::load() */
8
    public static $data = [];
9
    /** @param bool Export flag (for current request) */
10
    public static $export;
11
    /** @param bool Export is allowed (shows buttons in the tfoot) */
12
    public static $exportActive = true;
13
    /** @param bool Export data altered (use false for pure result) */
14
    public static $exportDataAsDisplayed = true;
15
    /** @param array Collector (values for current table call) */
16
    protected static $t = ['page' => 1, 'tables' => []];
17
    /** @param array Re-settable values for current table call */
18
    protected static $config;
19
    /** @param array Errors collector */
20
    protected static $errors = [];
21
22
    static function assets($path = "/public/table")
23
    {
24
        return "<script src=\"{$path}/table.js\"></script>\n\t" .
25
                "<link href=\"{$path}/table.css\" rel=\"stylesheet\">\n";
26
    }
27
28
    /** Set/Get config value
29
     * @param mixed $c (string) Get if exists, (array) Set if valid
30
     * @return mixed */
31
    public static function config($c)
32
    {
33
        self::$config = self::$config ?: [
34
            'APP' => 'App',
35
            'DB_COLLATION_CI' => 'utf8mb4_general_ci', //UTF8_GENERAL_CI
36
            'EMPTY_TABLE_MSG' => 'No results found.',
37
            'EXPORT_FILE_NAME' => ':app - :items export (:datetime)',
38
            'FILTER_ACTIVE' => true,
39
            'FILTER_CASE_SENSITIVE' => false,
40
            'SAVES' => ['CSV', 'Excel'],
41
            'UTF8_ASC_SYMBOL' => '&#9650;',
42
            'UTF8_DESC_SYMBOL' => '&#9660;',
43
            'UTF8_LEFT_SYMBOL' => '&lsaquo;',
44
            'UTF8_RIGHT_SYMBOL' => '&rsaquo;'
45
        ];
46
47
        $getValid = function($k, $v = null) {
48
            if (!array_key_exists($k, self::$config)) {
49
                throw new Exception('Request to undefined value: ' . $k);
50
            }
51
52
            if ($v !== null) {
53
                if (empty($v) || gettype($v) !== gettype(self::$config[$k])) {
54
                    throw new Exception("Setting invalid value: $v (:$k)");
55
                }
56
            }
57
58
            return $v === null ? self::$config[$k] : $v;
59
        };
60
61
        try {
62
            switch (gettype($c)) {
63
                case 'array';
64
                    foreach ($c as $k => $v) {
65
                        self::$config[$k] = $getValid((string) $k, $v);
66
                    }
67
                    break;
68
                case 'string':
69
                    return $getValid($c);
70
                default:
71
                    throw new Exception("Invalid value type.");
72
            }
73
        } catch (Exception $e) {
74
            self::err('ERROR: ' . $e->getMessage());
75
        }
76
    }
77
    
78
    /** Adds conditions, orderBy and Limits to query.
79
     * @param string $q
80
     * @param mixed $cond
81
     * @param array $order
82
     * @param mixed $limit -> 5 or [$start, $count]
83
     * @param bool $h Having flag - to use HAVING instead of WHERE
84
     * @return (string) */
85
    protected static function q($q, $cond = null, array $order = [], $limit = 0, $h = false)
86
    {
87
        //Condition: '' | '(HAVING|WHERE|AND) ' . $cond
88
        $c = !empty($cond) && !strpos($q, ($cl = !$h ? 'WHERE' : 'HAVING')) ?
89
                (' ' . (!strpos($q, $cl) ? $cl : ' AND ') . ' ' . $cond ) :
90
                '';
91
        //Order: '' | 'ORDER BY ' . array_keys($order)[0] . ' ' . $order[0]
92
        $o = !empty($order) ?
93
                ' ORDER BY ' . implode(', ',
94
                        array_map(function(&$v, $k) {
95
                            return $k . ' ' . strtoupper($v);
96
                        }, $order, array_keys($order))
97
                ) :
98
                '';
99
        //Limit: '' | ' LIMIT ' . '(20, 40|20)'
100
        $l = !empty($limit) ?
101
                (' LIMIT ' . (is_array($limit) ? implode(', ', $limit) : $limit)) :
102
                '';
103
104
        return $q . $c . $o . $l;
105
    }
106
107
    protected static function err($message = null)
108
    {
109
        if (isset($message)) {
110
            self::$errors[] = $message;
111
        } else {
112
            return empty(self::$errors) ? null :
113
            '<p class="tbl-err">' . implode('</br>', self::$errors) . '</p>';
114
        }
115
    }
116
117
    /** Converts array to space separated key value list
118
     * @param array $attributes
119
     * @return string */
120
    protected static function attributes(array $attributes)
121
    {
122
        $list = [];
123
        foreach ($attributes as $key => $value) {
124
            if (is_bool($value)) {
125
                if ((bool) $value) {
126
                    $list[] = $key;
127
                }
128
            } else if (empty($value) && !is_int($value)) {
129
                $list[] = $key;
130
            } else {
131
                $list[] = $key . '="' . $value . '"';
132
            }
133
        }
134
        return (count($list) > 0 ? ' ' : '') . join(' ', $list);
135
    }
136
137
    /** Parses view to string
138
     * @param string $template
139
     * @param array $vars
140
     * @return string */
141
    protected static function view($template, $vars = [])
142
    {
143
        extract($vars);
144
        ob_start();
145
        require $template;
146
        return (string) ob_get_clean();
147
    }
148
}
149