[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/spomky-labs/cbor-php/src/ -> MapObject.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 ArrayIterator;
  17  use function count;
  18  use Countable;
  19  use InvalidArgumentException;
  20  use Iterator;
  21  use IteratorAggregate;
  22  
  23  final class MapObject extends AbstractCBORObject implements Countable, IteratorAggregate
  24  {
  25      private const MAJOR_TYPE = 0b101;
  26  
  27      /**
  28       * @var MapItem[]
  29       */
  30      private $data = [];
  31  
  32      /**
  33       * @var int|null
  34       */
  35      private $length;
  36  
  37      /**
  38       * @param MapItem[] $data
  39       */
  40      public function __construct(array $data = [])
  41      {
  42          list($additionalInformation, $length) = LengthCalculator::getLengthOfArray($data);
  43          array_map(static function ($item): void {
  44              if (!$item instanceof MapItem) {
  45                  throw new InvalidArgumentException('The list must contain only MapItem objects.');
  46              }
  47          }, $data);
  48  
  49          parent::__construct(self::MAJOR_TYPE, $additionalInformation);
  50          $this->data = $data;
  51          $this->length = $length;
  52      }
  53  
  54      public function __toString(): string
  55      {
  56          $result = parent::__toString();
  57          if (null !== $this->length) {
  58              $result .= $this->length;
  59          }
  60          foreach ($this->data as $object) {
  61              $result .= (string) $object->getKey();
  62              $result .= (string) $object->getValue();
  63          }
  64  
  65          return $result;
  66      }
  67  
  68      public function add(CBORObject $key, CBORObject $value): void
  69      {
  70          $this->data[] = new MapItem($key, $value);
  71          list($this->additionalInformation, $this->length) = LengthCalculator::getLengthOfArray($this->data);
  72      }
  73  
  74      public function count(): int
  75      {
  76          return count($this->data);
  77      }
  78  
  79      public function getIterator(): Iterator
  80      {
  81          return new ArrayIterator($this->data);
  82      }
  83  
  84      public function getNormalizedData(bool $ignoreTags = false): array
  85      {
  86          $result = [];
  87          foreach ($this->data as $object) {
  88              $result[$object->getKey()->getNormalizedData($ignoreTags)] = $object->getValue()->getNormalizedData($ignoreTags);
  89          }
  90  
  91          return $result;
  92      }
  93  }


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