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