Test Failed
Push — dev ( 6beeb4...c21d30 )
by Plamen
02:05
created

TraitHelper::filterInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
trait TraitHelper
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
18
    public static function assets($path = '/public/table')
19
    {
20
        return "<script src=\"{$path}/table_helper.js\" defer></script>\n\t".
21
            "<script src=\"{$path}/table.js\" defer></script>\n\t".
22
            "<link href=\"{$path}/table.css\" rel=\"stylesheet\">\n";
23
    }
24
25
    /** Array to space separated key value list
26
     * @param array $attributes
27
     *
28
     * @return string
29
     */
30
    protected static function attributes(array $attributes)
31
    {
32
        $list = [' '];
33
        foreach ($attributes as $key => $value) {
34
            if ($value === true || (empty($value) && $value != 0)) {
35
                $list[] = $key;
36
            } else {
37
                $list[] = $key.'="'.$value.'"';
38
            }
39
        }
40
41
        return rtrim(implode(' ', $list));
42
    }
43
44
    /** 
45
     * Parses view to string
46
     * @param string $template
47
     * @param array  $vars
48
     *
49
     * @return string
50
     */
51
    protected static function view($template, $vars = [])
52
    {
53
        extract($vars);
54
        ob_start();
55
        require $template;
56
57
        return (string) ob_get_clean();
58
    }
59
60
    /** 
61
     * Needed for more than one table on page
62
     * @param string $items
63
     */
64
    protected static function reset($items)
65
    {
66
        if (!in_array($items, self::$t['tables'])) {
67
            self::$t['tables'][] = $items;
68
            self::$cols = [];
69
            self::$t['rows'] = null;
70
        } else {
71
            echo 'Existing table-id used in table::create(): '.$items;
72
        }
73
    }
74
75
    /**
76
     * Tests value protected value checker.
77
     */
78
    public static function confirmValue($property, $value)
79
    {
80
        try {
81
            $nodes = explode('.', $property);
82
            $name = array_shift($nodes);
83
            if (property_exists(__CLASS__, $name)) {
84
                $val = function(&$val) use ($nodes) {
85
                    $temp = &$val;
86
    				foreach($nodes as $key) {
87
                        if ($key === 'last') {
88
                            return array_pop($temp);
89
                        }
90
    					$temp = &$temp[$key];
91
    				}
92
93
    				return $temp;
94
                };
95
96
                if (($v = $val(static::$$name)) !== null) {
97
                    return $v === $value;
98
                }
99
100
                throw new Exception('Missing value ('.implode($nodes).').');
101
            }
102
103
            throw new Exception('Undefined property ('.$name.').');
104
        } catch (Exception $e) {
105
            echo 'Caught exception: ',  $e->getMessage(), "\n";
106
        }
107
108
        return false;
109
    }
110
    
111
    /** 
112
     * Executes filter_input($type, $variable_name)
113
     * 
114
     * @param int $type
115
     * @param string $variable_name
116
     * @return mixed
117
     */
118
    protected static function filterInput(int $type, string $variable_name){
119
        return filter_input($type, $variable_name);
120
    }
121
}
122