1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of sensorario/resources repository |
5
|
|
|
* |
6
|
|
|
* (c) Simone Gentili <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sensorario\Resources; |
13
|
|
|
|
14
|
|
|
use Sensorario\Resources\Interfaces\ResourceInterface; |
15
|
|
|
use Sensorario\Resources\Rulers\Rule; |
16
|
|
|
|
17
|
|
|
class Resource extends MagicResource implements ResourceInterface |
18
|
|
|
{ |
19
|
|
|
protected $allowed = []; |
20
|
|
|
|
21
|
|
|
protected $allowedValues = []; |
22
|
|
|
|
23
|
|
|
protected $mandatory = []; |
24
|
|
|
|
25
|
|
|
protected $defaults = []; |
26
|
|
|
|
27
|
|
|
protected $rules = []; |
28
|
|
|
|
29
|
|
|
protected $rewrites = []; |
30
|
|
|
|
31
|
|
|
protected $ranges = []; |
32
|
|
|
|
33
|
28 |
|
public function mandatory() |
34
|
|
|
{ |
35
|
28 |
|
return $this->mandatory; |
36
|
|
|
} |
37
|
|
|
|
38
|
22 |
|
public function allowed() |
39
|
|
|
{ |
40
|
22 |
|
return $this->allowed; |
41
|
|
|
} |
42
|
|
|
|
43
|
17 |
|
public function allowedValues() |
44
|
|
|
{ |
45
|
17 |
|
return $this->allowedValues; |
46
|
|
|
} |
47
|
|
|
|
48
|
28 |
|
public function rules() |
49
|
|
|
{ |
50
|
28 |
|
return $this->rules; |
51
|
|
|
} |
52
|
|
|
|
53
|
4 |
|
public function defaults() |
54
|
|
|
{ |
55
|
4 |
|
return $this->defaults; |
56
|
|
|
} |
57
|
|
|
|
58
|
19 |
|
public function rewrites() |
59
|
|
|
{ |
60
|
19 |
|
return $this->rewrites; |
61
|
|
|
} |
62
|
|
|
|
63
|
19 |
|
public function ranges() |
64
|
|
|
{ |
65
|
19 |
|
return $this->ranges; |
66
|
|
|
} |
67
|
|
|
|
68
|
25 |
|
public function applyConfiguration( |
69
|
|
|
Configurator $configurator |
70
|
|
|
) { |
71
|
25 |
|
$this->allowed = $configurator->allowed(); |
72
|
25 |
|
$this->mandatory = $configurator->mandatory(); |
73
|
25 |
|
$this->defaults = $configurator->defaults(); |
74
|
25 |
|
$this->rules = $configurator->rules(); |
75
|
25 |
|
$this->allowedValues = $configurator->allowedValues(); |
76
|
25 |
|
$this->rewrites = $configurator->rewrites(); |
77
|
25 |
|
$this->ranges = $configurator->ranges(); |
78
|
|
|
|
79
|
25 |
|
if ($configurator->globals()) { |
80
|
1 |
|
$globals = $configurator->globals(); |
81
|
1 |
|
if (isset($globals['allowed'])) { |
82
|
1 |
|
$this->allowed = array_merge( |
83
|
1 |
|
$this->allowed, |
84
|
1 |
|
$globals['allowed'] |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
25 |
|
} |
89
|
|
|
|
90
|
32 |
|
public function isRuleDefinedFor($ruleName) |
91
|
|
|
{ |
92
|
32 |
|
return isset($this->rules()[$ruleName]); |
93
|
|
|
} |
94
|
|
|
|
95
|
32 |
|
public function isRuleNotDefinedFor($ruleName) |
96
|
|
|
{ |
97
|
32 |
|
return !$this->isRuleDefinedFor($ruleName); |
98
|
|
|
} |
99
|
|
|
|
100
|
12 |
|
public function getRule($ruleName) : Rule |
101
|
|
|
{ |
102
|
12 |
|
return Rule::fromArray( |
103
|
12 |
|
$this->rules()[$ruleName] |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
public function isFieldNumeric($fieldName) |
108
|
|
|
{ |
109
|
2 |
|
return is_numeric($this->get($fieldName)); |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
public function isFieldString($fieldName) |
113
|
|
|
{ |
114
|
1 |
|
return is_string($this->get($fieldName)); |
115
|
|
|
} |
116
|
|
|
|
117
|
2 |
|
public function isFieldNumericButString($fieldName) |
118
|
|
|
{ |
119
|
2 |
|
return $this->isFieldNumeric($fieldName) |
120
|
2 |
|
&& $this->isFieldString($fieldName); |
121
|
|
|
} |
122
|
|
|
|
123
|
6 |
|
public function getFieldType($fieldName) |
124
|
|
|
{ |
125
|
6 |
|
return gettype($this->get($fieldName)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|