Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
20 / 20 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
| BaseRules | |
100.00% |
20 / 20 |
|
100.00% |
10 / 10 |
12 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __get | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| optional | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| blank | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| null | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| empty | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| esc | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| implode | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| create | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php declare(strict_types=1); |
| 2 | /* |
| 3 | * This file is part of Aplus Framework Validation Library. |
| 4 | * |
| 5 | * (c) Natan Felles <natanfelles@gmail.com> |
| 6 | * |
| 7 | * For the full copyright and license information, please view the LICENSE |
| 8 | * file that was distributed with this source code. |
| 9 | */ |
| 10 | namespace Framework\Validation; |
| 11 | |
| 12 | use Error; |
| 13 | use Stringable; |
| 14 | |
| 15 | /** |
| 16 | * Class BaseRules. |
| 17 | * |
| 18 | * @property-read array<int,string> $rules |
| 19 | * |
| 20 | * @package validation |
| 21 | */ |
| 22 | abstract class BaseRules implements Stringable |
| 23 | { |
| 24 | /** |
| 25 | * @var array<int,string> |
| 26 | */ |
| 27 | protected array $rules = []; |
| 28 | |
| 29 | /** |
| 30 | * @since 2.3 |
| 31 | */ |
| 32 | final public function __construct() |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | public function __toString() : string |
| 37 | { |
| 38 | return \implode('|', $this->rules); |
| 39 | } |
| 40 | |
| 41 | public function __get(string $property) : mixed |
| 42 | { |
| 43 | if ($property === 'rules') { |
| 44 | return $this->rules; |
| 45 | } |
| 46 | throw new Error( |
| 47 | 'Cannot access property ' . static::class . '::$' . $property |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Set field as optional. |
| 53 | * |
| 54 | * If field is undefined, validation passes. |
| 55 | * |
| 56 | * @return static |
| 57 | */ |
| 58 | public function optional() : static |
| 59 | { |
| 60 | $this->rules[] = 'optional'; |
| 61 | return $this; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * If the field has a blank string, the validation passes. |
| 66 | * |
| 67 | * @since 2.2 |
| 68 | * |
| 69 | * @return static |
| 70 | */ |
| 71 | public function blank() : static |
| 72 | { |
| 73 | $this->rules[] = 'blank'; |
| 74 | return $this; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * If the field value is null, the validation passes. |
| 79 | * |
| 80 | * @since 2.2 |
| 81 | * |
| 82 | * @return static |
| 83 | */ |
| 84 | public function null() : static |
| 85 | { |
| 86 | $this->rules[] = 'null'; |
| 87 | return $this; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * If the field has an empty value, the validation passes. |
| 92 | * |
| 93 | * @since 2.2 |
| 94 | * |
| 95 | * @return static |
| 96 | */ |
| 97 | public function empty() : static |
| 98 | { |
| 99 | $this->rules[] = 'empty'; |
| 100 | return $this; |
| 101 | } |
| 102 | |
| 103 | protected function esc(string $value) : string |
| 104 | { |
| 105 | return \strtr($value, [',' => '\,']); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @param array<scalar> $values |
| 110 | * |
| 111 | * @return string |
| 112 | */ |
| 113 | protected function implode(array $values) : string |
| 114 | { |
| 115 | foreach ($values as &$value) { |
| 116 | $value = $this->esc((string) $value); |
| 117 | } |
| 118 | return \implode(',', $values); |
| 119 | } |
| 120 | |
| 121 | public static function create() : static |
| 122 | { |
| 123 | return new static(); |
| 124 | } |
| 125 | } |