1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\ApiSkeleton\Validation; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\ApiSkeleton\Model\Document; |
8
|
|
|
use Chubbyphp\Model\Collection\ModelCollectionInterface; |
9
|
|
|
use Chubbyphp\Model\ResolverInterface; |
10
|
|
|
use Chubbyphp\Validation\Constraint\CallbackConstraint; |
11
|
|
|
use Chubbyphp\Validation\Constraint\ChoiceConstraint; |
12
|
|
|
use Chubbyphp\Validation\Constraint\ConstraintInterface; |
13
|
|
|
use Chubbyphp\Validation\Constraint\NotBlankConstraint; |
14
|
|
|
use Chubbyphp\Validation\Constraint\NotNullConstraint; |
15
|
|
|
use Chubbyphp\Validation\Constraint\NumericConstraint; |
16
|
|
|
use Chubbyphp\Validation\Constraint\NumericRangeConstraint; |
17
|
|
|
use Chubbyphp\Validation\Error\Error; |
18
|
|
|
use Chubbyphp\Validation\Mapping\ObjectMappingInterface; |
19
|
|
|
use Chubbyphp\Validation\Mapping\PropertyMapping; |
20
|
|
|
use Chubbyphp\Validation\Mapping\PropertyMappingInterface; |
21
|
|
|
use Chubbyphp\ApiSkeleton\Model\Course; |
22
|
|
|
use Chubbyphp\ValidationModel\Constraint\ModelCollectionConstraint; |
23
|
|
|
use Chubbyphp\ValidationModel\Constraint\UniqueModelConstraint; |
24
|
|
|
|
25
|
|
|
final class CourseMapping implements ObjectMappingInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var ResolverInterface |
29
|
|
|
*/ |
30
|
|
|
private $resolver; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param ResolverInterface $resolver |
34
|
|
|
*/ |
35
|
|
|
public function __construct(ResolverInterface $resolver) |
36
|
|
|
{ |
37
|
|
|
$this->resolver = $resolver; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function getClass(): string |
44
|
|
|
{ |
45
|
|
|
return Course::class; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return ConstraintInterface[] |
50
|
|
|
*/ |
51
|
|
|
public function getConstraints(): array |
52
|
|
|
{ |
53
|
|
|
return [new UniqueModelConstraint($this->resolver, ['name'])]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return PropertyMappingInterface[] |
58
|
|
|
*/ |
59
|
|
|
public function getPropertyMappings(): array |
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
|
|
new PropertyMapping('name', [new NotNullConstraint(), new NotBlankConstraint()]), |
63
|
|
|
new PropertyMapping('level', [ |
64
|
|
|
new NotNullConstraint(), |
65
|
|
|
new ChoiceConstraint( |
66
|
|
|
ChoiceConstraint::TYPE_INT, |
67
|
|
|
Course::LEVEL |
68
|
|
|
), |
69
|
|
|
]), |
70
|
|
|
new PropertyMapping('progress', [ |
71
|
|
|
new NotNullConstraint(), |
72
|
|
|
new NumericConstraint(), |
73
|
|
|
new NumericRangeConstraint(0, 1), |
74
|
|
|
]), |
75
|
|
|
new PropertyMapping('active', [new NotNullConstraint(), new NotBlankConstraint()]), |
76
|
|
|
new PropertyMapping('documents', [ |
77
|
|
|
new ModelCollectionConstraint(), |
78
|
|
|
new CallbackConstraint(function (string $path, $collection) { |
79
|
|
|
if (!$collection instanceof ModelCollectionInterface) { |
80
|
|
|
return []; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$names = []; |
84
|
|
|
foreach ($collection->getModels() as $document) { |
85
|
|
|
/** @var Document $document */ |
86
|
|
|
if (isset($names[$document->getName()])) { |
87
|
|
|
return [ |
88
|
|
|
new Error( |
89
|
|
|
$path.'[_all]', |
90
|
|
|
'constraint.uniquemodel.notunique', |
91
|
|
|
['uniqueProperties' => 'name'] |
92
|
|
|
), |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$names[$document->getName()] = true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return []; |
100
|
|
|
}), |
101
|
|
|
]), |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|