Test Failed
Push — fix/localized-img-bug ( a16f3e...daa57d )
by Ben
21:18
created

FieldValidatorFactory::normalizeRules()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 14
rs 9.6111
cc 5
nc 5
nop 3
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
    public function __construct(Factory $validatorFactory)
16
    {
17
        $this->validatorFactory = $validatorFactory;
18
    }
19
20
    public function create(Field $field, array $data): Validator
21
    {
22
        if (!$field->hasValidation()) {
23
            return new NullValidator();
24
        }
25
26
        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
        if ($field->validation instanceof \Closure) {
31
            $closure = $field->validation;
32
            return $closure($this->validatorFactory, $data);
33
        }
34
35
        return $this->composeValidatorFromRules($field, $data);
36
    }
37
38
    private function composeValidatorFromRules(Field $field, array $data): Validator
39
    {
40
        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
        return $this->validatorFactory->make($data,
45
            $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
            $field->validation[1] ?? [], // messages
47
            $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
    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
        if (!is_array($rules) || isset($rules[0])) {
61
            $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
            if ($field->isTranslatable()) {
64
                $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
                    ->influenceByPayload($data)
66
                    ->rules($rules);
67
            }
68
        }
69
70
        return $rules;
71
    }
72
}
73