These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler; |
||
4 | |||
5 | use eZ\Publish\API\Repository\Values\Content\Content; |
||
6 | use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition; |
||
7 | use eZ\Publish\Core\FieldType\Float as FloatValue; |
||
8 | use eZ\Publish\Core\Helper\FieldHelper; |
||
9 | use eZ\Publish\SPI\FieldType\Value; |
||
10 | use Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler; |
||
11 | use Symfony\Component\Form\Extension\Core\Type\NumberType; |
||
12 | use Symfony\Component\Form\FormBuilderInterface; |
||
13 | use Symfony\Component\Validator\Constraints as Assert; |
||
14 | |||
15 | View Code Duplication | class FloatHandler extends FieldTypeHandler |
|
0 ignored issues
–
show
|
|||
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 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.