[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/spomky-labs/cbor-php/src/Tag/ -> BigFloatTag.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\Tag;
  15  
  16  use CBOR\CBORObject;
  17  use CBOR\ListObject;
  18  use CBOR\SignedIntegerObject;
  19  use CBOR\TagObject as Base;
  20  use CBOR\UnsignedIntegerObject;
  21  use function count;
  22  use function extension_loaded;
  23  use InvalidArgumentException;
  24  use RuntimeException;
  25  
  26  final class BigFloatTag extends Base
  27  {
  28      public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
  29      {
  30          if (!extension_loaded('bcmath')) {
  31              throw new RuntimeException('The extension "bcmath" is required to use this tag');
  32          }
  33          parent::__construct($additionalInformation, $data, $object);
  34      }
  35  
  36      public static function getTagId(): int
  37      {
  38          return 5;
  39      }
  40  
  41      public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base
  42      {
  43          return new self($additionalInformation, $data, $object);
  44      }
  45  
  46      public static function create(CBORObject $object): Base
  47      {
  48          if (!$object instanceof ListObject || 2 !== count($object)) {
  49              throw new InvalidArgumentException('This tag only accepts a ListObject object that contains an exponent and a mantissa.');
  50          }
  51          $e = $object->get(0);
  52          if (!$e instanceof UnsignedIntegerObject && !$e instanceof SignedIntegerObject) {
  53              throw new InvalidArgumentException('The exponent must be a Signed Integer or an Unsigned Integer object.');
  54          }
  55          $m = $object->get(1);
  56          if (!$m instanceof UnsignedIntegerObject && !$m instanceof SignedIntegerObject && !$m instanceof NegativeBigIntegerTag && !$m instanceof PositiveBigIntegerTag) {
  57              throw new InvalidArgumentException('The mantissa must be a Positive or Negative Signed Integer or an Unsigned Integer object.');
  58          }
  59  
  60          return new self(5, null, $object);
  61      }
  62  
  63      public static function createFromExponentAndMantissa(CBORObject $e, CBORObject $m): Base
  64      {
  65          $object = new ListObject();
  66          $object->add($e);
  67          $object->add($m);
  68  
  69          return self::create($object);
  70      }
  71  
  72      public function getNormalizedData(bool $ignoreTags = false)
  73      {
  74          if ($ignoreTags) {
  75              return $this->object->getNormalizedData($ignoreTags);
  76          }
  77  
  78          if (!$this->object instanceof ListObject || 2 !== count($this->object)) {
  79              return $this->object->getNormalizedData($ignoreTags);
  80          }
  81          $e = $this->object->get(0);
  82          $m = $this->object->get(1);
  83  
  84          if (!$e instanceof UnsignedIntegerObject && !$e instanceof SignedIntegerObject) {
  85              return $this->object->getNormalizedData($ignoreTags);
  86          }
  87          if (!$m instanceof UnsignedIntegerObject && !$m instanceof SignedIntegerObject && !$m instanceof NegativeBigIntegerTag && !$m instanceof PositiveBigIntegerTag) {
  88              return $this->object->getNormalizedData($ignoreTags);
  89          }
  90  
  91          return rtrim(
  92              bcmul(
  93                  $m->getNormalizedData($ignoreTags),
  94                  bcpow(
  95                      '2',
  96                      $e->getNormalizedData($ignoreTags),
  97                      100),
  98                  100),
  99              '0'
 100          );
 101      }
 102  }


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