-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
911 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/week_widget.html.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php if ($widget == 'single_text'): ?> | ||
<?php echo $view['form']->block($form, 'form_widget_simple'); ?> | ||
<?php else: ?> | ||
<?php $vars = $widget == 'text' ? ['attr' => ['size' => 1]] : [] ?> | ||
<div <?php echo $view['form']->block($form, 'widget_container_attributes') ?>> | ||
<?php | ||
// There should be no spaces between the colons and the widgets, that's why | ||
// this block is written in a single PHP tag | ||
echo $view['form']->widget($form['year'], $vars); | ||
echo '-'; | ||
echo $view['form']->widget($form['week'], $vars); | ||
?> | ||
</div> | ||
<?php endif ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/Symfony/Component/Form/Extension/Core/DataTransformer/WeekToArrayTransformer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Extension\Core\DataTransformer; | ||
|
||
use Symfony\Component\Form\DataTransformerInterface; | ||
use Symfony\Component\Form\Exception\TransformationFailedException; | ||
|
||
/** | ||
* Transforms between an ISO 8601 week date string and an array. | ||
* | ||
* @author Damien Fayet <damienf1521@gmail.com> | ||
*/ | ||
class WeekToArrayTransformer implements DataTransformerInterface | ||
{ | ||
/** | ||
* Transforms a string containing an ISO 8601 week date into an array. | ||
* | ||
* @param string|null $value A week date string | ||
* | ||
* @return array A value containing year and week | ||
* | ||
* @throws TransformationFailedException If the given value is not a string, | ||
* or if the given value does not follow the right format | ||
*/ | ||
public function transform($value) | ||
{ | ||
if (null === $value) { | ||
return ['year' => null, 'week' => null]; | ||
} | ||
|
||
if (!\is_string($value)) { | ||
throw new TransformationFailedException(sprintf('Value is expected to be a string but was "%s".', \is_object($value) ? \get_class($value) : \gettype($value))); | ||
} | ||
|
||
if (0 === preg_match('/^(?P<year>\d{4})-W(?P<week>\d{2})$/', $value, $matches)) { | ||
throw new TransformationFailedException('Given data does not follow the date format "Y-\WW".'); | ||
} | ||
|
||
return [ | ||
'year' => (int) $matches['year'], | ||
'week' => (int) $matches['week'], | ||
]; | ||
} | ||
|
||
/** | ||
* Transforms an array into a week date string. | ||
* | ||
* @param array $value An array containing a year and a week number | ||
* | ||
* @return string|null A week date string following the format Y-\WW | ||
* | ||
* @throws TransformationFailedException If the given value can not be merged in a valid week date string, | ||
* or if the obtained week date does not exists | ||
*/ | ||
public function reverseTransform($value) | ||
{ | ||
if (null === $value || [] === $value) { | ||
return; | ||
} | ||
|
||
if (!\is_array($value)) { | ||
throw new TransformationFailedException(sprintf('Value is expected to be an array, but was "%s".', \is_object($value) ? \get_class($value) : \gettype($value))); | ||
} | ||
|
||
if (!\array_key_exists('year', $value)) { | ||
throw new TransformationFailedException('Key "year" is missing.'); | ||
} | ||
|
||
if (!\array_key_exists('week', $value)) { | ||
throw new TransformationFailedException('Key "week" is missing.'); | ||
} | ||
|
||
if (null === $value['year'] && null === $value['week']) { | ||
return null; | ||
} | ||
|
||
if (!\is_int($value['year'])) { | ||
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']))); | ||
} | ||
|
||
if (!\is_int($value['week'])) { | ||
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']))); | ||
} | ||
|
||
// The 28th December is always in the last week of the year | ||
if (date('W', strtotime('28th December '.$value['year'])) < $value['week']) { | ||
throw new TransformationFailedException(sprintf('Week "%d" does not exist for year "%d".', $value['week'], $value['year'])); | ||
} | ||
|
||
return sprintf('%d-W%02d', $value['year'], $value['week']); | ||
} | ||
} |
Oops, something went wrong.