Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| StringInputLength | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
7 | |
100.00% |
1 / 1 |
| validate | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| isDateTime | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Engelsystem\Http\Validation\Rules; |
| 6 | |
| 7 | use Carbon\Exceptions\InvalidFormatException; |
| 8 | use Engelsystem\Helpers\Carbon; |
| 9 | use Engelsystem\Helpers\CarbonDay; |
| 10 | use Illuminate\Support\Str; |
| 11 | |
| 12 | trait StringInputLength |
| 13 | { |
| 14 | /** |
| 15 | * Use the input length of a string |
| 16 | */ |
| 17 | public function validate(mixed $input): bool |
| 18 | { |
| 19 | if ( |
| 20 | is_string($input) |
| 21 | && !is_numeric($input) |
| 22 | && !$this->isDateTime($input) |
| 23 | ) { |
| 24 | $input = Str::length($input); |
| 25 | } |
| 26 | |
| 27 | return parent::validate($input); |
| 28 | } |
| 29 | |
| 30 | protected function isDateTime(mixed $input): bool |
| 31 | { |
| 32 | try { |
| 33 | return !is_null(Carbon::createFromDatetime($input)) || !is_null(CarbonDay::createFromDay($input)); |
| 34 | } catch (InvalidFormatException) { |
| 35 | } |
| 36 | |
| 37 | return false; |
| 38 | } |
| 39 | } |