Skip to content

Commit

Permalink
bug #64 Fix FieldTypes view for form rendering (sstok)
Browse files Browse the repository at this point in the history
This PR was merged into the master branch.

Discussion
----------

|Q            |A  |
|---          |---|
|Bug Fix?     |yes|
|New Feature? |no |
|BC Breaks?   |no |
|Deprecations?|no |
|Fixed Tickets|#51|
|License      |MIT|

Commits
-------

a81c3b8 add FieldConfigInterface::createView()
f937d75 remove FieldTypeTestCase::getTestedType()
a833977 update DateTimeType options configuring
c7158c9 [Type] fix configuring SearchFieldView for types
  • Loading branch information
sstok committed Jul 1, 2015
2 parents e4adfe3 + c7158c9 commit 818366b
Show file tree
Hide file tree
Showing 17 changed files with 316 additions and 91 deletions.
10 changes: 10 additions & 0 deletions src/Extension/Core/Type/BirthdayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Rollerworks\Component\Search\AbstractFieldType;
use Rollerworks\Component\Search\Extension\Core\DataTransformer\BirthdayTransformer;
use Rollerworks\Component\Search\FieldConfigInterface;
use Rollerworks\Component\Search\SearchFieldView;
use Rollerworks\Component\Search\ValueComparisonInterface;
use Rollerworks\Component\Search\ValuesBag;
use Symfony\Component\OptionsResolver\Options;
Expand Down Expand Up @@ -57,6 +58,15 @@ public function buildType(FieldConfigInterface $config, array $options)
);
}

/**
* {@inheritdoc}
*/
public function buildView(SearchFieldView $view, FieldConfigInterface $config, array $options)
{
$view->vars['allow_age'] = $options['allow_age'];
$view->vars['allow_future_date'] = $options['allow_future_date'];
}

/**
* {@inheritdoc}
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Extension/Core/Type/ChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Rollerworks\Component\Search\Extension\Core\DataTransformer\ChoiceToLabelTransformer;
use Rollerworks\Component\Search\Extension\Core\DataTransformer\ChoiceToValueTransformer;
use Rollerworks\Component\Search\FieldConfigInterface;
use Rollerworks\Component\Search\SearchFieldView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
Expand Down Expand Up @@ -53,6 +54,15 @@ public function buildType(FieldConfigInterface $config, array $options)
}
}

/**
* {@inheritdoc}
*/
public function buildView(SearchFieldView $view, FieldConfigInterface $config, array $options)
{
$view->vars['choices'] = $options['choice_list'];
$view->vars['label_as_value'] = $options['label_as_value'];
}

