Skip to content
This repository has been archived by the owner on Sep 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #347 from OskarStark/deprecations
Browse files Browse the repository at this point in the history
better deprecation handling for BasePickerType
  • Loading branch information
OskarStark committed Sep 27, 2016
2 parents 589f043 + 823950f commit 1b12f54
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
30 changes: 24 additions & 6 deletions Form/Type/BasePickerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
/**
* Class BasePickerType (to factorize DatePickerType and DateTimePickerType code.
*
*
* @author Hugo Briand <briand@ekino.com>
*/
abstract class BasePickerType extends AbstractType
Expand All @@ -35,12 +34,15 @@ abstract class BasePickerType extends AbstractType
* @var string
*/
protected $locale;

/**
* @var MomentFormatConverter
*/
private $formatConverter;

/**
* NEXT_MAJOR: TranslatorInterface needs to be mandatory.
*
* @param MomentFormatConverter $formatConverter
* @param TranslatorInterface $translator
*/
Expand All @@ -49,12 +51,21 @@ public function __construct(MomentFormatConverter $formatConverter, TranslatorIn
$this->formatConverter = $formatConverter;
$this->translator = $translator;

if (null !== $this->translator) {
$this->locale = $this->translator->getLocale();
} else {
@trigger_error('Initializing '.__CLASS__.' without TranslatorInterface is deprecated since 3.0 and will be removed in 4.0.', E_USER_DEPRECATED);
/*
* NEXT_MAJOR: remove this check
*/
if (null === $this->translator) {
@trigger_error(
'Initializing '.__CLASS__.' without TranslatorInterface
is deprecated since 2.4 and will be removed in 4.0.',
E_USER_DEPRECATED
);
$this->locale = \Locale::getDefault();

return;
}

$this->locale = $this->translator->getLocale();
}

/**
Expand All @@ -68,7 +79,14 @@ public function finishView(FormView $view, FormInterface $form, array $options)
$format = $options['date_format'];
} elseif (is_int($format)) {
$timeFormat = ($options['dp_pick_time']) ? DateTimeType::DEFAULT_TIME_FORMAT : \IntlDateFormatter::NONE;
$intlDateFormatter = new \IntlDateFormatter(\Locale::getDefault(), $format, $timeFormat, null, \IntlDateFormatter::GREGORIAN, null);
$intlDateFormatter = new \IntlDateFormatter(
\Locale::getDefault(),
$format,
$timeFormat,
null,
\IntlDateFormatter::GREGORIAN,
null
);
$format = $intlDateFormatter->getPattern();
}

Expand Down
12 changes: 11 additions & 1 deletion Tests/Form/Type/BasePickerTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ protected function getDefaultFormat()
}

/**
* Class BasePickerTypeTest.
*
* @author Hugo Briand <briand@ekino.com>
*/
class BasePickerTypeTest extends \PHPUnit_Framework_TestCase
{
public function testFinishView()
{
$type = new BasePickerTest(new MomentFormatConverter(), $this->getMock('Symfony\Component\Translation\TranslatorInterface'));
$type = new BasePickerTest(
new MomentFormatConverter(),
$this->getMock('Symfony\Component\Translation\TranslatorInterface')
);

$view = new FormView();
$form = new Form($this->getMock('Symfony\Component\Form\FormConfigInterface'));
Expand All @@ -56,6 +61,11 @@ public function testFinishView()
$this->assertSame('text', $view->vars['type']);
}

/**
* NEXT_MAJOR: remove this test.
*
* @group legacy
*/
public function testLegacyConstructor()
{
new BasePickerTest(new MomentFormatConverter());
Expand Down
21 changes: 21 additions & 0 deletions UPGRADE-3.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,24 @@ The exporter class and service are now deprecated in favor of very similar equiv
[`sonata-project/exporter`](https://github.com/sonata-project/exporter) library,
which are available since version 1.6.0,
if you enable the bundle as described in the documentation.

### BasePickerType needs a ``TranslatorInterface`` as 2nd argument

Before:

```php
/**
* @param MomentFormatConverter $formatConverter
*/
public function __construct(MomentFormatConverter $formatConverter)
```

After:

```php
/**
* @param MomentFormatConverter $formatConverter
* @param TranslatorInterface $translator
*/
public function __construct(MomentFormatConverter $formatConverter, TranslatorInterface $translator)
```

0 comments on commit 1b12f54

Please sign in to comment.