|
| 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 | + |
| 17 | +/** |
| 18 | + * Transforms between an ISO 8601 week date string and an array. |
| 19 | + * |
| 20 | + * @author Damien Fayet <damienf1521@gmail.com> |
| 21 | + */ |
| 22 | +class WeekToArrayTransformer implements DataTransformerInterface |
| 23 | +{ |
| 24 | + /** |
| 25 | + * Transforms a string containing an ISO 8601 week date into an array. |
| 26 | + * |
| 27 | + * @param string|null $value A week date string |
| 28 | + * |
| 29 | + * @return array A value containing year and week |
| 30 | + * |
| 31 | + * @throws TransformationFailedException If the given value is not a string, |
| 32 | + * or if the given value does not follow the right format |
| 33 | + */ |
| 34 | + public function transform($value) |
| 35 | + { |
| 36 | + if (null === $value) { |
| 37 | + return ['year' => null, 'week' => null]; |
| 38 | + } |
| 39 | + |
| 40 | + if (!\is_string($value)) { |
| 41 | + throw new TransformationFailedException(sprintf('Value is expected to be a string but was "%s".', \is_object($value) ? \get_class($value) : \gettype($value))); |
| 42 | + } |
| 43 | + |
| 44 | + if (0 === preg_match('/^(?P<year>\d{4})-W(?P<week>\d{2})$/', $value, $matches)) { |
| 45 | + throw new TransformationFailedException('Given data does not follow the date format "Y-\WW".'); |
| 46 | + } |
| 47 | + |
| 48 | + return [ |
| 49 | + 'year' => (int) $matches['year'], |
| 50 | + 'week' => (int) $matches['week'], |
| 51 | + ]; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Transforms an array into a week date string. |
| 56 | + * |
| 57 | + * @param array $value An array containing a year and a week number |
| 58 | + * |
| 59 | + * @return string|null A week date string following the format Y-\WW |
| 60 | + * |
| 61 | + * @throws TransformationFailedException If the given value can not be merged in a valid week date string, |
| 62 | + * or if the obtained week date does not exists |
| 63 | + */ |
| 64 | + public function reverseTransform($value) |
| 65 | + { |
| 66 | + if (null === $value || [] === $value) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + if (!\is_array($value)) { |
| 71 | + throw new TransformationFailedException(sprintf('Value is expected to be an array, but was "%s".', \is_object($value) ? \get_class($value) : \gettype($value))); |
| 72 | + } |
| 73 | + |
| 74 | + if (!\array_key_exists('year', $value)) { |
| 75 | + throw new TransformationFailedException('Key "year" is missing.'); |
| 76 | + } |
| 77 | + |
| 78 | + if (!\array_key_exists('week', $value)) { |
| 79 | + throw new TransformationFailedException('Key "week" is missing.'); |
| 80 | + } |
| 81 | + |
| 82 | + if ($additionalKeys = array_diff(array_keys($value), ['year', 'week'])) { |
| 83 | + throw new TransformationFailedException(sprintf('Expected only keys "year" and "week" to be present, but also got ["%s"].', implode('", "', $additionalKeys))); |
| 84 | + } |
| 85 | + |
| 86 | + if (null === $value['year'] && null === $value['week']) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + if (!\is_int($value['year'])) { |
| 91 | + throw new TransformationFailedException(sprintf('Year is expected to be an integer, but was "%s".', \is_object($value['year']) ? \get_class($value['year']) : \gettype($value['year']))); |
| 92 | + } |
| 93 | + |
| 94 | + if (!\is_int($value['week'])) { |
| 95 | + throw new TransformationFailedException(sprintf('Week is expected to be an integer, but was "%s".', \is_object($value['week']) ? \get_class($value['week']) : \gettype($value['week']))); |
| 96 | + } |
| 97 | + |
| 98 | + // The 28th December is always in the last week of the year |
| 99 | + if (date('W', strtotime('28th December '.$value['year'])) < $value['week']) { |
| 100 | + throw new TransformationFailedException(sprintf('Week "%d" does not exist for year "%d".', $value['week'], $value['year'])); |
| 101 | + } |
| 102 | + |
| 103 | + return sprintf('%d-W%02d', $value['year'], $value['week']); |
| 104 | + } |
| 105 | +} |
0 commit comments