Debian Codegraph @

lib/3rdParty/composer/spomky-labs/cbor-php/src/IndefiniteLengthTextStringObject.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace CBOR;
6
7 /**
8 * @see \CBOR\Test\IndefiniteLengthTextStringObjectTest
9 */
10 final class IndefiniteLengthTextStringObject extends AbstractCBORObject implements Normalizable
11 {
12 private const MAJOR_TYPE = self::MAJOR_TYPE_TEXT_STRING;
13
14 private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE;
15
16 /**
17 * @var TextStringObject[]
18 */
19 private array $data = [];
20
21 public function __construct()
22 {
23 parent::__construct(self::MAJOR_TYPE, self::ADDITIONAL_INFORMATION);
24 }
25
26 public function __toString(): string
27 {
28 $result = parent::__toString();
29 foreach ($this->data as $object) {
30 $result .= (string) $object;
31 }
32
33 return $result . "\xFF";
34 }
35
36 public static function create(string ...$chunks): self
37 {
38 $object = new self();
39 foreach ($chunks as $chunk) {
40 $object->append($chunk);
41 }
42
43 return $object;
44 }
45
46 public function add(TextStringObject $chunk): self
47 {
48 $this->data[] = $chunk;
49
50 return $this;
51 }
52
53 public function append(string $chunk): self
54 {
55 $this->add(TextStringObject::create($chunk));
56
57 return $this;
58 }
59
60 public function getValue(): string
61 {
62 $result = '';
63 foreach ($this->data as $object) {
64 $result .= $object->getValue();
65 }
66
67 return $result;
68 }
69
70 public function getLength(): int
71 {
72 $length = 0;
73 foreach ($this->data as $object) {
74 $length += $object->getLength();
75 }
76
77 return $length;
78 }
79
80 public function normalize(): string
81 {
82 $result = '';
83 foreach ($this->data as $object) {
84 $result .= $object->normalize();
85 }
86
87 return $result;
88 }
89 }