|
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) { |
|
|
|
|
|
|
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
|
|
|
|
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.