1<?php
2/**
3 * This file is part of the FreeDSx ASN1 package.
4 *
5 * (c) Chad Sikorra <Chad.Sikorra@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11namespace FreeDSx\Asn1\Type;
12
13/**
14 * Represents a Generalized Time type.
15 *
16 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
17 */
18class GeneralizedTimeType extends AbstractTimeType
19{
20    /**
21     * GeneralizedTime defined as:
22     *
23     * a)  A string representing the calendar date, as specified in ISO 8601, with a four-digit representation of the
24     *     year, a two-digit representation of the month and a two-digit representation of the day, without use of
25     *     separators, followed by a string representing the time of day, as specified in ISO 8601, without separators
26     *     other than decimal comma or decimal period (as provided for in ISO 8601), and with no terminating Z (as
27     *     provided for in ISO 8601); or
28     *
29     * b)  the characters in a) above followed by an upper-case letter Z; or
30     *
31     * c)  the characters in a) above followed by a string representing a local time differential, as specified in
32     *     ISO 8601, without separators
33     */
34    public const TIME_REGEX = '~^
35        (\d\d\d\d)              # 1  - Year
36        (\d\d)                  # 2  - Month
37        (\d\d)                  # 3  - Day
38        (\d\d)                  # 4  - Hour
39        ((\d\d)                 # 6  - Minutes, capture group before since all others are optional
40            ((\d\d)             # 8  - Seconds, capture group as this can be optional
41                ((\.\d{1,}))?   # 10 - Fractions of seconds, also optional.
42            )?                  # ---- End of seconds capture group.
43        )?                      # ---- End of minutes capture group.
44        (Z|[\+\-]\d\d\d\d)?     # 11 - Timezone modifier (optional). It can either be a Z (UTC) or a time differential.
45    $~x';
46
47    public const REGEX_MAP = [
48        'hours' => 4,
49        'minutes' => 6,
50        'seconds' => 8,
51        'fractions' => 10,
52        'timezone' => 11,
53    ];
54
55    protected $tagNumber = self::TAG_TYPE_GENERALIZED_TIME;
56
57    /**
58     * Valid datetime formats.
59     */
60    protected $validDateFormats = [
61        self::FORMAT_HOURS,
62        self::FORMAT_MINUTES,
63        self::FORMAT_SECONDS,
64        self::FORMAT_FRACTIONS,
65    ];
66
67    /**
68     * Valid timezone formats
69     */
70    protected $validTzFormats = [
71        self::TZ_UTC,
72        self::TZ_DIFF,
73        self::TZ_LOCAL,
74    ];
75
76    /**
77     * {@inheritdoc}
78     */
79    public function __construct(?\DateTime $dateTime = null, string $dateFormat = self::FORMAT_FRACTIONS, string $tzFormat = self::TZ_UTC)
80    {
81        parent::__construct($dateTime, $dateFormat, $tzFormat);
82    }
83}
84