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