| @@ 5-36 (lines=32) @@ | ||
| 2 | ||
| 3 | namespace Mbright\Validation\Rule\Sanitize; |
|
| 4 | ||
| 5 | class Field implements SanitizeRuleInterface |
|
| 6 | { |
|
| 7 | /** @var string */ |
|
| 8 | protected $otherField; |
|
| 9 | ||
| 10 | /** |
|
| 11 | * @param string $other_field The name of the other subject field. |
|
| 12 | */ |
|
| 13 | public function __construct(string $otherField) |
|
| 14 | { |
|
| 15 | $this->otherField = $otherField; |
|
| 16 | } |
|
| 17 | ||
| 18 | /** |
|
| 19 | * Modifies the field value to match that of another field. |
|
| 20 | * |
|
| 21 | * @param object $subject The subject to be filtered. |
|
| 22 | * @param string $field The subject field name. |
|
| 23 | * |
|
| 24 | * @return bool True if the value was sanitized, false if not. |
|
| 25 | */ |
|
| 26 | public function __invoke($subject, string $field): bool |
|
| 27 | { |
|
| 28 | // the other field needs to exist and *not* be null |
|
| 29 | if (! isset($subject->{$this->otherField})) { |
|
| 30 | return false; |
|
| 31 | } |
|
| 32 | $subject->$field = $subject->{$this->otherField}; |
|
| 33 | ||
| 34 | return true; |
|
| 35 | } |
|
| 36 | } |
|
| 37 | ||
| @@ 5-36 (lines=32) @@ | ||
| 2 | ||
| 3 | namespace Mbright\Validation\Rule\Validate; |
|
| 4 | ||
| 5 | class EqualToField implements ValidateRuleInterface |
|
| 6 | { |
|
| 7 | /** @var string */ |
|
| 8 | protected $otherField; |
|
| 9 | ||
| 10 | /** |
|
| 11 | * @param string $otherField Check against the value of this field on the subject. |
|
| 12 | */ |
|
| 13 | public function __construct(string $otherField) |
|
| 14 | { |
|
| 15 | $this->otherField = $otherField; |
|
| 16 | } |
|
| 17 | ||
| 18 | /** |
|
| 19 | * Validates that this value is loosely equal to some other subject field. |
|
| 20 | * |
|
| 21 | * If the other element does not exist in $subject, or is null, the validation will fail. |
|
| 22 | * |
|
| 23 | * @param object $subject The subject to be filtered. |
|
| 24 | * @param string $field The subject field name. |
|
| 25 | * |
|
| 26 | * @return bool True if the values are equal, false if not equal. |
|
| 27 | */ |
|
| 28 | public function __invoke($subject, string $field): bool |
|
| 29 | { |
|
| 30 | if (!isset($subject->{$this->otherField})) { |
|
| 31 | return false; |
|
| 32 | } |
|
| 33 | ||
| 34 | return $subject->$field == $subject->{$this->otherField}; |
|
| 35 | } |
|
| 36 | } |
|
| 37 | ||
| @@ 5-37 (lines=33) @@ | ||
| 2 | ||
| 3 | namespace Mbright\Validation\Rule\Validate; |
|
| 4 | ||
| 5 | class StrictEqualToField implements ValidateRuleInterface |
|
| 6 | { |
|
| 7 | /** @var string */ |
|
| 8 | protected $otherField; |
|
| 9 | ||
| 10 | /** |
|
| 11 | * @param string $otherField Check against the value of this element in $subject. |
|
| 12 | */ |
|
| 13 | public function __construct(string $otherField) |
|
| 14 | { |
|
| 15 | $this->otherField = $otherField; |
|
| 16 | } |
|
| 17 | ||
| 18 | /** |
|
| 19 | * Validates that this value is strictly equal to some other subject field. |
|
| 20 | * |
|
| 21 | * If the other element does not exist in $subject, or is null, the validation will fail. |
|
| 22 | * |
|
| 23 | * @param object $subject The subject to be filtered. |
|
| 24 | * @param string $field The subject field name. |
|
| 25 | * |
|
| 26 | * @return bool True if the values are equal, false if not equal. |
|
| 27 | */ |
|
| 28 | public function __invoke($subject, string $field): bool |
|
| 29 | { |
|
| 30 | // the other field needs to exist and *not* be null |
|
| 31 | if (! isset($subject->{$this->otherField})) { |
|
| 32 | return false; |
|
| 33 | } |
|
| 34 | ||
| 35 | return $subject->$field === $subject->{$this->otherField}; |
|
| 36 | } |
|
| 37 | } |
|
| 38 | ||