/**
* {@inheritdoc}
*/
Expand Down
78 changes: 59 additions & 19 deletions src/Extension/Core/Type/DateTimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
use Rollerworks\Component\Search\Extension\Core\DataTransformer\DateTimeToLocalizedStringTransformer;
use Rollerworks\Component\Search\Extension\Core\DataTransformer\DateTimeToRfc3339Transformer;
use Rollerworks\Component\Search\FieldConfigInterface;
use Rollerworks\Component\Search\SearchFieldView;
use Rollerworks\Component\Search\ValueComparisonInterface;
use Rollerworks\Component\Search\ValuesBag;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
* @author Sebastiaan Stok <s.stok@rollerscapes.net>
Expand All @@ -27,7 +29,6 @@
class DateTimeType extends AbstractFieldType
{
const DEFAULT_DATE_FORMAT = \IntlDateFormatter::MEDIUM;

const DEFAULT_TIME_FORMAT = \IntlDateFormatter::MEDIUM;

/**
Expand Down Expand Up @@ -88,19 +89,23 @@ public function buildType(FieldConfigInterface $config, array $options)
$config->setValueTypeSupport(ValuesBag::VALUE_TYPE_RANGE, true);
$config->setValueTypeSupport(ValuesBag::VALUE_TYPE_COMPARISON, true);

$dateFormat = is_int($options['date_format']) ? $options['date_format'] : self::DEFAULT_DATE_FORMAT;
$timeFormat = self::DEFAULT_TIME_FORMAT;
$calendar = \IntlDateFormatter::GREGORIAN;
$pattern = is_string($options['format']) ? $options['format'] : null;
if (null === $options['pattern']) {
if (!in_array($options['date_format'], self::$acceptedFormats, true)) {
throw new InvalidConfigurationException(
'The "date_format" option must be one of the IntlDateFormatter constants '.
'(FULL, LONG, MEDIUM, SHORT) or the "format" must be a string representing a custom format.'
);
}

if (!in_array($dateFormat, self::$acceptedFormats, true)) {
throw new InvalidConfigurationException(
'The "date_format" option must be one of the IntlDateFormatter constants (FULL, LONG, MEDIUM, SHORT) '.
'or a string representing a custom format.'
);
if (!in_array($options['time_format'], self::$acceptedFormats, true)) {
throw new InvalidConfigurationException(
'The "time_format" option must be one of the IntlDateFormatter constants '.
'(FULL, LONG, MEDIUM, SHORT) or the "format" must be a string representing a custom format.'
);
}
}

if (self::HTML5_FORMAT === $pattern) {
if (self::HTML5_FORMAT === $options['pattern']) {
$config->addViewTransformer(
new DateTimeToRfc3339Transformer(
$options['model_timezone'],
Expand All @@ -112,15 +117,36 @@ public function buildType(FieldConfigInterface $config, array $options)
new DateTimeToLocalizedStringTransformer(
$options['model_timezone'],
$options['view_timezone'],
$dateFormat,
$timeFormat,
$calendar,
$pattern
$options['date_format'],
$options['time_format'],
\IntlDateFormatter::GREGORIAN,
$options['pattern']
)
);
}
}

/**
* {@inheritdoc}
*/
public function buildView(SearchFieldView $view, FieldConfigInterface $config, array $options)
{
$pattern = $options['pattern'];

if (null === $pattern) {
$pattern = \IntlDateFormatter::create(
\Locale::getDefault(),
$options['date_format'],
$options['time_format'],
$options['view_timezone'],
\IntlDateFormatter::GREGORIAN
)->getPattern();
}

$view->vars['timezone'] = $options['view_timezone'] ?: date_default_timezone_get();
$view->vars['pattern'] = $pattern;
}

/**
* {@inheritdoc}
*/
Expand All @@ -130,12 +156,26 @@ public function configureOptions(OptionsResolver $resolver)
array(
'model_timezone' => 'UTC',
'view_timezone' => null,
'format' => self::HTML5_FORMAT,
'date_format' => null,
'with_minutes' => true,
'with_seconds' => false,
'pattern' => null,
'date_format' => self::DEFAULT_DATE_FORMAT,
'time_format' => self::DEFAULT_TIME_FORMAT,
)
);

// BC layer for Symfony 2.7 and 3.0
if ($resolver instanceof OptionsResolverInterface) {
$resolver->setAllowedTypes(
array(
'pattern' => array('string', 'null'),
'date_format' => array('int'),
'time_format' => array('int'),
)
);
} else {
$resolver->setAllowedTypes('pattern', array('string', 'null'));
$resolver->setAllowedTypes('date_format', array('int'));
$resolver->setAllowedTypes('time_format', array('int'));
}
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/Extension/Core/Type/DateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Rollerworks\Component\Search\Exception\InvalidConfigurationException;
use Rollerworks\Component\Search\Extension\Core\DataTransformer\DateTimeToLocalizedStringTransformer;
use Rollerworks\Component\Search\FieldConfigInterface;
use Rollerworks\Component\Search\SearchFieldView;
use Rollerworks\Component\Search\ValueComparisonInterface;
use Rollerworks\Component\Search\ValuesBag;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down Expand Up @@ -99,6 +100,26 @@ public function buildType(FieldConfigInterface $config, array $options)
);
}

/**
* {@inheritdoc}
*/
public function buildView(SearchFieldView $view, FieldConfigInterface $config, array $options)
{
if (is_string($options['format'])) {
$pattern = $options['format'];
} else {
$pattern = \IntlDateFormatter::create(
\Locale::getDefault(),
is_int($options['format']) ? $options['format'] : self::DEFAULT_FORMAT,
\IntlDateFormatter::NONE,
null,
\IntlDateFormatter::GREGORIAN
)->getPattern();
}

$view->vars['pattern'] = $pattern;
}

/**
* {@inheritdoc}
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Extension/Core/Type/IntegerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Rollerworks\Component\Search\AbstractFieldType;
use Rollerworks\Component\Search\Extension\Core\DataTransformer\IntegerToLocalizedStringTransformer;
use Rollerworks\Component\Search\FieldConfigInterface;
use Rollerworks\Component\Search\SearchFieldView;
use Rollerworks\Component\Search\ValueComparisonInterface;
use Rollerworks\Component\Search\ValuesBag;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down Expand Up @@ -56,6 +57,15 @@ public function buildType(FieldConfigInterface $config, array $options)
);
}

/**
* {@inheritdoc}
*/
public function buildView(SearchFieldView $view, FieldConfigInterface $config, array $options)
{
$view->vars['precision'] = $options['precision'];
$view->vars['grouping'] = $options['grouping'];
}

