[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/spomky-labs/cbor-php/src/ -> SignedIntegerObject.php (source)

   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 GMP;
  18  use InvalidArgumentException;
  19  
  20  final class SignedIntegerObject extends AbstractCBORObject
  21  {
  22      private const MAJOR_TYPE = 0b001;
  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 (null !== $this->data) {
  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 createFromString(string $value): self
  56      {
  57          $integer = BigInteger::of($value);
  58  
  59          return self::createBigInteger($integer);
  60      }
  61  
  62      /**
  63       * @deprecated Deprecated since v1.1 and will be removed in v2.0. Please use "create" or "createFromString" instead
  64       */
  65      public static function createFromGmpValue(GMP $value): self
  66      {
  67          if (gmp_cmp($value, gmp_init(0)) >= 0) {
  68              throw new InvalidArgumentException('The value must be a negative integer.');
  69          }
  70  
  71          $minusOne = gmp_init(-1);
  72          $computed_value = gmp_sub($minusOne, $value);
  73  
  74          switch (true) {
  75              case gmp_intval($computed_value) < 24:
  76                  $ai = gmp_intval($computed_value);
  77                  $data = null;
  78                  break;
  79              case gmp_cmp($computed_value, gmp_init('FF', 16)) < 0:
  80                  $ai = 24;
  81                  $data = self::hex2bin(str_pad(gmp_strval($computed_value, 16), 2, '0', STR_PAD_LEFT));
  82                  break;
  83              case gmp_cmp($computed_value, gmp_init('FFFF', 16)) < 0:
  84                  $ai = 25;
  85                  $data = self::hex2bin(str_pad(gmp_strval($computed_value, 16), 4, '0', STR_PAD_LEFT));
  86                  break;
  87              case gmp_cmp($computed_value, gmp_init('FFFFFFFF', 16)) < 0:
  88                  $ai = 26;
  89                  $data = self::hex2bin(str_pad(gmp_strval($computed_value, 16), 8, '0', STR_PAD_LEFT));
  90                  break;
  91              default:
  92                  throw new InvalidArgumentException('Out of range. Please use NegativeBigIntegerTag tag with ByteStringObject object instead.');
  93          }
  94  
  95          return new self($ai, $data);
  96      }
  97  
  98      public function getValue(): string
  99      {
 100          return $this->getNormalizedData();
 101      }
 102  
 103      public function getNormalizedData(bool $ignoreTags = false): string
 104      {
 105          if (null === $this->data) {
 106              return (string) (-1 - $this->additionalInformation);
 107          }
 108  
 109          $result = Utils::binToBigInteger($this->data);
 110          $minusOne = BigInteger::of(-1);
 111  
 112          return $minusOne->minus($result)->toBase(10);
 113      }
 114  
 115      private static function createBigInteger(BigInteger $integer): self
 116      {
 117          if ($integer->isGreaterThanOrEqualTo(BigInteger::zero())) {
 118              throw new InvalidArgumentException('The value must be a negative integer.');
 119          }
 120  
 121          $minusOne = BigInteger::of(-1);
 122          $computed_value = $minusOne->minus($integer);
 123  
 124          switch (true) {
 125              case $computed_value->isLessThan(BigInteger::of(24)):
 126                  $ai = $computed_value->toInt();
 127                  $data = null;
 128                  break;
 129              case $computed_value->isLessThan(BigInteger::fromBase('FF', 16)):
 130                  $ai = 24;
 131                  $data = self::hex2bin(str_pad($computed_value->toBase(16), 2, '0', STR_PAD_LEFT));
 132                  break;
 133              case $computed_value->isLessThan(BigInteger::fromBase('FFFF', 16)):
 134                  $ai = 25;
 135                  $data = self::hex2bin(str_pad($computed_value->toBase(16), 4, '0', STR_PAD_LEFT));
 136                  break;
 137              case $computed_value->isLessThan(BigInteger::fromBase('FFFFFFFF', 16)):
 138                  $ai = 26;
 139                  $data = self::hex2bin(str_pad($computed_value->toBase(16), 8, '0', STR_PAD_LEFT));
 140                  break;
 141              default:
 142                  throw new InvalidArgumentException('Out of range. Please use NegativeBigIntegerTag tag with ByteStringObject object instead.');
 143          }
 144  
 145          return new self($ai, $data);
 146      }
 147  
 148      private static function hex2bin(string $data): string
 149      {
 150          $result = hex2bin($data);
 151          if (false === $result) {
 152              throw new InvalidArgumentException('Unable to convert the data');
 153          }
 154  
 155          return $result;
 156      }
 157  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer