|
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\ConstraintViolationListInterface; |
|
16
|
|
|
use Symfony\Component\Validator\Constraints\Length; |
|
17
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author Mateusz Zalewski <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class TextAttributeType implements AttributeTypeInterface |
|
23
|
|
|
{ |
|
24
|
|
|
const TYPE = 'text'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getStorageType() |
|
30
|
|
|
{ |
|
31
|
|
|
return AttributeValueInterface::STORAGE_TEXT; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getType() |
|
38
|
|
|
{ |
|
39
|
|
|
return static::TYPE; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function validate(AttributeValueInterface $attributeValue, ExecutionContextInterface $context, array $configuration) |
|
46
|
|
|
{ |
|
47
|
|
|
if (!isset($configuration['min']) || !isset($configuration['max'])) { |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$value = $attributeValue->getValue(); |
|
52
|
|
|
|
|
53
|
|
|
foreach ($this->getValidationErrors($context, $value, $configuration) as $error) { |
|
54
|
|
|
$context |
|
55
|
|
|
->buildViolation($error->getMessage()) |
|
56
|
|
|
->atPath('value') |
|
57
|
|
|
->addViolation() |
|
58
|
|
|
; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param ExecutionContextInterface $context |
|
64
|
|
|
* @param string $value |
|
65
|
|
|
* @param array $validationConfiguration |
|
66
|
|
|
* |
|
67
|
|
|
* @return ConstraintViolationListInterface |
|
68
|
|
|
*/ |
|
69
|
|
|
private function getValidationErrors(ExecutionContextInterface $context, $value, array $validationConfiguration) |
|
70
|
|
|
{ |
|
71
|
|
|
$validator = $context->getValidator(); |
|
72
|
|
|
|
|
73
|
|
|
return $validator->validate( |
|
74
|
|
|
$value, |
|
75
|
|
|
new Length(array( |
|
76
|
|
|
'min' => $validationConfiguration['min'], |
|
77
|
|
|
'max' => $validationConfiguration['max'], |
|
78
|
|
|
)) |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|