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 Base64Url\Base64Url;
17use CBOR\ByteStringObject;
18use CBOR\ByteStringWithChunkObject;
19use CBOR\CBORObject;
20use CBOR\TagObject as Base;
21use CBOR\TextStringObject;
22use CBOR\TextStringWithChunkObject;
23use InvalidArgumentException;
24
25final class Base64UrlEncodingTag extends Base
26{
27    public static function getTagId(): int
28    {
29        return 21;
30    }
31
32    public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base
33    {
34        return new self($additionalInformation, $data, $object);
35    }
36
37    public static function create(CBORObject $object): Base
38    {
39        if (!$object instanceof ByteStringObject && !$object instanceof ByteStringWithChunkObject && !$object instanceof TextStringObject && !$object instanceof TextStringWithChunkObject) {
40            throw new InvalidArgumentException('This tag only accepts Byte String, Infinite Byte String, Text String or Infinite Text String objects.');
41        }
42
43        return new self(21, null, $object);
44    }
45
46    public function getNormalizedData(bool $ignoreTags = false)
47    {
48        if ($ignoreTags) {
49            return $this->object->getNormalizedData($ignoreTags);
50        }
51
52        if (!$this->object instanceof ByteStringObject && !$this->object instanceof ByteStringWithChunkObject && !$this->object instanceof TextStringObject && !$this->object instanceof TextStringWithChunkObject) {
53            return $this->object->getNormalizedData($ignoreTags);
54        }
55
56        return Base64Url::decode($this->object->getNormalizedData($ignoreTags));
57    }
58}
59