[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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


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