HasValidationRules.php
82 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * |
| 5 | * Modified by impress-org on 27-April-2023 using Strauss. |
| 6 | * @see https://github.com/BrianHenryIE/strauss |
| 7 | */ |
| 8 | |
| 9 | declare(strict_types=1); |
| 10 | |
| 11 | namespace Give\Vendors\StellarWP\Validation\Concerns; |
| 12 | |
| 13 | use Give\Vendors\StellarWP\Validation\Config; |
| 14 | use Give\Vendors\StellarWP\Validation\Contracts\ValidationRule; |
| 15 | use Give\Vendors\StellarWP\Validation\ValidationRuleSet; |
| 16 | |
| 17 | /** |
| 18 | * Apply this trait to a class to enable it to have validation rules. These rules may be passed to the front-end |
| 19 | * or used with the Validator to validate data. |
| 20 | * |
| 21 | * @unreleased |
| 22 | */ |
| 23 | trait HasValidationRules |
| 24 | { |
| 25 | /** |
| 26 | * @var ValidationRuleSet |
| 27 | */ |
| 28 | protected $validationRules; |
| 29 | |
| 30 | /** |
| 31 | * @unreleased |
| 32 | */ |
| 33 | public function __construct() |
| 34 | { |
| 35 | $this->validationRules = Config::getServiceContainer()->get(ValidationRuleSet::class); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @unreleased |
| 40 | */ |
| 41 | public function rules(...$rules): self |
| 42 | { |
| 43 | $this->validationRules->rules(...$rules); |
| 44 | |
| 45 | return $this; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @unreleased |
| 50 | */ |
| 51 | public function hasRule(string $ruleId): bool |
| 52 | { |
| 53 | return $this->validationRules->hasRule($ruleId); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @unreleased |
| 58 | */ |
| 59 | public function getRule(string $ruleId): ValidationRule |
| 60 | { |
| 61 | return $this->validationRules->getRule($ruleId); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @unreleased |
| 66 | */ |
| 67 | public function forgetRule(string $ruleId): self |
| 68 | { |
| 69 | $this->validationRules->removeRuleWithId($ruleId); |
| 70 | |
| 71 | return $this; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @unreleased |
| 76 | */ |
| 77 | public function getValidationRules(): ValidationRuleSet |
| 78 | { |
| 79 | return $this->validationRules; |
| 80 | } |
| 81 | } |
| 82 |