| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
/* |
| 6 |
* The MIT License (MIT) |
| 7 |
* |
| 8 |
* Copyright (c) 2018-2020 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 |
|
| 14 |
namespace WP_DEFENDER_VENDOR\CBOR; |
| 15 |
|
| 16 |
use InvalidArgumentException; |
| 17 |
|
| 18 |
abstract class Tag extends AbstractCBORObject |
| 19 |
{ |
| 20 |
private const MAJOR_TYPE = self::MAJOR_TYPE_TAG; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var string|null |
| 24 |
*/ |
| 25 |
protected $data; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var CBORObject |
| 29 |
*/ |
| 30 |
protected $object; |
| 31 |
|
| 32 |
public function __construct(int $additionalInformation, ?string $data, CBORObject $object) |
| 33 |
{ |
| 34 |
parent::__construct(self::MAJOR_TYPE, $additionalInformation); |
| 35 |
$this->data = $data; |
| 36 |
$this->object = $object; |
| 37 |
} |
| 38 |
|
| 39 |
public function __toString(): string |
| 40 |
{ |
| 41 |
$result = parent::__toString(); |
| 42 |
if ($this->data !== null) { |
| 43 |
$result .= $this->data; |
| 44 |
} |
| 45 |
|
| 46 |
return $result . $this->object; |
| 47 |
} |
| 48 |
|
| 49 |
public function getData(): ?string |
| 50 |
{ |
| 51 |
return $this->data; |
| 52 |
} |
| 53 |
|
| 54 |
abstract public static function getTagId(): int; |
| 55 |
|
| 56 |
abstract public static function createFromLoadedData( |
| 57 |
int $additionalInformation, |
| 58 |
?string $data, |
| 59 |
CBORObject $object |
| 60 |
): self; |
| 61 |
|
| 62 |
public function getValue(): CBORObject |
| 63 |
{ |
| 64 |
return $this->object; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @return array{int, null|string} |
| 69 |
*/ |
| 70 |
protected static function determineComponents(int $tag): array |
| 71 |
{ |
| 72 |
switch (true) { |
| 73 |
case $tag < 0: |
| 74 |
throw new InvalidArgumentException('The value must be a positive integer.'); |
| 75 |
case $tag < 24: |
| 76 |
return [$tag, null]; |
| 77 |
case $tag < 0xFF: |
| 78 |
return [24, self::hex2bin(dechex($tag))]; |
| 79 |
case $tag < 0xFFFF: |
| 80 |
return [25, self::hex2bin(dechex($tag))]; |
| 81 |
case $tag < 0xFFFFFFFF: |
| 82 |
return [26, self::hex2bin(dechex($tag))]; |
| 83 |
default: |
| 84 |
throw new InvalidArgumentException( |
| 85 |
'Out of range. Please use PositiveBigIntegerTag tag with ByteStringObject object instead.' |
| 86 |
); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
private static function hex2bin(string $data): string |
| 91 |
{ |
| 92 |
$result = hex2bin($data); |
| 93 |
if ($result === false) { |
| 94 |
throw new InvalidArgumentException('Unable to convert the data'); |
| 95 |
} |
| 96 |
|
| 97 |
return $result; |
| 98 |
} |
| 99 |
} |
| 100 |
|