@@ 15-94 (lines=80) @@ | ||
12 | use Symfony\Component\Form\FormBuilderInterface; |
|
13 | use Symfony\Component\Validator\Constraints as Assert; |
|
14 | ||
15 | class FloatHandler extends FieldTypeHandler |
|
16 | { |
|
17 | /** |
|
18 | * @var FieldHelper |
|
19 | */ |
|
20 | protected $fieldHelper; |
|
21 | ||
22 | /** |
|
23 | * Integer constructor. |
|
24 | * |
|
25 | * @param FieldHelper $fieldHelper |
|
26 | */ |
|
27 | public function __construct(FieldHelper $fieldHelper) |
|
28 | { |
|
29 | $this->fieldHelper = $fieldHelper; |
|
30 | } |
|
31 | ||
32 | /** |
|
33 | * {@inheritdoc} |
|
34 | * |
|
35 | * @return float |
|
36 | */ |
|
37 | public function convertFieldValueToForm(Value $value, FieldDefinition $fieldDefinition = null) |
|
38 | { |
|
39 | return $value->value; |
|
40 | } |
|
41 | ||
42 | /** |
|
43 | * {@inheritdoc} |
|
44 | * |
|
45 | * @return FloatValue\Value |
|
46 | */ |
|
47 | public function convertFieldValueFromForm($data) |
|
48 | { |
|
49 | if (!is_numeric($data)) { |
|
50 | $data = null; |
|
51 | } |
|
52 | ||
53 | return new FloatValue\Value($data); |
|
54 | } |
|
55 | ||
56 | /** |
|
57 | * {@inheritdoc} |
|
58 | */ |
|
59 | protected function buildFieldForm( |
|
60 | FormBuilderInterface $formBuilder, |
|
61 | FieldDefinition $fieldDefinition, |
|
62 | $languageCode, |
|
63 | Content $content = null |
|
64 | ) { |
|
65 | $options = $this->getDefaultFieldOptions($fieldDefinition, $languageCode, $content); |
|
66 | ||
67 | if (!empty($fieldDefinition->getValidatorConfiguration()['FloatValueValidator'])) { |
|
68 | $rangeConstraints = []; |
|
69 | ||
70 | $min = $fieldDefinition->getValidatorConfiguration()['FloatValueValidator']['minFloatValue']; |
|
71 | $max = $fieldDefinition->getValidatorConfiguration()['FloatValueValidator']['maxFloatValue']; |
|
72 | ||
73 | if ($min !== false) { |
|
74 | $rangeConstraints['min'] = $min; |
|
75 | } |
|
76 | ||
77 | if ($max !== false) { |
|
78 | $rangeConstraints['max'] = $max; |
|
79 | } |
|
80 | ||
81 | if (!empty($rangeConstraints)) { |
|
82 | $options['constraints'][] = new Assert\Range($rangeConstraints); |
|
83 | } |
|
84 | } |
|
85 | ||
86 | if ($fieldDefinition->defaultValue instanceof FloatValue\Value) { |
|
87 | if (!$content instanceof Content) { |
|
88 | $options['data'] = (float) $fieldDefinition->defaultValue->value; |
|
89 | } |
|
90 | } |
|
91 | ||
92 | $formBuilder->add($fieldDefinition->identifier, NumberType::class, $options); |
|
93 | } |
|
94 | } |
|
95 |
@@ 15-94 (lines=80) @@ | ||
12 | use Symfony\Component\Form\FormBuilderInterface; |
|
13 | use Symfony\Component\Validator\Constraints as Assert; |
|
14 | ||
15 | class IntegerHandler extends FieldTypeHandler |
|
16 | { |
|
17 | /** |
|
18 | * @var FieldHelper |
|
19 | */ |
|
20 | protected $fieldHelper; |
|
21 | ||
22 | /** |
|
23 | * Integer constructor. |
|
24 | * |
|
25 | * @param FieldHelper $fieldHelper |
|
26 | */ |
|
27 | public function __construct(FieldHelper $fieldHelper) |
|
28 | { |
|
29 | $this->fieldHelper = $fieldHelper; |
|
30 | } |
|
31 | ||
32 | /** |
|
33 | * {@inheritdoc} |
|
34 | * |
|
35 | * @return int |
|
36 | */ |
|
37 | public function convertFieldValueToForm(Value $value, FieldDefinition $fieldDefinition = null) |
|
38 | { |
|
39 | return (int) $value->value; |
|
40 | } |
|
41 | ||
42 | /** |
|
43 | * {@inheritdoc} |
|
44 | * |
|
45 | * @return IntegerValue\Value |
|
46 | */ |
|
47 | public function convertFieldValueFromForm($data) |
|
48 | { |
|
49 | if (!is_int($data)) { |
|
50 | $data = null; |
|
51 | } |
|
52 | ||
53 | return new IntegerValue\Value($data); |
|
54 | } |
|
55 | ||
56 | /** |
|
57 | * {@inheritdoc} |
|
58 | */ |
|
59 | protected function buildFieldForm( |
|
60 | FormBuilderInterface $formBuilder, |
|
61 | FieldDefinition $fieldDefinition, |
|
62 | $languageCode, |
|
63 | Content $content = null |
|
64 | ) { |
|
65 | $options = $this->getDefaultFieldOptions($fieldDefinition, $languageCode, $content); |
|
66 | ||
67 | if ($fieldDefinition->defaultValue instanceof IntegerValue\Value) { |
|
68 | if (!$content instanceof Content) { |
|
69 | $options['data'] = (int) $fieldDefinition->defaultValue->value; |
|
70 | } |
|
71 | } |
|
72 | ||
73 | if (!empty($fieldDefinition->getValidatorConfiguration()['IntegerValueValidator'])) { |
|
74 | $rangeConstraints = []; |
|
75 | ||
76 | $min = $fieldDefinition->getValidatorConfiguration()['IntegerValueValidator']['minIntegerValue']; |
|
77 | $max = $fieldDefinition->getValidatorConfiguration()['IntegerValueValidator']['maxIntegerValue']; |
|
78 | ||
79 | if ($min !== null) { |
|
80 | $rangeConstraints['min'] = $min; |
|
81 | } |
|
82 | ||
83 | if ($max !== null) { |
|
84 | $rangeConstraints['max'] = $max; |
|
85 | } |
|
86 | ||
87 | if (!empty($rangeConstraints)) { |
|
88 | $options['constraints'][] = new Assert\Range($rangeConstraints); |
|
89 | } |
|
90 | } |
|
91 | ||
92 | $formBuilder->add($fieldDefinition->identifier, IntegerType::class, $options); |
|
93 | } |
|
94 | } |
|
95 |