Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
11.11% |
2 / 18 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
TimestampFunction | |
11.11% |
2 / 18 |
|
50.00% |
2 / 4 |
52.95 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createHtml | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getFunctionName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
functionBodyImpl | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\Core\Mvc\Template\Plugin; |
6 | |
7 | use DateTime; |
8 | use DateTimeInterface; |
9 | use DateTimeZone; |
10 | use PeServer\Core\Mvc\Template\Plugin\TemplatePluginArgument; |
11 | use PeServer\Core\Throws\ArgumentException; |
12 | |
13 | class TimestampFunction extends TemplateFunctionBase |
14 | { |
15 | public function __construct(TemplatePluginArgument $argument) |
16 | { |
17 | parent::__construct($argument); |
18 | } |
19 | |
20 | #region function |
21 | |
22 | //@phpstan-ignore method.unused |
23 | private function createHtml(DateTimeInterface $utc, DateTimeInterface $timestamp, string $format): string |
24 | { |
25 | $datetime = $utc->format(DateTimeInterface::ISO8601_EXPANDED); |
26 | $display = $timestamp->format($format); |
27 | return "<time datetime='$datetime'>$display</time>"; |
28 | } |
29 | |
30 | #endregion |
31 | |
32 | #region ITemplateModifier |
33 | |
34 | public function getFunctionName(): string |
35 | { |
36 | return 'timestamp'; |
37 | } |
38 | |
39 | #endregion |
40 | |
41 | #region TemplateFunctionBase |
42 | |
43 | protected function functionBodyImpl(): string |
44 | { |
45 | //@phpstan-ignore booleanOr.alwaysTrue, instanceof.alwaysFalse |
46 | if (!isset($this->params['value']) || !($this->params['value'] instanceof DateTimeInterface)) { |
47 | if (isset($this->params['fallback'])) { |
48 | /** @var string */ |
49 | return $this->params['fallback']; |
50 | } |
51 | throw new ArgumentException('value'); |
52 | } |
53 | |
54 | /** @var DateTimeInterface */ |
55 | $value = $this->params['value']; //@phpstan-ignore deadCode.unreachable |
56 | |
57 | /** @var string */ |
58 | $timezoneText = $this->params['timezone'] ?? 'Asia/Tokyo'; |
59 | $timezone = new DateTimeZone($timezoneText); |
60 | |
61 | $work = DateTime::createFromInterface($value); |
62 | $work->setTimezone($timezone); |
63 | |
64 | if (isset($this->params['format'])) { |
65 | /** @var string */ |
66 | $format = $this->params['format']; |
67 | return $this->createHtml($value, $work, $format); |
68 | } |
69 | |
70 | return $this->createHtml($value, $work, 'Y-m-d\\TH:i:sP'); |
71 | } |
72 | |
73 | #endregion |
74 | } |