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\All; |
16
|
|
|
use Symfony\Component\Validator\Constraints\Length; |
17
|
|
|
use Symfony\Component\Validator\Constraints\NotBlank; |
18
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface; |
19
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Mateusz Zalewski <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class TextAttributeType implements AttributeTypeInterface |
25
|
|
|
{ |
26
|
|
|
const TYPE = 'text'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function getStorageType() |
32
|
|
|
{ |
33
|
|
|
return AttributeValueInterface::STORAGE_TEXT; |
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['required']) && (!isset($configuration['min']) || !isset($configuration['max']))) { |
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) |
72
|
|
|
{ |
73
|
|
|
$validator = $context->getValidator(); |
74
|
|
|
$constraints = []; |
75
|
|
|
|
76
|
|
|
if (isset($validationConfiguration['required'])) { |
77
|
|
|
$constraints = [new NotBlank([])]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (isset($validationConfiguration['min']) && isset($validationConfiguration['max'])) { |
81
|
|
|
$constraints[] = new Length( |
82
|
|
|
[ |
83
|
|
|
'min' => $validationConfiguration['min'], |
84
|
|
|
'max' => $validationConfiguration['max'], |
85
|
|
|
] |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $validator->validate( |
90
|
|
|
$value, $constraints |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|