|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Telkins\Validation; |
|
4
|
|
|
|
|
5
|
|
|
use OutOfBoundsException; |
|
6
|
|
|
use Telkins\Validation\Contracts\ResourceRuleSetContract; |
|
7
|
|
|
|
|
8
|
|
|
abstract class AbstractResourceRuleSet implements ResourceRuleSetContract |
|
9
|
|
|
{ |
|
10
|
|
|
public function rules() : array |
|
11
|
|
|
{ |
|
12
|
|
|
return $this->provideRules(); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
public function creationRules() : array |
|
16
|
|
|
{ |
|
17
|
|
|
return array_merge_recursive( |
|
18
|
|
|
$this->provideRules(), |
|
19
|
|
|
$this->provideCreationRules() |
|
20
|
|
|
); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function updateRules() : array |
|
24
|
|
|
{ |
|
25
|
|
|
return array_merge_recursive( |
|
26
|
|
|
$this->provideRules(), |
|
27
|
|
|
$this->provideUpdateRules() |
|
28
|
|
|
); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function fieldRules(string $field) : array |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->getFieldRules($field, $this->rules()); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function fieldCreationRules(string $field) : array |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->getFieldRules($field, $this->creationRules()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function fieldUpdateRules(string $field) : array |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->getFieldRules($field, $this->updateRules()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
protected function getFieldRules(string $field, array $rules) : array |
|
47
|
|
|
{ |
|
48
|
|
|
$this->guardAgainstInvalidField($field, $rules); |
|
49
|
|
|
|
|
50
|
|
|
return $rules[$field]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function guardAgainstInvalidField(string $field, array $rules) |
|
54
|
|
|
{ |
|
55
|
|
|
if (! array_key_exists($field, $rules)) { |
|
56
|
|
|
throw new OutOfBoundsException("invalid field, '{$field}'"); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function provideRules() : array |
|
61
|
|
|
{ |
|
62
|
|
|
return []; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function provideCreationRules() : array |
|
66
|
|
|
{ |
|
67
|
|
|
return []; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function provideUpdateRules() : array |
|
71
|
|
|
{ |
|
72
|
|
|
return []; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|