Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ParserJavascriptDateTime
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 parseValue
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 setMsgContent
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * @author: Doug Wilbourne (dougwilbourne@gmail.com)
5 */
6
7declare(strict_types=1);
8
9namespace pvc\parser\date_time;
10
11use DateMalformedStringException;
12use DateTimeImmutable;
13use DateTimeZone;
14use pvc\interfaces\msg\MsgInterface;
15use pvc\parser\Parser;
16
17/**
18 * Class ParserJavascriptDateTime
19 * @extends Parser<float>
20 *
21 * A fully formed JavascriptDateTime string looks like '2012-07-15T13:54:56Z-05:00'.  The timezone info
22 * at the end of the string is optional - the local timezone is assumed if none is specified.
23 * DateTimeImmutable will parse timezone info of all kinds
24 */
25class ParserJavascriptDateTime extends Parser
26{
27    protected DateTimeZone $tz;
28
29    public function __construct(MsgInterface $msg)
30    {
31        parent::__construct($msg);
32    }
33
34    protected function parseValue(string $data): bool
35    {
36        $data = str_replace('Z', '', $data);
37        try {
38            /**
39             * if tz is ommitted, the local timezone is assumed EXCEPT if the timezone info is specified
40             * in the input string.  If the input string contains timezone info, the timezone parameter
41             * to the constructor is ignored.
42             */
43            $dt = new DateTimeImmutable($data);
44        } catch (DateMalformedStringException $e) {
45            return false;
46        }
47
48        $this->parsedValue = $dt->getTimestamp();
49        return true;
50    }
51
52    /**
53     * setMsgContent
54     * @param MsgInterface $msg
55     */
56    protected function setMsgContent(MsgInterface $msg): void
57    {
58        $msgId = 'not_javascript_datetime';
59        $msgParameters = [];
60        $msg->setContent($this->getMsgDomain(), $msgId, $msgParameters);
61    }
62}