|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Woo\GridView\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Woo\GridView\Exceptions\GridViewConfigException; |
|
7
|
|
|
|
|
8
|
|
|
trait Configurable |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Allows to load config into object properties |
|
12
|
|
|
* @param array $config |
|
13
|
|
|
* @throws GridViewConfigException |
|
14
|
|
|
*/ |
|
15
|
|
|
public function loadConfig(array $config) |
|
16
|
|
|
{ |
|
17
|
|
|
foreach ($config as $key => $value) { |
|
18
|
|
|
|
|
19
|
|
|
if (property_exists($this, $key)) { |
|
20
|
|
|
$this->$key = $value; |
|
21
|
|
|
} |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
$this->testConfig(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Should specify tests |
|
29
|
|
|
* @return array |
|
30
|
|
|
*/ |
|
31
|
|
|
abstract protected function configTests() : array; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Allows to test attributes and types in config |
|
35
|
|
|
* @throws GridViewConfigException |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function testConfig() |
|
38
|
|
|
{ |
|
39
|
|
|
foreach ($this->configTests() as $property => $tests) { |
|
40
|
|
|
|
|
41
|
|
|
if (!property_exists($this, $property)) { |
|
42
|
|
|
throw new GridViewConfigException( |
|
43
|
|
|
'Unable to test ' . get_class($this) . ': property ' . $property . ' does not exist' |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$testPassed = true; |
|
48
|
|
|
$testMessage = 'Validation failed'; |
|
49
|
|
|
|
|
50
|
|
|
foreach (explode('|', $tests) as $test) { |
|
51
|
|
|
|
|
52
|
|
|
switch ($test) { |
|
53
|
|
|
case 'int': |
|
54
|
|
|
$testPassed = is_numeric($this->$property); |
|
55
|
|
|
$testMessage = 'Property should be numeric'; |
|
56
|
|
|
break; |
|
57
|
|
|
|
|
58
|
|
|
case 'string': |
|
59
|
|
|
$testPassed = is_string($this->$property); |
|
60
|
|
|
$testMessage = 'Property should be a string'; |
|
61
|
|
|
break; |
|
62
|
|
|
|
|
63
|
|
|
case 'array': |
|
64
|
|
|
$testPassed = is_array($this->$property); |
|
65
|
|
|
$testMessage = 'Property should be an array'; |
|
66
|
|
|
break; |
|
67
|
|
|
|
|
68
|
|
|
case 'closure': |
|
69
|
|
|
$testPassed = $this->$property instanceof Closure; |
|
70
|
|
|
$testMessage = 'Property should be a valid callback (Closure instance)'; |
|
71
|
|
|
break; |
|
72
|
|
|
|
|
73
|
|
|
case 'boolean': |
|
74
|
|
|
$testPassed = is_bool($this->$property); |
|
75
|
|
|
$testMessage = 'Property should be boolean'; |
|
76
|
|
|
break; |
|
77
|
|
|
|
|
78
|
|
|
case 'any': |
|
79
|
|
|
break; |
|
80
|
|
|
|
|
81
|
|
|
default: |
|
82
|
|
|
$testPassed = $testPassed || is_a($this->$property, $test) || is_subclass_of($this->$property, $test); |
|
|
|
|
|
|
83
|
|
|
$testMessage = 'Property should be ' . $test . ' instance/class reference, got ' . print_r($this->$property, 1); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if (!$testPassed) { |
|
88
|
|
|
throw new GridViewConfigException( |
|
89
|
|
|
'Tests ' . $tests . ' has failed on ' . get_class($this) . '::' . $property . ': ' . $testMessage |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |