|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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 Symfony\Component\Form\Extension\Core\DataTransformer; |
| 13 | + |
| 14 | +use Symfony\Component\Form\DataTransformerInterface; |
| 15 | +use Symfony\Component\Form\Exception\TransformationFailedException; |
| 16 | +use Symfony\Component\Form\Exception\UnexpectedTypeException; |
| 17 | + |
| 18 | +/** |
| 19 | + * Transforms between a normalized date interval and an interval string/array. |
| 20 | + * |
| 21 | + * @author Steffen Roßkamp <steffen.rosskamp@gimmickmedia.de> |
| 22 | + */ |
| 23 | +class DateIntervalToArrayTransformer implements DataTransformerInterface |
| 24 | +{ |
| 25 | + const YEARS = 'years'; |
| 26 | + const MONTHS = 'months'; |
| 27 | + const DAYS = 'days'; |
| 28 | + const HOURS = 'hours'; |
| 29 | + const MINUTES = 'minutes'; |
| 30 | + const SECONDS = 'seconds'; |
| 31 | + const INVERT = 'invert'; |
| 32 | + |
| 33 | + private static $availableFields = array( |
| 34 | + self::YEARS => 'y', |
| 35 | + self::MONTHS => 'm', |
| 36 | + self::DAYS => 'd', |
| 37 | + self::HOURS => 'h', |
| 38 | + self::MINUTES => 'i', |
| 39 | + self::SECONDS => 's', |
| 40 | + self::INVERT => 'r', |
| 41 | + ); |
| 42 | + private $fields; |
| 43 | + |
| 44 | + /** |
| 45 | + * @param string[] $fields The date fields |
| 46 | + * @param bool $pad Whether to use padding |
| 47 | + */ |
| 48 | + public function __construct(array $fields = null, $pad = false) |
| 49 | + { |
| 50 | + if (null === $fields) { |
| 51 | + $fields = array('years', 'months', 'days', 'hours', 'minutes', 'seconds', 'invert'); |
| 52 | + } |
| 53 | + $this->fields = $fields; |
| 54 | + $this->pad = (bool) $pad; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Transforms a normalized date interval into an interval array. |
| 59 | + * |
| 60 | + * @param \DateInterval $dateInterval Normalized date interval. |
| 61 | + * |
| 62 | + * @return array Interval array. |
| 63 | + * |
| 64 | + * @throws UnexpectedTypeException If the given value is not a \DateInterval instance. |
| 65 | + */ |
| 66 | + public function transform($dateInterval) |
| 67 | + { |
| 68 | + if (null === $dateInterval) { |
| 69 | + return array_intersect_key( |
| 70 | + array( |
| 71 | + 'years' => '', |
| 72 | + 'months' => '', |
| 73 | + 'weeks' => '', |
| 74 | + 'days' => '', |
| 75 | + 'hours' => '', |
| 76 | + 'minutes' => '', |
| 77 | + 'seconds' => '', |
| 78 | + 'invert' => false, |
| 79 | + ), |
| 80 | + array_flip($this->fields) |
| 81 | + ); |
| 82 | + } |
| 83 | + if (!$dateInterval instanceof \DateInterval) { |
| 84 | + throw new UnexpectedTypeException($dateInterval, '\DateInterval'); |
| 85 | + } |
| 86 | + $result = array(); |
| 87 | + foreach (self::$availableFields as $field => $char) { |
| 88 | + $result[$field] = $dateInterval->format('%'.($this->pad ? strtoupper($char) : $char)); |
| 89 | + } |
| 90 | + if (in_array('weeks', $this->fields, true)) { |
| 91 | + $result['weeks'] = 0; |
| 92 | + if (isset($result['days']) && (int) $result['days'] >= 7) { |
| 93 | + $result['weeks'] = (string) floor($result['days'] / 7); |
| 94 | + $result['days'] = (string) ($result['days'] % 7); |
| 95 | + } |
| 96 | + } |
| 97 | + $result['invert'] = '-' === $result['invert']; |
| 98 | + $result = array_intersect_key($result, array_flip($this->fields)); |
| 99 | + |
| 100 | + return $result; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Transforms an interval array into a normalized date interval. |
| 105 | + * |
| 106 | + * @param array $value Interval array |
| 107 | + * |
| 108 | + * @return \DateInterval Normalized date interval |
| 109 | + * |
| 110 | + * @throws UnexpectedTypeException If the given value is not an array. |
| 111 | + * @throws TransformationFailedException If the value could not be transformed. |
| 112 | + */ |
| 113 | + public function reverseTransform($value) |
| 114 | + { |
| 115 | + if (null === $value) { |
| 116 | + return; |
| 117 | + } |
| 118 | + if (!is_array($value)) { |
| 119 | + throw new UnexpectedTypeException($value, 'array'); |
| 120 | + } |
| 121 | + if ('' === implode('', $value)) { |
| 122 | + return; |
| 123 | + } |
| 124 | + $emptyFields = array(); |
| 125 | + foreach ($this->fields as $field) { |
| 126 | + if (!isset($value[$field])) { |
| 127 | + $emptyFields[] = $field; |
| 128 | + } |
| 129 | + } |
| 130 | + if (count($emptyFields) > 0) { |
| 131 | + throw new TransformationFailedException(sprintf('The fields "%s" should not be empty', implode('", "', $emptyFields))); |
| 132 | + } |
| 133 | + if (isset($value['invert']) && !is_bool($value['invert'])) { |
| 134 | + throw new TransformationFailedException('The value of "invert" must be boolean'); |
| 135 | + } |
| 136 | + foreach (self::$availableFields as $field => $char) { |
| 137 | + if ($field !== 'invert' && isset($value[$field]) && !ctype_digit((string) $value[$field])) { |
| 138 | + throw new TransformationFailedException(sprintf('This amount of "%s" is invalid', $field)); |
| 139 | + } |
| 140 | + } |
| 141 | + try { |
| 142 | + if (!empty($value['weeks'])) { |
| 143 | + $interval = sprintf( |
| 144 | + 'P%sY%sM%sWT%sH%sM%sS', |
| 145 | + empty($value['years']) ? '0' : $value['years'], |
| 146 | + empty($value['months']) ? '0' : $value['months'], |
| 147 | + empty($value['weeks']) ? '0' : $value['weeks'], |
| 148 | + empty($value['hours']) ? '0' : $value['hours'], |
| 149 | + empty($value['minutes']) ? '0' : $value['minutes'], |
| 150 | + empty($value['seconds']) ? '0' : $value['seconds'] |
| 151 | + ); |
| 152 | + } else { |
| 153 | + $interval = sprintf( |
| 154 | + 'P%sY%sM%sDT%sH%sM%sS', |
| 155 | + empty($value['years']) ? '0' : $value['years'], |
| 156 | + empty($value['months']) ? '0' : $value['months'], |
| 157 | + empty($value['days']) ? '0' : $value['days'], |
| 158 | + empty($value['hours']) ? '0' : $value['hours'], |
| 159 | + empty($value['minutes']) ? '0' : $value['minutes'], |
| 160 | + empty($value['seconds']) ? '0' : $value['seconds'] |
| 161 | + ); |
| 162 | + } |
| 163 | + $dateInterval = new \DateInterval($interval); |
| 164 | + if (isset($value['invert'])) { |
| 165 | + $dateInterval->invert = $value['invert'] ? 1 : 0; |
| 166 | + } |
| 167 | + } catch (\Exception $e) { |
| 168 | + throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e); |
| 169 | + } |
| 170 | + |
| 171 | + return $dateInterval; |
| 172 | + } |
| 173 | +} |
0 commit comments