Passed
Push — dev ( bd133d...5cccb4 )
by Plamen
02:10
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
    /** 
26
     * Array to space separated key value list.
27
     * @param array $attributes
28
     *
29
     * @return string
30
     */
31
    protected static function attributes(array $attributes)
32
    {
33
        $list = [' '];
34
        foreach ($attributes as $key => $value) {
35
            if ($value === true || (empty($value) && $value != 0)) {
36
                $list[] = $key;
37
            } else {
38
                $list[] = $key.'="'.$value.'"';
39
            }
40
        }
41
42
        return rtrim(implode(' ', $list));
43
    }
44
45
    /**
46
     * Parses view to string.
47
     *
48
     * @param string $template
49
     * @param array  $vars
50
     *
51
     * @return string
52
     */
53
    protected static function view($template, $vars = [])
54
    {
55
        extract($vars);
56
        ob_start();
57
        require $template;
58
59
        return (string) ob_get_clean();
60
    }
61
62
    /** 
63
     * Needed for more than one table on page.
64
     *
65
     * @param string $items
66
     */
67
    protected static function reset($items)
68
    {
69
        if (!in_array($items, self::$t['tables'])) {
70
            self::$t['tables'][] = $items;
71
            self::$cols = [];
72
            self::$t['rows'] = null;
73
        } else {
74
            echo 'Existing table-id used in table::create(): '.$items;
75
        }
76
    }
77
78
    /**
79
     * Tests value protected value checker.
80
     */
81
    public static function confirmValue($property, $value)
82
    {
83
        try {
84
            $nodes = explode('.', $property);
85
            $name = array_shift($nodes);
86
            if (property_exists(__CLASS__, $name)) {
87
                $val = function (&$val) use ($nodes) {
88
                    $temp = &$val;
89
                    foreach ($nodes as $key) {
90
                        if ($key === 'last') {
91
                            return array_pop($temp);
92
                        }
93
                        $temp = &$temp[$key];
94
                    }
95
96
                    return $temp;
97
                };
98
99
                if (($v = $val(static::$$name)) !== null) {
100
                    return $v === $value;
101
                }
102
103
                throw new Exception('Missing value ('.implode($nodes).').');
104
            }
105
106
            throw new Exception('Undefined property ('.$name.').');
107
        } catch (Exception $e) {
108
            echo 'Caught exception: ',  $e->getMessage(), "\n";
109
        }
110
111
        return false;
112
    }
113
}
114