| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace CBOR; |
| 6 |
|
| 7 |
use function assert; |
| 8 |
use Brick\Math\BigInteger; |
| 9 |
use InvalidArgumentException; |
| 10 |
use const STR_PAD_LEFT; |
| 11 |
|
| 12 |
final class UnsignedIntegerObject extends AbstractCBORObject implements Normalizable |
| 13 |
{ |
| 14 |
private const MAJOR_TYPE = self::MAJOR_TYPE_UNSIGNED_INTEGER; |
| 15 |
|
| 16 |
public function __construct( |
| 17 |
int $additionalInformation, |
| 18 |
private ?string $data |
| 19 |
) { |
| 20 |
parent::__construct(self::MAJOR_TYPE, $additionalInformation); |
| 21 |
} |
| 22 |
|
| 23 |
public function __toString(): string |
| 24 |
{ |
| 25 |
$result = parent::__toString(); |
| 26 |
if ($this->data !== null) { |
| 27 |
$result .= $this->data; |
| 28 |
} |
| 29 |
|
| 30 |
return $result; |
| 31 |
} |
| 32 |
|
| 33 |
public static function createObjectForValue(int $additionalInformation, ?string $data): self |
| 34 |
{ |
| 35 |
return new self($additionalInformation, $data); |
| 36 |
} |
| 37 |
|
| 38 |
public static function create(int $value): self |
| 39 |
{ |
| 40 |
return self::createFromString((string) $value); |
| 41 |
} |
| 42 |
|
| 43 |
public static function createFromHex(string $value): self |
| 44 |
{ |
| 45 |
assert($value !== '', 'Value must not be empty'); |
| 46 |
$integer = BigInteger::fromBase($value, 16); |
| 47 |
|
| 48 |
return self::createBigInteger($integer); |
| 49 |
} |
| 50 |
|
| 51 |
public static function createFromString(string $value): self |
| 52 |
{ |
| 53 |
$integer = BigInteger::of($value); |
| 54 |
|
| 55 |
return self::createBigInteger($integer); |
| 56 |
} |
| 57 |
|
| 58 |
public function getMajorType(): int |
| 59 |
{ |
| 60 |
return self::MAJOR_TYPE; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function getValue(): string |
| 67 |
{ |
| 68 |
if ($this->data === null) { |
| 69 |
return (string) $this->additionalInformation; |
| 70 |
} |
| 71 |
|
| 72 |
$hex = bin2hex($this->data); |
| 73 |
assert($hex !== '', 'Value must not be empty'); |
| 74 |
|
| 75 |
return BigInteger::fromBase($hex, 16)->toBase(10); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public function normalize(): string |
| 82 |
{ |
| 83 |
return $this->getValue(); |
| 84 |
} |
| 85 |
|
| 86 |
private static function createBigInteger(BigInteger $integer): self |
| 87 |
{ |
| 88 |
if ($integer->isLessThan(BigInteger::zero())) { |
| 89 |
throw new InvalidArgumentException('The value must be a positive integer.'); |
| 90 |
} |
| 91 |
|
| 92 |
switch (true) { |
| 93 |
case $integer->isLessThan(BigInteger::of(24)): |
| 94 |
$ai = $integer->toInt(); |
| 95 |
$data = null; |
| 96 |
break; |
| 97 |
case $integer->isLessThan(BigInteger::fromBase('FF', 16)): |
| 98 |
$ai = 24; |
| 99 |
$data = self::hex2bin(str_pad($integer->toBase(16), 2, '0', STR_PAD_LEFT)); |
| 100 |
break; |
| 101 |
case $integer->isLessThan(BigInteger::fromBase('FFFF', 16)): |
| 102 |
$ai = 25; |
| 103 |
$data = self::hex2bin(str_pad($integer->toBase(16), 4, '0', STR_PAD_LEFT)); |
| 104 |
break; |
| 105 |
case $integer->isLessThan(BigInteger::fromBase('FFFFFFFF', 16)): |
| 106 |
$ai = 26; |
| 107 |
$data = self::hex2bin(str_pad($integer->toBase(16), 8, '0', STR_PAD_LEFT)); |
| 108 |
break; |
| 109 |
default: |
| 110 |
throw new InvalidArgumentException( |
| 111 |
'Out of range. Please use PositiveBigIntegerTag tag with ByteStringObject object instead.' |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
return new self($ai, $data); |
| 116 |
} |
| 117 |
|
| 118 |
private static function hex2bin(string $data): string |
| 119 |
{ |
| 120 |
$result = hex2bin($data); |
| 121 |
if ($result === false) { |
| 122 |
throw new InvalidArgumentException('Unable to convert the data'); |
| 123 |
} |
| 124 |
|
| 125 |
return $result; |
| 126 |
} |
| 127 |
} |