CourseMapping::getConstraints()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
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 3
    public function __construct(ResolverInterface $resolver)
36
    {
37 3
        $this->resolver = $resolver;
38 3
    }
39
40
    /**
41
     * @return string
42
     */
43 1
    public function getClass(): string
44
    {
45 1
        return Course::class;
46
    }
47
48
    /**
49
     * @return ConstraintInterface[]
50
     */
51 1
    public function getConstraints(): array
52
    {
53 1
        return [new UniqueModelConstraint($this->resolver, ['name'])];
54
    }
55
56
    /**
57
     * @return PropertyMappingInterface[]
58
     */
59 1
    public function getPropertyMappings(): array
60
    {
61
        return [
62 1
            new PropertyMapping('name', [new NotNullConstraint(), new NotBlankConstraint()]),
63 1
            new PropertyMapping('level', [
64 1
                new NotNullConstraint(),
65 1
                new ChoiceConstraint(
66 1
                    ChoiceConstraint::TYPE_INT,
67 1
                    Course::LEVEL
68
                ),
69
            ]),
70 1
            new PropertyMapping('progress', [
71 1
                new NotNullConstraint(),
72 1
                new NumericConstraint(),
73 1
                new NumericRangeConstraint(0, 1),
74
            ]),
75 1
            new PropertyMapping('active', [new NotNullConstraint(), new NotBlankConstraint()]),
76 1
            new PropertyMapping('documents', [
77 1
                new ModelCollectionConstraint(),
78 1
                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 1
                }),
101
            ]),
102
        ];
103
    }
104
}
105