Passed
Push — dev ( 712147...22d017 )
by Plamen
04:42
created

TraitHelper::confirmValue()   B

Complexity

Conditions 8
Paths 15

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 19
nc 15
nop 2
dl 0
loc 24
rs 8.4444
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
     * @return string */
28
    protected static function attributes(array $attributes)
29
    {
30
        $list = [' '];
31
        foreach ($attributes as $key => $value) {
32
            if ($value === true || (empty($value) && $value != 0)) {
33
                $list[] = $key;
34
            } else {
35
                $list[] = $key.'="'.$value.'"';
36
            }
37
        }
38
        return rtrim(implode(' ', $list));
39
    }
40
41
    /** Parses view to string
42
     * @param string    $template
43
     * @param array     $vars
44
     * @return string */
45
    protected static function view($template, $vars = [])
46
    {
47
        extract($vars);
48
        ob_start();
49
        require $template;
50
        return (string) ob_get_clean();
51
    }
52
53
    /** Needed for more than one table on page
54
     * @param string $items */
55
    protected static function reset($items)
56
    {
57
        if (!in_array($items, self::$t["tables"])) {
58
            self::$t["tables"][] = $items;
59
            self::$cols = [];
60
            self::$t['rows'] = null;
61
        } else {
62
            echo 'Existing table-id used in table::create(): '.$items;
63
        }
64
    }
65
    
66
    /**
67
     * Tests value protected value checker
68
     */
69
    public static function confirmValue($property, $value)
70
    {
71
        try {
72
            $p = explode('.', $property);
73
            $name = array_shift($p);
74
            if(property_exists(__CLASS__, $name)){
75
                switch(sizeof($p)){
76
                    case 3: return static::$$name[$p[0]][$p[1]][$p[2]] === $value;
77
                    case 2: return static::$$name[$p[0]][$p[1]] === $value;
78
                    case 1: return $p[0] === '∞' ? //request is to the last one
79
                        static::$$name[(sizeof(static::$$name)-1)] === $value : 
80
                        static::$$name[$p[0]] === $value;
81
                    case 0: return static::$$name === $value;
82
                    default: 
83
                        $oops = implode($p);
84
                        throw new Exception('Missing value (' . $oops . ').');
85
                }
86
            } else {
87
                throw new Exception('Undefined property ('.$name.').');
88
            }
89
        } catch (Exception $e) {
90
            echo 'Caught exception: ',  $e->getMessage(), "\n";
91
        }
92
        return false;
93
    }
94
}
95