| 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 |
/** |
| 17 |
* @final |
| 18 |
*/ |
| 19 |
class IndefiniteLengthTextStringObject extends AbstractCBORObject implements Normalizable |
| 20 |
{ |
| 21 |
private const MAJOR_TYPE = self::MAJOR_TYPE_TEXT_STRING; |
| 22 |
|
| 23 |
private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var TextStringObject[] |
| 27 |
*/ |
| 28 |
private $data = []; |
| 29 |
|
| 30 |
public function __construct() |
| 31 |
{ |
| 32 |
parent::__construct(self::MAJOR_TYPE, self::ADDITIONAL_INFORMATION); |
| 33 |
} |
| 34 |
|
| 35 |
public function __toString(): string |
| 36 |
{ |
| 37 |
$result = parent::__toString(); |
| 38 |
foreach ($this->data as $object) { |
| 39 |
$result .= (string) $object; |
| 40 |
} |
| 41 |
|
| 42 |
return $result . "\xFF"; |
| 43 |
} |
| 44 |
|
| 45 |
public static function create(): self |
| 46 |
{ |
| 47 |
return new self(); |
| 48 |
} |
| 49 |
|
| 50 |
public function add(TextStringObject $chunk): self |
| 51 |
{ |
| 52 |
$this->data[] = $chunk; |
| 53 |
|
| 54 |
return $this; |
| 55 |
} |
| 56 |
|
| 57 |
public function append(string $chunk): self |
| 58 |
{ |
| 59 |
$this->add(TextStringObject::create($chunk)); |
| 60 |
|
| 61 |
return $this; |
| 62 |
} |
| 63 |
|
| 64 |
public function getValue(): string |
| 65 |
{ |
| 66 |
$result = ''; |
| 67 |
foreach ($this->data as $object) { |
| 68 |
$result .= $object->getValue(); |
| 69 |
} |
| 70 |
|
| 71 |
return $result; |
| 72 |
} |
| 73 |
|
| 74 |
public function getLength(): int |
| 75 |
{ |
| 76 |
$length = 0; |
| 77 |
foreach ($this->data as $object) { |
| 78 |
$length += $object->getLength(); |
| 79 |
} |
| 80 |
|
| 81 |
return $length; |
| 82 |
} |
| 83 |
|
| 84 |
public function normalize(): string |
| 85 |
{ |
| 86 |
$result = ''; |
| 87 |
foreach ($this->data as $object) { |
| 88 |
$result .= $object->normalize(); |
| 89 |
} |
| 90 |
|
| 91 |
return $result; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @deprecated The method will be removed on v3.0. Please rely on the CBOR\Normalizable interface |
| 96 |
*/ |
| 97 |
public function getNormalizedData(bool $ignoreTags = false): string |
| 98 |
{ |
| 99 |
$result = ''; |
| 100 |
foreach ($this->data as $object) { |
| 101 |
$result .= $object->getNormalizedData($ignoreTags); |
| 102 |
} |
| 103 |
|
| 104 |
return $result; |
| 105 |
} |
| 106 |
} |
| 107 |
|