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 DateTime; |
||
6 | use eZ\Publish\API\Repository\Values\Content\Content; |
||
7 | use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition; |
||
8 | use eZ\Publish\Core\FieldType\Time\Value as TimeValue; |
||
9 | use eZ\Publish\SPI\FieldType\Value; |
||
10 | use Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler; |
||
11 | use Symfony\Component\Form\Extension\Core\Type\TimeType; |
||
12 | use Symfony\Component\Form\FormBuilderInterface; |
||
13 | use Symfony\Component\Validator\Constraints as Assert; |
||
14 | |||
15 | class Time extends FieldTypeHandler |
||
16 | { |
||
17 | /** |
||
18 | * {@inheritdoc} |
||
19 | * |
||
20 | * @return int|null |
||
21 | */ |
||
22 | public function convertFieldValueToForm(Value $value, FieldDefinition $fieldDefinition = null) |
||
23 | { |
||
24 | $time = $value->time; |
||
25 | if (is_int($time)) { |
||
26 | return new DateTime("@{$time}"); |
||
27 | } |
||
28 | |||
29 | return new DateTime(); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | * |
||
35 | * @return TimeValue |
||
36 | */ |
||
37 | public function convertFieldValueFromForm($data) |
||
38 | { |
||
39 | if ($data instanceof DateTime) { |
||
40 | return TimeValue::fromDateTime($data); |
||
41 | } |
||
42 | |||
43 | if (is_int($data)) { |
||
44 | return new TimeValue($data); |
||
45 | } |
||
46 | |||
47 | return new TimeValue(null); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | View Code Duplication | protected function buildFieldForm( |
|
0 ignored issues
–
show
|
|||
54 | FormBuilderInterface $formBuilder, |
||
55 | FieldDefinition $fieldDefinition, |
||
56 | $languageCode, |
||
57 | Content $content = null |
||
58 | ) { |
||
59 | $options = $this->getDefaultFieldOptions($fieldDefinition, $languageCode, $content); |
||
60 | |||
61 | $useSeconds = $fieldDefinition->getFieldSettings()['useSeconds']; |
||
62 | $options['input'] = 'datetime'; |
||
63 | $options['widget'] = 'choice'; |
||
64 | $options['with_seconds'] = $useSeconds; |
||
65 | $options['constraints'][] = new Assert\Time(); |
||
66 | |||
67 | $formBuilder->add($fieldDefinition->identifier, TimeType::class, $options); |
||
68 | } |
||
69 | } |
||
70 |
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.