Completed
Push — dependabot/npm_and_yarn/tippy.... ( 40037f...deaa3c )
by
unknown
400:02 queued 379:38
created

FieldValidatorFactory::create()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.5923

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 8
c 2
b 1
f 0
dl 0
loc 16
ccs 6
cts 9
cp 0.6667
rs 10
cc 4
nc 4
nop 2
crap 4.5923
1
<?php
2
3
namespace Thinktomorrow\Chief\Fields\Validators;
4
5
use Illuminate\Contracts\Validation\Validator;
6
use Illuminate\Contracts\Validation\Factory;
7
use Thinktomorrow\Chief\Fields\LocalizedFieldValidationRules;
8
use Thinktomorrow\Chief\Fields\Types\Field;
9
10
class FieldValidatorFactory
11
{
12
    /** @var Factory */
13
    private $validatorFactory;
14
15 100
    public function __construct(Factory $validatorFactory)
16
    {
17 100
        $this->validatorFactory = $validatorFactory;
18 100
    }
19
20 100
    public function create(Field $field, array $data): Validator
21
    {
22 100
        if (!$field->hasValidation()) {
23 90
            return new NullValidator();
24
        }
25
26 89
        if ($field->validation instanceof Validator) {
0 ignored issues
show
Bug Best Practice introduced by
The property validation does not exist on Thinktomorrow\Chief\Fields\Types\Field. Since you implemented __get, consider adding a @property annotation.
Loading history...
27
            return $field->validation;
28
        }
29
30 89
        if ($field->validation instanceof \Closure) {
31
            $closure = $field->validation;
32
            return $closure($this->validatorFactory, $data);
33
        }
34
35 89
        return $this->composeValidatorFromRules($field, $data);
36
    }
37
38 89
    private function composeValidatorFromRules(Field $field, array $data): Validator
39
    {
40 89
        if (!is_array($field->validation) || !isset($field->validation[0])) {
0 ignored issues
show
Bug Best Practice introduced by
The property validation does not exist on Thinktomorrow\Chief\Fields\Types\Field. Since you implemented __get, consider adding a @property annotation.
Loading history...
41
            throw new \Exception('Invalid validation given. Rules should be passed as non-associative array.');
42
        }
43
44 89
        return $this->validatorFactory->make($data,
45 89
            $this->normalizeRules($field, $field->validation[0], $data),
0 ignored issues
show
Bug introduced by
It seems like $this->normalizeRules($f...->validation[0], $data) can also be of type null; however, parameter $rules of Illuminate\Validation\Factory::make() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
            /** @scrutinizer ignore-type */ $this->normalizeRules($field, $field->validation[0], $data),
Loading history...
46 89
            $field->validation[1] ?? [], // messages
47 89
            $field->validation[2] ?? [] // custom attributes
48
        );
49
    }
50
51
    /**
52
     * @param Field $field
53
     * @param string|array $rules
54
     * @param array $data
55
     * @return array|null
56
     */
57 89
    private function normalizeRules(Field $field, $rules, array $data)
58
    {
59
        // Normalize rules: If no attribute is passed for the rule, we use the field name.
60 89
        if (!is_array($rules) || isset($rules[0])) {
61 63
            $rules = [$field->name => (is_array($rules) ? reset($rules) : $rules)];
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Thinktomorrow\Chief\Fields\Types\Field. Since you implemented __get, consider adding a @property annotation.
Loading history...
62
63 63
            if ($field->isTranslatable()) {
64 55
                $rules = (new LocalizedFieldValidationRules($field->locales))
0 ignored issues
show
Bug Best Practice introduced by
The property locales does not exist on Thinktomorrow\Chief\Fields\Types\Field. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
It seems like $field->locales can also be of type null; however, parameter $locales of Thinktomorrow\Chief\Fiel...ionRules::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
                $rules = (new LocalizedFieldValidationRules(/** @scrutinizer ignore-type */ $field->locales))
Loading history...
65 55
                    ->influenceByPayload($data)
66 55
                    ->rules($rules);
67
            }
68
        }
69
70 89
        return $rules;
71
    }
72
}
73