1<?php
2
3declare(strict_types=1);
4
5/*
6 * The MIT License (MIT)
7 *
8 * Copyright (c) 2018 Spomky-Labs
9 *
10 * This software may be modified and distributed under the terms
11 * of the MIT license.  See the LICENSE file for details.
12 */
13
14namespace CBOR\Tag;
15
16use CBOR\CBORObject;
17use CBOR\OtherObject\DoublePrecisionFloatObject;
18use CBOR\OtherObject\HalfPrecisionFloatObject;
19use CBOR\OtherObject\SinglePrecisionFloatObject;
20use CBOR\TagObject as Base;
21use CBOR\UnsignedIntegerObject;
22use InvalidArgumentException;
23
24final class TimestampTag extends Base
25{
26    public static function getTagId(): int
27    {
28        return 1;
29    }
30
31    public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base
32    {
33        return new self($additionalInformation, $data, $object);
34    }
35
36    public static function create(CBORObject $object): Base
37    {
38        if (!$object instanceof UnsignedIntegerObject && !$object instanceof HalfPrecisionFloatObject && !$object instanceof SinglePrecisionFloatObject && !$object instanceof DoublePrecisionFloatObject) {
39            throw new InvalidArgumentException('This tag only accepts a Byte String object.');
40        }
41
42        return new self(1, null, $object);
43    }
44
45    public function getNormalizedData(bool $ignoreTags = false)
46    {
47        if ($ignoreTags) {
48            return $this->object->getNormalizedData($ignoreTags);
49        }
50        switch (true) {
51            case $this->object instanceof UnsignedIntegerObject:
52                return \DateTimeImmutable::createFromFormat('U', \strval($this->object->getNormalizedData($ignoreTags)));
53            case $this->object instanceof HalfPrecisionFloatObject:
54            case $this->object instanceof SinglePrecisionFloatObject:
55            case $this->object instanceof DoublePrecisionFloatObject:
56                return \DateTimeImmutable::createFromFormat('U.u', \strval($this->object->getNormalizedData($ignoreTags)));
57            default:
58                return $this->object->getNormalizedData($ignoreTags);
59        }
60    }
61}
62