PluginProbe
Metricool – Social media and site statistics / 2.1.1
Metricool – Social media and site statistics v2.1.1
2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
metricool / app / Support / Validation / Rules / RequiredIfRule.php

RequiredIfRule.php in Metricool – Social media and site statistics 2.1.1, at app/Support/Validation/Rules/RequiredIfRule.php

45 lines 1.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Metricool\Support\Validation\Rules;
6
7 /**
8 * Makes a field required when another field has a given value, e.g.
9 * "requiredIf:sendToAlternativeEmail,true".
10 */
11 class RequiredIfRule extends AbstractRule
12 {
13 /**
14 * Checks if the required param matches the required value in the data and
15 * then validates the field with the {@see RequiredRule}
16 */
17 public function validate(string $field, $value, array $data): void
18 {
19 if ($this->shouldValidate($data)) {
20 (new RequiredRule())->validate($field, $value, $data);
21 }
22 }
23
24 /**
25 * @inheritDoc
26 */
27 public function isRequired(): bool
28 {
29 return true;
30 }
31
32 /**
33 * Checks if the required param matches the required value in the data.
34 * Uses a loose comparison so the string parameter "true" also matches a
35 * boolean request value.
36 */
37 protected function shouldValidate(array $data): bool
38 {
39 $requiredParam = $this->parameters[0] ?? '';
40 $requiredValue = $this->parameters[1] ?? null;
41
42 return ($data[$requiredParam] ?? null) == $requiredValue;
43 }
44 }
45