Completed
Push — master ( 9d2442...b1f272 )
by Denis
01:49
created

GridViewHelper   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 93
rs 10
c 0
b 0
f 0
wmc 17
lcom 0
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A loadConfig() 0 9 3
B testConfig() 0 46 10
A htmlOptionsToString() 0 14 3
1
<?php
2
3
namespace Woo\GridView;
4
5
use Woo\GridView\Exceptions\GridViewConfigException;
6
7
class GridViewHelper
8
{
9
    private function __construct() {}
10
11
    /**
12
     * Allows to load config into object properties
13
     * @param object $object
14
     * @param array $config
15
     * @throws GridViewConfigException
16
     */
17
    public static function loadConfig($object, array $config)
18
    {
19
        foreach ($config as $key => $value) {
20
21
            if (property_exists($object, $key)) {
22
                $object->$key = $value;
23
            }
24
        }
25
    }
26
27
    /**
28
     * Allows to test attributes and types in config
29
     * @param $object
30
     * @param array $tests
31
     * @throws GridViewConfigException
32
     */
33
    public static function testConfig($object, array $tests)
34
    {
35
        foreach ($tests as $property => $test) {
36
37
            if (!property_exists($object, $property)) {
38
                throw new GridViewConfigException(
39
                    'Unable to test ' . get_class($object) . ': property ' . $property . ' does not exist'
40
                );
41
            }
42
43
            if (is_scalar($test)) {
44
45
                $testPassed = true;
46
47
                switch ($test) {
48
                    case 'int':
49
                        $testPassed = is_numeric($object->$property);
50
                        break;
51
52
                    case 'string':
53
                        $testPassed = is_string($object->$property);
54
                        break;
55
56
                    case 'array':
57
                        $testPassed = is_array($object->$property);
58
                        break;
59
60
                    case 'closure':
61
                        $testPassed = $object->$property instanceof \Closure;
62
                        break;
63
64
                    case 'any':
65
                        break;
66
67
                    default:
68
                        $testPassed = is_subclass_of($object->$property, $test);
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $test can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
69
                }
70
71
                if (!$testPassed) {
72
                    throw new GridViewConfigException('
73
                        Test ' . $test . ' has failed on ' . get_class($object) . '::' . $property
74
                    );
75
                }
76
            }
77
        }
78
    }
79
80
    /**
81
     * Allows to convert options array to html string
82
     * @param array $htmlOptions
83
     * @return string
84
     */
85
    public static function htmlOptionsToString(array $htmlOptions) : string
86
    {
87
        if (empty($htmlOptions)) {
88
            return '';
89
        }
90
91
        $out = [];
92
93
        foreach ($htmlOptions as $k => $v) {
94
            $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"';
95
        }
96
97
        return implode(' ', $out);
98
    }
99
}