Completed
Push — npm-shrinkwrap ( 52ca24 )
by Kamil
23:13
created

SelectAttributeType::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Attribute\AttributeType;
13
14
use Sylius\Component\Attribute\Model\AttributeValueInterface;
15
use Symfony\Component\Validator\Constraints\Count;
16
use Symfony\Component\Validator\Constraints\Type;
17
use Symfony\Component\Validator\Constraints\All;
18
use Symfony\Component\Validator\ConstraintViolationListInterface;
19
use Symfony\Component\Validator\Context\ExecutionContextInterface;
20
21
/**
22
 * @author Laurent Paganin-Gioanni <[email protected]>
23
 */
24
class SelectAttributeType implements AttributeTypeInterface
25
{
26
    const TYPE = 'select';
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getStorageType()
32
    {
33
        return AttributeValueInterface::STORAGE_JSON;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getType()
40
    {
41
        return static::TYPE;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function validate(AttributeValueInterface $attributeValue, ExecutionContextInterface $context, array $configuration)
48
    {
49
        if (!isset($configuration['multiple'])) {
50
            return;
51
        }
52
53
        $value = $attributeValue->getValue();
54
55
        foreach ($this->getValidationErrors($context, $value, $configuration) as $error) {
56
            $context
57
                ->buildViolation($error->getMessage())
58
                ->atPath('value')
59
                ->addViolation()
60
            ;
61
        }
62
    }
63
64
    /**
65
     * @param ExecutionContextInterface $context
66
     * @param string $value
67
     * @param array $validationConfiguration
68
     *
69
     * @return ConstraintViolationListInterface
70
     */
71
    private function getValidationErrors(ExecutionContextInterface $context, $value, array $validationConfiguration)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $validationConfiguration exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
72
    {
73
        $validator = $context->getValidator();
74
75
        $constraints = [
76
            new All([
77
                new Type([
78
                    'type' => 'int',
79
                ])
80
            ]),
81
        ];
82
83
        if (isset($validationConfiguration['min']) && !empty($validationConfiguration['min'])) {
84
            $constraints[] = new Count([
85
                'min' => $validationConfiguration['min'],
86
            ]);
87
        }
88
89
        if (isset($validationConfiguration['max']) && !empty($validationConfiguration['max'])) {
90
            $constraints[] = new Count([
91
                'max' => $validationConfiguration['max'],
92
            ]);
93
        }
94
95
        return $validator->validate($value, $constraints);
96
    }
97
}
98