Skip to content

Commit

Permalink
Add an option to configure the date_time patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Ben87 committed Feb 8, 2016
1 parent a4e4599 commit 6b2ca60
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
12 changes: 12 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ public function getConfigTreeBuilder()
->scalarNode('trigger_event')
->defaultValue('blur')
->end()
->arrayNode('date_time_pattern')
->useAttributeAsKey('name')
->prototype('scalar')->end()
->end()
->arrayNode('date_pattern')
->useAttributeAsKey('name')
->prototype('scalar')->end()
->end()
->arrayNode('time_pattern')
->useAttributeAsKey('name')
->prototype('scalar')->end()
->end()
->end();

return $treeBuilder;
Expand Down
30 changes: 30 additions & 0 deletions DependencyInjection/ParsleyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('jben87_parsley.global', $config['global']);
$container->setParameter('jben87_parsley.trigger_event', $config['trigger_event']);

$this->setDateTimePatternParameters($config, $container);

$loader = new XmlFileLoader($container, new FileLocator(sprintf('%s/../Resources/config', __DIR__)));
$loader->load('builder.xml');
$loader->load('factory.xml');
Expand All @@ -51,4 +53,32 @@ public function getAlias()
{
return $this->name;
}

/**
* @param array $config
* @param ContainerBuilder $container
*/
private function setDateTimePatternParameters(array $config, ContainerBuilder $container)
{
$locale = $container->getParameter('locale');
$datePattern = '\d{4}-\d{2}-\d{2}';
$timePattern = '\d{2}:\d{2}';
$dateTimePattern = sprintf('%s %s', $datePattern, $timePattern);

if (isset($config['date_time_pattern'][$locale])) {
$dateTimePattern = $config['date_time_pattern'][$locale];
}

if (isset($config['date_pattern'][$locale])) {
$datePattern = $config['date_pattern'][$locale];
}

if (isset($config['time_pattern'][$locale])) {
$timePattern = $config['time_pattern'][$locale];
}

$container->setParameter('jben87_parsley.date_time_format', $dateTimePattern);
$container->setParameter('jben87_parsley.date_format', $datePattern);
$container->setParameter('jben87_parsley.time_format', $timePattern);
}
}
15 changes: 11 additions & 4 deletions Factory/ConstraintFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ class ConstraintFactory
*/
private $translator;

/**
* @var array
*/
private $patterns;

/**
* @param TranslatorInterface $translator
* @param array $patterns
*/
public function __construct(TranslatorInterface $translator)
public function __construct(TranslatorInterface $translator, array $patterns)
{
$this->translator = $translator;
$this->patterns = $patterns;
}

/**
Expand Down Expand Up @@ -66,14 +73,14 @@ public function create(Constraint $constraint)
*/
private function createDateTimeConstraint(Constraint $constraint)
{
$pattern = '\d{4}-\d{2}-\d{2} \d{2}:\d{2}';
$pattern = $this->patterns['date_time'];

if ($constraint instanceof Date) {
$pattern = '\d{4}-\d{2}-\d{2}';
$pattern = $this->patterns['date'];
}

if ($constraint instanceof Time) {
$pattern = '\d{2}:\d{2}';
$pattern = $this->patterns['time'];
}

$options = [
Expand Down
5 changes: 5 additions & 0 deletions Resources/config/factory.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
<service id="jben87_parsley.validator.constraint_factory"
class="JBen87\ParsleyBundle\Factory\ConstraintFactory">
<argument type="service" id="translator"/>
<argument type="collection">
<argument key="date_time">%jben87_parsley.date_time_pattern%</argument>
<argument key="date">%jben87_parsley.date_pattern%</argument>
<argument key="time">%jben87_parsley.time_pattern%</argument>
</argument>
</service>
</services>

Expand Down
12 changes: 11 additions & 1 deletion Tests/Unit/Factory/ConstraintFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class ConstraintFactoryTest extends \PHPUnit_Framework_TestCase
*/
private $normalizer;

/**
* @var array
*/
private $patterns;

/**
* @test
* @expectedException \JBen87\ParsleyBundle\Exception\Validator\UnsupportedConstraintException
Expand Down Expand Up @@ -85,6 +90,11 @@ protected function setUp()
{
$this->translator = $this->prophesize('Symfony\Component\Translation\Translator');
$this->normalizer = $this->prophesize('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
$this->patterns = [
'date_time' => '\d{4}-\d{2}-\d{2} \d{2}:\d{2}',
'date' => '\d{4}-\d{2}-\d{2}',
'time' => '\d{2}:\d{2}',
];
}

/**
Expand Down Expand Up @@ -294,6 +304,6 @@ private function configureTranslator($expected, array $config)
*/
private function createFactory()
{
return new ConstraintFactory($this->translator->reveal());
return new ConstraintFactory($this->translator->reveal(), $this->patterns);
}
}

0 comments on commit 6b2ca60

Please sign in to comment.