/**
* {@inheritdoc}
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Extension/Core/Type/MoneyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Rollerworks\Component\Search\AbstractFieldType;
use Rollerworks\Component\Search\Extension\Core\DataTransformer\MoneyToLocalizedStringTransformer;
use Rollerworks\Component\Search\FieldConfigInterface;
use Rollerworks\Component\Search\SearchFieldView;
use Rollerworks\Component\Search\ValueComparisonInterface;
use Rollerworks\Component\Search\ValuesBag;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down Expand Up @@ -57,6 +58,17 @@ public function buildType(FieldConfigInterface $config, array $options)
);
}

/**
* {@inheritdoc}
*/
public function buildView(SearchFieldView $view, FieldConfigInterface $config, array $options)
{
$view->vars['precision'] = $options['precision'];
$view->vars['grouping'] = $options['grouping'];
$view->vars['divisor'] = $options['divisor'];
$view->vars['default_currency'] = $options['default_currency'];
}

/**
* {@inheritdoc}
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Extension/Core/Type/NumberType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Rollerworks\Component\Search\AbstractFieldType;
use Rollerworks\Component\Search\Extension\Core\DataTransformer\NumberToLocalizedStringTransformer;
use Rollerworks\Component\Search\FieldConfigInterface;
use Rollerworks\Component\Search\SearchFieldView;
use Rollerworks\Component\Search\ValueComparisonInterface;
use Rollerworks\Component\Search\ValuesBag;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down Expand Up @@ -56,6 +57,15 @@ public function buildType(FieldConfigInterface $config, array $options)
);
}

/**
* {@inheritdoc}
*/
public function buildView(SearchFieldView $view, FieldConfigInterface $config, array $options)
{
$view->vars['precision'] = $options['precision'];
$view->vars['grouping'] = $options['grouping'];
}

/**
* {@inheritdoc}
*/
Expand Down
21 changes: 21 additions & 0 deletions src/Extension/Core/Type/TimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Rollerworks\Component\Search\Exception\InvalidConfigurationException;
use Rollerworks\Component\Search\Extension\Core\DataTransformer\DateTimeToStringTransformer;
use Rollerworks\Component\Search\FieldConfigInterface;
use Rollerworks\Component\Search\SearchFieldView;
use Rollerworks\Component\Search\ValueComparisonInterface;
use Rollerworks\Component\Search\ValuesBag;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down Expand Up @@ -70,6 +71,26 @@ public function buildType(FieldConfigInterface $config, array $options)
);
}

/**
* {@inheritdoc}
*/
public function buildView(SearchFieldView $view, FieldConfigInterface $config, array $options)
{
$pattern = 'H';

if ($options['with_minutes']) {
$pattern .= ':i';
}

if ($options['with_seconds']) {
$pattern .= ':s';
}

$view->vars['pattern'] = $pattern;
$view->vars['with_minutes'] = $options['with_minutes'];
$view->vars['with_seconds'] = $options['with_seconds'];
}

/**
* {@inheritdoc}
*/
Expand Down
7 changes: 7 additions & 0 deletions src/FieldConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,11 @@ public function hasOption($name);
* @return mixed The option value.
*/
public function getOption($name, $default = null);

/**
* Returns a new SearchFieldView for the SearchField.
*
* @return SearchFieldView
*/
public function createView();
}
17 changes: 17 additions & 0 deletions src/SearchField.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,21 @@ public function getOption($name, $default = null)

return $default;
}

/**
* {@inheritdoc}
*/
public function createView()
{
if (!$this->locked) {
throw new BadMethodCallException(
'Unable to create SearchFieldView when configuration is not locked.'
);
}

$view = new SearchFieldView();
$this->type->buildFieldView($view, $this, $this->options);

return $view;
}
}
5 changes: 0 additions & 5 deletions src/Test/FieldTypeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ protected function formatInput(FieldConfigInterface $field, $input)
return array($normValue, $viewValue);
}

/**
* @return string
*/
abstract protected function getTestedType();

/**
* Transforms the value if a value transformer is set.
*
Expand Down
2 changes: 0 additions & 2 deletions tests/Extension/Core/Type/BirthdayTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ public function testAgeInWorksWhenAllowed()

protected function setUp()
{
IntlTestHelper::requireIntl($this);

parent::setUp();
}

Expand Down

0 comments on commit 818366b

Please sign in to comment.