PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.0.1
Defender Security – Malware Scanner, Login Security & Firewall v3.0.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / vendor / spomky-labs / cbor-php / src / MapObject.php

MapObject.php in Defender Security – Malware Scanner, Login Security & Firewall 3.0.1, at vendor/spomky-labs/cbor-php/src/MapObject.php

94 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }
94