Passed
Pull Request — main (#138)
by Andrey
15:00
created

Validation::validateNeedles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Helldar\Support\Concerns;
4
5
use Helldar\Support\Exceptions\ForbiddenVariableTypeException;
6
use Helldar\Support\Facades\Helpers\Ables\Arrayable;
7
use Helldar\Support\Facades\Helpers\Str;
8
9
trait Validation
10
{
11
    /**
12
     * @param  mixed  $haystack
13
     * @param  array|string  $needles
14
     */
15
    protected function validateType($haystack, $needles): void
16
    {
17
        $type    = $this->validateGetType($haystack);
18
        $needles = $this->validateNeedles($needles);
19
20
        if (! Str::contains($type, $needles)) {
21
            throw new ForbiddenVariableTypeException($type, $needles);
22
        }
23
    }
24
25
    protected function validateNeedles($values): array
26
    {
27
        return Arrayable::of($values)
28
            ->map(static function ($value) {
29
                return Str::lower($value);
30
            })->get();
31
    }
32
33
    protected function validateGetType($haystack): string
34
    {
35
        return Str::lower(gettype($haystack));
0 ignored issues
show
Bug Best Practice introduced by
The expression return Helldar\Support\F...wer(gettype($haystack)) could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
36
    }
37
}
38