andrey-helldar /
support
| 1 | <?php |
||
| 2 | /* |
||
| 3 | * This file is part of the "andrey-helldar/support" project. |
||
| 4 | * |
||
| 5 | * For the full copyright and license information, please view the LICENSE |
||
| 6 | * file that was distributed with this source code. |
||
| 7 | * |
||
| 8 | * @author Andrey Helldar <[email protected]> |
||
| 9 | * |
||
| 10 | * @copyright 2021 Andrey Helldar |
||
| 11 | * |
||
| 12 | * @license MIT |
||
| 13 | * |
||
| 14 | * @see https://github.com/andrey-helldar/support |
||
| 15 | */ |
||
| 16 | |||
| 17 | namespace Helldar\Support\Concerns; |
||
| 18 | |||
| 19 | use Helldar\Support\Exceptions\ForbiddenVariableTypeException; |
||
| 20 | use Helldar\Support\Facades\Helpers\Ables\Arrayable; |
||
| 21 | use Helldar\Support\Facades\Helpers\Str; |
||
| 22 | |||
| 23 | trait Validation |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @param mixed $haystack |
||
| 27 | * @param array|string $needles |
||
| 28 | */ |
||
| 29 | 65 | protected function validateType($haystack, $needles): void |
|
| 30 | { |
||
| 31 | 65 | $type = $this->validateGetType($haystack); |
|
| 32 | 65 | $needles = $this->validateNeedles($needles); |
|
| 33 | |||
| 34 | 65 | if (! Str::contains($type, $needles)) { |
|
| 35 | 1 | throw new ForbiddenVariableTypeException($type, $needles); |
|
| 36 | } |
||
| 37 | 64 | } |
|
| 38 | |||
| 39 | 65 | protected function validateNeedles($values): array |
|
| 40 | { |
||
| 41 | 65 | return Arrayable::of($values) |
|
| 42 | 65 | ->map(static function ($value) { |
|
| 43 | 65 | return Str::lower($value); |
|
| 44 | 65 | })->get(); |
|
| 45 | } |
||
| 46 | |||
| 47 | 65 | protected function validateGetType($haystack): string |
|
| 48 | { |
||
| 49 | 65 | return Str::lower(gettype($haystack)); |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 50 | } |
||
| 51 | } |
||
| 52 |