PublicationFieldValidator::validate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Publications\Actions;
6
7
use Hyde\Publications\Concerns\PublicationFieldTypes;
8
use Hyde\Publications\Models\PublicationFieldDefinition;
9
use Hyde\Publications\Models\PublicationType;
10
use Hyde\Publications\Publications;
11
use Illuminate\Contracts\Validation\Validator;
12
13
use function array_merge;
14
use function validator;
15
16
/**
17
 * @see \Hyde\Publications\Testing\Feature\PublicationFieldValidatorTest
18
 */
19
class PublicationFieldValidator
20
{
21
    protected PublicationType $publicationType;
22
    protected PublicationFieldDefinition $fieldDefinition;
23
24
    public function __construct(PublicationType $publicationType, PublicationFieldDefinition $fieldDefinition)
25
    {
26
        $this->publicationType = $publicationType;
27
        $this->fieldDefinition = $fieldDefinition;
28
    }
29
30
    public function getValidationRules(): array
31
    {
32
        return array_merge(
33
            $this->fieldDefinition->getRules(),
34
            $this->makeDynamicRules()
35
        );
36
    }
37
38
    /** @throws \Illuminate\Validation\ValidationException */
39
    public function validate(mixed $input = null): array
40
    {
41
        return $this->makeValidator($input, $this->getValidationRules())->validate();
42
    }
43
44
    protected function makeDynamicRules(): array
45
    {
46
        if ($this->fieldDefinition->type == PublicationFieldTypes::Media) {
47
            $mediaFiles = Publications::getMediaForType($this->publicationType);
48
            $valueList = $mediaFiles->implode(',');
49
50
            return ["in:$valueList"];
51
        }
52
53
        if ($this->fieldDefinition->type == PublicationFieldTypes::Tag) {
0 ignored issues
show
Deprecated Code introduced by
The constant Hyde\Publications\Concer...licationFieldTypes::Tag has been deprecated: May be renamed to Tags to better fit usage ( Ignorable by Annotation )

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

53
        if ($this->fieldDefinition->type == /** @scrutinizer ignore-deprecated */ PublicationFieldTypes::Tag) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
54
            $tagValues = Publications::getPublicationTags() ?? [];
55
            $valueList = implode(',', $tagValues);
56
57
            return ["in:$valueList"];
58
        }
59
60
        return [];
61
    }
62
63
    protected function makeValidator(mixed $input, array $rules): Validator
64
    {
65
        return validator(
66
            [$this->fieldDefinition->name => $input],
67
            [$this->fieldDefinition->name => $rules]
68
        );
69
    }
70
}
71