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