|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Linio\DynamicFormBundle\FormlyMapper\FormlyField; |
| 6 | + |
| 7 | +use Linio\DynamicFormBundle\Exception\InvalidConfigurationException; |
| 8 | +use Linio\DynamicFormBundle\Exception\NumberFormatException; |
| 9 | +use Linio\DynamicFormBundle\FormlyMapper\FormlyField; |
| 10 | + |
| 11 | +class BirthdayField extends FormlyField |
| 12 | +{ |
| 13 | + const ORDER_ASC = 'asc'; |
| 14 | + const ORDER_DESC = 'desc'; |
| 15 | + |
| 16 | + /** |
| 17 | + * {@inheritdoc} |
| 18 | + */ |
| 19 | + protected function getFieldType() |
| 20 | + { |
| 21 | + return 'birthday'; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * {@inheritdoc} |
| 26 | + */ |
| 27 | + protected function buildFieldTypeConfiguration(): void |
| 28 | + { |
| 29 | + $options = $this->fieldConfiguration['options']; |
| 30 | + |
| 31 | + $this->validateNumberFormatForSpecificRangeOfYearsArray($options); |
| 32 | + $this->validateMaxAgeAllowedIsLargerThanMinAgeAllowed($options); |
| 33 | + |
| 34 | + $this->formlyFieldConfiguration['templateOptions']['type'] = $this->getFieldType(); |
| 35 | + |
| 36 | + $order = isset($options['order']) && (strtolower($options['order']) == (self::ORDER_ASC || self::ORDER_DESC)) |
| 37 | + ? $options['order'] |
| 38 | + : self::ORDER_DESC; |
| 39 | + |
| 40 | + unset($this->formlyFieldConfiguration['templateOptions']['order']); |
| 41 | + |
| 42 | + if (!isset($options['minAgeAllowed']) || !isset($options['maxAgeAllowed']) |
| 43 | + || !is_numeric($options['minAgeAllowed']) || !is_numeric($options['maxAgeAllowed']) |
| 44 | + || $options['minAgeAllowed'] < 0 || $options['maxAgeAllowed'] < 0) { |
| 45 | + $this->removeAgeAllowedFromTemplateOptions(); |
| 46 | + |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + $yearsRange = range(date('Y') - $options['minAgeAllowed'], date('Y') - $options['maxAgeAllowed']); |
| 51 | + |
| 52 | + $this->formlyFieldConfiguration['templateOptions']['years'] = ($order == self::ORDER_ASC) |
| 53 | + ? array_reverse($yearsRange) |
| 54 | + : $yearsRange; |
| 55 | + |
| 56 | + $this->removeAgeAllowedFromTemplateOptions(); |
| 57 | + } |
| 58 | + |
| 59 | + private function removeAgeAllowedFromTemplateOptions(): void |
| 60 | + { |
| 61 | + unset($this->formlyFieldConfiguration['templateOptions']['minAgeAllowed']); |
| 62 | + unset($this->formlyFieldConfiguration['templateOptions']['maxAgeAllowed']); |
| 63 | + } |
| 64 | + |
| 65 | + private function validateNumberFormatForSpecificRangeOfYearsArray(array $options): void |
| 66 | + { |
| 67 | + if (!isset($options['years'])) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + foreach ($options['years'] as $key => $year) { |
| 72 | + if (!is_numeric($year)) { |
| 73 | + throw new NumberFormatException(sprintf('The year "%s" does not have a valid number format.', $year)); |
| 74 | + } |
| 75 | + |
| 76 | + $options['years'][$key] = (int) $year; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private function validateMaxAgeAllowedIsLargerThanMinAgeAllowed(array $options): void |
| 81 | + { |
| 82 | + if (!isset($options['minAgeAllowed']) || !isset($options['maxAgeAllowed'])) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + if ($options['minAgeAllowed'] > $options['maxAgeAllowed']) { |
| 87 | + throw new InvalidConfigurationException(sprintf('The value of minAgeAllowed (%s) cannot be greater than maxAgeAllowed (%s).', $options['minAgeAllowed'], $options['maxAgeAllowed'])); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments