|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Marek\SiteAccessAwareDateFormatBundle\Core; |
|
6
|
|
|
|
|
7
|
|
|
use DateTimeInterface; |
|
8
|
|
|
use eZ\Publish\Core\MVC\ConfigResolverInterface; |
|
9
|
|
|
use eZ\Publish\Core\MVC\Symfony\Locale\LocaleConverterInterface; |
|
10
|
|
|
use IntlDateFormatter; |
|
11
|
|
|
use Marek\SiteAccessAwareDateFormatBundle\API\DateFormatter; |
|
12
|
|
|
use Marek\SiteAccessAwareDateFormatBundle\DependencyInjection\Configuration; |
|
13
|
|
|
|
|
14
|
|
|
final class DefaultDateFormatter implements DateFormatter |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var \eZ\Publish\Core\MVC\Symfony\Locale\LocaleConverterInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
private $localeConverter; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
private $languages; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var \eZ\Publish\Core\MVC\ConfigResolverInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $configResolver; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* DefaultDateFormatter constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param \eZ\Publish\Core\MVC\Symfony\Locale\LocaleConverterInterface $localeConverter |
|
35
|
|
|
* @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver |
|
36
|
|
|
* @param array $languages |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(LocaleConverterInterface $localeConverter, ConfigResolverInterface $configResolver, array $languages) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->localeConverter = $localeConverter; |
|
41
|
|
|
$this->languages = $languages; |
|
42
|
|
|
$this->configResolver = $configResolver; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
public function format(DateTimeInterface $date, string $dateFormatIdentifier): string |
|
49
|
|
|
{ |
|
50
|
|
|
$defaultFormat = 'short'; |
|
51
|
|
|
if ($this->configResolver->hasParameter('defaults', Configuration::TREE_ROOT)) { |
|
52
|
|
|
$defaultFormat = $this->configResolver->getParameter('defaults', Configuration::TREE_ROOT); |
|
53
|
|
|
$defaultFormat = $defaultFormat['format']; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
switch ($defaultFormat) { |
|
57
|
|
|
case 'none': |
|
58
|
|
|
$dateConstant = IntlDateFormatter::NONE; |
|
59
|
|
|
|
|
60
|
|
|
break; |
|
61
|
|
|
case 'full': |
|
62
|
|
|
$dateConstant = IntlDateFormatter::FULL; |
|
63
|
|
|
|
|
64
|
|
|
break; |
|
65
|
|
|
case 'long': |
|
66
|
|
|
$dateConstant = IntlDateFormatter::LONG; |
|
67
|
|
|
|
|
68
|
|
|
break; |
|
69
|
|
|
case 'medium': |
|
70
|
|
|
$dateConstant = IntlDateFormatter::MEDIUM; |
|
71
|
|
|
|
|
72
|
|
|
break; |
|
73
|
|
|
default: |
|
74
|
|
|
$dateConstant = IntlDateFormatter::SHORT; |
|
75
|
|
|
|
|
76
|
|
|
break; |
|
77
|
|
|
} |
|
78
|
|
|
$formatter = new IntlDateFormatter( |
|
79
|
|
|
$this->localeConverter->convertToPOSIX($this->languages[0]), |
|
80
|
|
|
$dateConstant, |
|
81
|
|
|
$dateConstant |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
return $formatter->format($date); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: