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\Rulers; |
13
|
|
|
|
14
|
|
|
use RuntimeException; |
15
|
|
|
use Sensorario\Resources\Resource; |
16
|
|
|
use Sensorario\Resources\Validators\Interfaces\Validator; |
17
|
|
|
use Egulias\EmailValidator\EmailValidator; |
18
|
|
|
use Egulias\EmailValidator\Validation\DNSCheckValidation; |
19
|
|
|
use Egulias\EmailValidator\Validation\MultipleValidationWithAnd; |
20
|
|
|
use Egulias\EmailValidator\Validation\RFCValidation; |
21
|
|
|
|
22
|
|
|
class Ruler |
23
|
|
|
{ |
24
|
|
|
private $resource; |
25
|
|
|
|
26
|
|
|
private $rule; |
27
|
|
|
|
28
|
|
|
private $fieldName; |
29
|
|
|
|
30
|
|
|
public function __construct(Resource $resource = null, $fieldName = null) |
31
|
|
|
{ |
32
|
|
|
$this->resource = $resource; |
33
|
|
|
$this->fieldName = $fieldName; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function fromResourceAndKey(Resource $resource, $fieldName) : Ruler |
37
|
|
|
{ |
38
|
|
|
return new static($resource, $fieldName); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getRuleFromResource() |
42
|
|
|
{ |
43
|
|
|
if (!$this->resource) { |
44
|
|
|
throw new \RuntimeException( |
45
|
|
|
'Oops! Cant get rule without resource' |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->rule = $this->resource->getRule($this->fieldName); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function isNotCustom() |
53
|
|
|
{ |
54
|
|
|
$this->getRuleFromResource(); |
55
|
|
|
|
56
|
|
|
return $this->rule->isNotCustom(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function typeMismatch() |
60
|
|
|
{ |
61
|
|
|
return $this->isNotCustom() |
62
|
|
|
&& $this->resource->getFieldType($this->fieldName) !== $this->rule->getRuleType(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function classMismatch() |
66
|
|
|
{ |
67
|
|
|
return $this->isNotCustom() |
68
|
|
|
&& !($this->resource->get($this->fieldName) instanceof \Sensorario\Resources\Resource) |
69
|
|
|
&& get_class($this->resource->get($this->fieldName)) != $this->rule->getValue(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function ensureTypeIsValid() |
73
|
|
|
{ |
74
|
|
|
if ($this->typeMismatch()) { |
75
|
|
|
if ($this->resource->isFieldNumericButString($this->fieldName)) { |
76
|
|
|
throw new \RuntimeException( |
77
|
|
|
'Property `'.$this->fieldName.'` must be an integer!' |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
throw new RuntimeException( |
82
|
|
|
'Attribute `' . $this->fieldName |
83
|
|
|
. '` must be of type ' |
84
|
|
|
. '`' . $this->rule->getExpectedType() . '`' |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function ensureClassIsValid() |
90
|
|
|
{ |
91
|
|
|
if ($this->classMismatch()) { |
92
|
|
|
if ($this->rule->isValueNotAnObject()) { |
93
|
|
|
throw new RuntimeException( |
94
|
|
|
'Attribute `' . $this->fieldName |
95
|
|
|
. '` must be an object of type ' . $this->rule->getValue() |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function ensureRuleTypeIsEmail() |
102
|
|
|
{ |
103
|
|
|
if (!$this->rule) { |
104
|
|
|
$this->getRuleFromResource(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($this->rule->isNotMail()) { |
108
|
|
|
throw new \RuntimeException( |
109
|
|
|
'Oops! `'. $this->rule->getRuleType() .'` custom validator is not available. Only email is.' |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|