[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/spomky-labs/cbor-php/src/Tag/ -> TagObjectManager.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 function array_key_exists;
  17  use Assert\Assertion;
  18  use CBOR\CBORObject;
  19  use CBOR\TagObject;
  20  use CBOR\Utils;
  21  use InvalidArgumentException;
  22  
  23  class TagObjectManager
  24  {
  25      /**
  26       * @var string[]
  27       */
  28      private $classes = [];
  29  
  30      public function add(string $class): void
  31      {
  32          if ($class::getTagId() < 0) {
  33              throw new InvalidArgumentException('Invalid tag ID.');
  34          }
  35          $this->classes[$class::getTagId()] = $class;
  36      }
  37  
  38      public function getClassForValue(int $value): string
  39      {
  40          return array_key_exists($value, $this->classes) ? $this->classes[$value] : GenericTag::class;
  41      }
  42  
  43      public function createObjectForValue(int $additionalInformation, ?string $data, CBORObject $object): TagObject
  44      {
  45          $value = $additionalInformation;
  46          if ($additionalInformation >= 24) {
  47              Assertion::string($data, 'Invalid data');
  48              $value = Utils::binToInt($data);
  49          }
  50          /** @var TagObject $class */
  51          $class = $this->getClassForValue($value);
  52  
  53          return $class::createFromLoadedData($additionalInformation, $data, $object);
  54      }
  55  }


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