Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 committed Jul 1, 2022
1 parent c539cef commit 4c500c9
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 115 deletions.
24 changes: 1 addition & 23 deletions src/Bridge/Symfony/Resources/config/form_types.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,19 @@

->set('sonata.form.type.date_range', DateRangeType::class)
->tag('form.type', ['alias' => 'sonata_type_date_range'])
->args([
service('translator'),
])

->set('sonata.form.type.datetime_range', DateTimeRangeType::class)
->tag('form.type', ['alias' => 'sonata_type_datetime_range'])
->args([
service('translator'),
])

->set('sonata.form.type.date_picker', DatePickerType::class)
->tag('kernel.locale_aware')
->tag('form.type', ['alias' => 'sonata_type_date_picker'])
->args([
service('translator'),
param('kernel.default_locale'),
])

->set('sonata.form.type.datetime_picker', DateTimePickerType::class)
->tag('kernel.locale_aware')
->tag('form.type', ['alias' => 'sonata_type_datetime_picker'])
->args([
service('translator'),
param('kernel.default_locale'),
])

->set('sonata.form.type.date_range_picker', DateRangePickerType::class)
->tag('form.type', ['alias' => 'sonata_type_date_range_picker'])
->args([
service('translator'),
])

->set('sonata.form.type.datetime_range_picker', DateTimeRangePickerType::class)
->tag('form.type', ['alias' => 'sonata_type_datetime_range_picker'])
->args([
service('translator'),
]);
->tag('form.type', ['alias' => 'sonata_type_datetime_range_picker']);
};
77 changes: 2 additions & 75 deletions src/Type/BasePickerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\LocaleAwareInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* Class BasePickerType (to factorize DatePickerType and DateTimePickerType code.
*
* @author Hugo Briand <[email protected]>
*/
abstract class BasePickerType extends AbstractType implements LocaleAwareInterface
abstract class BasePickerType extends AbstractType
{
/**
* @var array<string, array<string>|string>
Expand Down Expand Up @@ -68,43 +65,8 @@ abstract class BasePickerType extends AbstractType implements LocaleAwareInterfa
'monthSelectorType' => 'string',
];

public function __construct(
protected TranslatorInterface $translator,
protected string $locale
) {
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setNormalizer(
'format',
function (Options $options, int|string $format) {
if (isset($options['date_format']) && \is_string($options['date_format'])) {
return $options['date_format'];
}

if (\is_int($format)) {
$timeFormat = \IntlDateFormatter::NONE;
// if (true === $options['dp_pick_time']) {
// $timeFormat = true === $options['dp_use_seconds'] ?
// DateTimeType::DEFAULT_TIME_FORMAT :
// \IntlDateFormatter::SHORT;
// }
$intlDateFormatter = new \IntlDateFormatter(
$this->locale,
$format,
$timeFormat,
null,
\IntlDateFormatter::GREGORIAN
);

return $intlDateFormatter->getPattern();
}

return $format;
}
);

$resolver->setDefault('datepicker_options', function (OptionsResolver $datePickerResolver) {
$dateTimeNormalizer = static function (OptionsResolver $options, string|array|\DateTimeInterface $value): string|array {
if ($value instanceof \DateTimeInterface) {
Expand Down Expand Up @@ -142,33 +104,10 @@ function (Options $options, int|string $format) {

public function finishView(FormView $view, FormInterface $form, array $options): void
{
$format = $options['format'];

// use seconds if it's allowed in format
// $options['dp_use_seconds'] = str_contains($format, 's');

// if ($options['dp_min_date'] instanceof \DateTimeInterface) {
// $options['dp_min_date'] = $this->formatObject($options['dp_min_date'], $format);
// }
// if ($options['dp_max_date'] instanceof \DateTimeInterface) {
// $options['dp_max_date'] = $this->formatObject($options['dp_max_date'], $format);
// }

$view->vars['type'] = 'text';
$view->vars['datepicker_use_button'] = $options['datepicker_use_button'] ?? false;
$view->vars['datepicker_options'] = $options['datepicker_options'];
}

public function getLocale(): string
{
return $this->locale;
}

public function setLocale(string $locale): void
{
$this->locale = $locale;
}

/**
* Gets base default options for the form types
* (except `datepicker_options` which should be handled with `getCommonDatepickerDefaults()`).
Expand All @@ -180,6 +119,7 @@ protected function getCommonDefaults(): array
return [
'widget' => 'single_text',
'datepicker_use_button' => true,
'html5' => false,
];
}

Expand Down Expand Up @@ -227,17 +167,4 @@ protected function getCommonDatepickerDefaults(): array
// 'monthSelectorType' => 'dropdown',
];
}

// private function formatObject(\DateTimeInterface $dateTime, string $format): string
// {
// $formatter = new \IntlDateFormatter($this->locale, \IntlDateFormatter::NONE, \IntlDateFormatter::NONE);
// $formatter->setPattern($format);

// $formatted = $formatter->format($dateTime);
// if (!\is_string($formatted)) {
// throw new \RuntimeException(sprintf('The format "%s" is invalid.', $format));
// }

// return $formatted;
// }
}
8 changes: 0 additions & 8 deletions src/Type/DatePickerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,4 @@ public function getBlockPrefix(): string
{
return 'sonata_type_datetime_picker';
}

protected function getCommonDefaults(): array
{
return array_merge(parent::getCommonDefaults(), [
'format' => DateType::DEFAULT_FORMAT,
'html5' => false,
]);
}
}
9 changes: 0 additions & 9 deletions src/Type/DateTimePickerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@ public function getBlockPrefix(): string
return 'sonata_type_datetime_picker';
}

protected function getCommonDefaults(): array
{
return array_merge(parent::getCommonDefaults(), [
'format' => DateTimeType::DEFAULT_DATE_FORMAT,
'date_format' => null,
'html5' => false,
]);
}

protected function getCommonDatepickerDefaults(): array
{
return array_merge(parent::getCommonDatepickerDefaults(), [
Expand Down

0 comments on commit 4c500c9

Please sign in to comment.