1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Validator; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database\DefaultsManager; |
6
|
|
|
use GeminiLabs\SiteReviews\Defaults\ValidateReviewDefaults; |
7
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Rating; |
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Validator; |
10
|
|
|
use GeminiLabs\SiteReviews\Request; |
11
|
|
|
|
12
|
|
|
class DefaultValidator extends ValidatorAbstract |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @return bool |
16
|
|
|
*/ |
17
|
3 |
|
public function isValid() |
18
|
|
|
{ |
19
|
3 |
|
$this->errors = glsr(Validator::class)->validate( |
20
|
3 |
|
$this->request->toArray(), |
21
|
3 |
|
$this->rules() |
22
|
|
|
); |
23
|
3 |
|
return empty($this->errors); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* This only validates the provided values in the Request. |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
1 |
|
public function isValidRequest() |
31
|
|
|
{ |
32
|
1 |
|
$options = glsr(DefaultsManager::class)->pluck('settings.submissions.required.options'); |
33
|
1 |
|
$excludedKeys = array_keys(array_diff_key($options, $this->request->toArray())); |
34
|
1 |
|
$this->request->excluded = $excludedKeys; |
35
|
1 |
|
if ($this->isValid()) { |
36
|
1 |
|
return true; |
37
|
|
|
} |
38
|
|
|
glsr_log()->warning($this->errors); |
39
|
|
|
return false; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
2 |
|
public function performValidation() |
46
|
|
|
{ |
47
|
2 |
|
if (!$this->isValid()) { |
48
|
2 |
|
glsr_log()->debug($this->errors); |
49
|
2 |
|
$this->setErrors(__('Please fix the submission errors.', 'site-reviews')); |
50
|
2 |
|
return; |
51
|
|
|
} |
52
|
1 |
|
$values = glsr(ValidateReviewDefaults::class)->merge($this->request->toArray()); |
53
|
1 |
|
$this->request = new Request($values); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
3 |
|
protected function defaultRules() |
60
|
|
|
{ |
61
|
3 |
|
$maxRating = max(1, (int) glsr()->constant('MAX_RATING', Rating::class)); |
62
|
|
|
$rules = [ |
63
|
3 |
|
'content' => 'required', |
64
|
3 |
|
'email' => 'required|email', |
65
|
3 |
|
'name' => 'required', |
66
|
3 |
|
'rating' => 'required|between:0,'.$maxRating, |
67
|
3 |
|
'terms' => 'accepted', |
68
|
3 |
|
'title' => 'required', |
69
|
|
|
]; |
70
|
3 |
|
return glsr()->filterArray('validation/rules', $rules, $this->request); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
3 |
|
protected function normalizedRules() |
77
|
|
|
{ |
78
|
3 |
|
$rules = $this->defaultRules(); |
79
|
3 |
|
$required = glsr_get_option('submissions.required', []); |
80
|
|
|
array_walk($rules, function (&$value, $key) use ($required) { |
81
|
3 |
|
if (!in_array($key, $required)) { |
82
|
|
|
// remove the accepted and required rules from validation |
83
|
|
|
// since they are not required in the settings |
84
|
|
|
$values = explode('|', $value); |
85
|
|
|
$values = array_diff($values, ['accepted','required']); |
86
|
|
|
$value = implode('|', $values); |
87
|
|
|
} |
88
|
3 |
|
}); |
89
|
3 |
|
return $rules; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
3 |
|
protected function rules() |
96
|
|
|
{ |
97
|
3 |
|
$defaultRules = $this->normalizedRules(); |
98
|
3 |
|
$customRules = array_diff_key($defaultRules, |
99
|
3 |
|
glsr(DefaultsManager::class)->pluck('settings.submissions.required.options') |
100
|
|
|
); |
101
|
3 |
|
$excluded = Arr::convertFromString($this->request->excluded); // these fields were ommited with the hide option |
102
|
3 |
|
$rules = array_merge($defaultRules, $customRules); |
103
|
3 |
|
$rules = array_diff_key($rules, array_flip($excluded)); |
104
|
3 |
|
return glsr()->filterArray('validation/rules/normalized', $rules, $this->request, $defaultRules); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|