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 RuntimeException; |
15
|
|
|
use Sensorario\Resources\Validators\ResourcesValidator; |
16
|
|
|
|
17
|
|
|
class Resource |
18
|
|
|
extends MagicResource |
|
|
|
|
19
|
|
|
implements Interfaces\ResourceInterface |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
protected $allowed = []; |
22
|
|
|
|
23
|
|
|
protected $allowedValues = []; |
24
|
|
|
|
25
|
|
|
protected $mandatory = []; |
26
|
|
|
|
27
|
|
|
protected $defaults = []; |
28
|
|
|
|
29
|
|
|
protected $rules = []; |
30
|
|
|
|
31
|
|
|
protected $rewrites = []; |
32
|
|
|
|
33
|
|
|
protected $ranges = []; |
34
|
|
|
|
35
|
|
|
public function mandatory() |
36
|
|
|
{ |
37
|
|
|
return $this->mandatory; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function allowed() |
41
|
|
|
{ |
42
|
|
|
return $this->allowed; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function allowedValues() |
46
|
|
|
{ |
47
|
|
|
return $this->allowedValues; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function rules() |
51
|
|
|
{ |
52
|
|
|
return $this->rules; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function defaults() |
56
|
|
|
{ |
57
|
|
|
return $this->defaults; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function rewrites() |
61
|
|
|
{ |
62
|
|
|
return $this->rewrites; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function ranges() |
66
|
|
|
{ |
67
|
|
|
return $this->ranges; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function applyConfiguration( |
71
|
|
|
Configurator $configurator |
72
|
|
|
) { |
73
|
|
|
$this->allowed = $configurator->allowed(); |
|
|
|
|
74
|
|
|
$this->mandatory = $configurator->mandatory(); |
|
|
|
|
75
|
|
|
$this->defaults = $configurator->defaults(); |
|
|
|
|
76
|
|
|
$this->rules = $configurator->rules(); |
|
|
|
|
77
|
|
|
$this->allowedValues = $configurator->allowedValues(); |
|
|
|
|
78
|
|
|
$this->rewrites = $configurator->rewrites(); |
79
|
|
|
$this->ranges = $configurator->ranges(); |
|
|
|
|
80
|
|
|
|
81
|
|
|
if ($configurator->globals()) { |
82
|
|
|
$globals = $configurator->globals(); |
83
|
|
|
if (isset($globals['allowed'])) { |
84
|
|
|
$this->allowed = array_merge( |
85
|
|
|
$this->allowed, |
86
|
|
|
$globals['allowed'] |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function isRuleDefinedFor($ruleName) |
93
|
|
|
{ |
94
|
|
|
return isset($this->rules()[$ruleName]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function isRuleNotDefinedFor($ruleName) |
98
|
|
|
{ |
99
|
|
|
return !$this->isRuleDefinedFor($ruleName); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getRule($ruleName) : Rule |
103
|
|
|
{ |
104
|
|
|
return Rule::fromArray( |
105
|
|
|
$this->rules()[$ruleName] |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function isFieldNumeric($fieldName) |
110
|
|
|
{ |
111
|
|
|
return is_numeric($this->get($fieldName)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function isFieldString($fieldName) |
115
|
|
|
{ |
116
|
|
|
return is_string($this->get($fieldName)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function isFieldNumericButString($fieldName) |
120
|
|
|
{ |
121
|
|
|
return $this->isFieldNumeric($fieldName) |
122
|
|
|
&& $this->isFieldString($fieldName); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getFieldType($fieldName) |
126
|
|
|
{ |
127
|
|
|
return gettype($this->get($fieldName)); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|