| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace CBOR; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
final class IndefiniteLengthByteStringObject extends AbstractCBORObject implements Normalizable |
| 11 |
{ |
| 12 |
private const MAJOR_TYPE = self::MAJOR_TYPE_BYTE_STRING; |
| 13 |
|
| 14 |
private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
private array $chunks = []; |
| 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->chunks as $chunk) { |
| 30 |
$result .= (string) $chunk; |
| 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(ByteStringObject $chunk): self |
| 47 |
{ |
| 48 |
$this->chunks[] = $chunk; |
| 49 |
|
| 50 |
return $this; |
| 51 |
} |
| 52 |
|
| 53 |
public function append(string $chunk): self |
| 54 |
{ |
| 55 |
$this->add(ByteStringObject::create($chunk)); |
| 56 |
|
| 57 |
return $this; |
| 58 |
} |
| 59 |
|
| 60 |
public function getValue(): string |
| 61 |
{ |
| 62 |
$result = ''; |
| 63 |
foreach ($this->chunks as $chunk) { |
| 64 |
$result .= $chunk->getValue(); |
| 65 |
} |
| 66 |
|
| 67 |
return $result; |
| 68 |
} |
| 69 |
|
| 70 |
public function getLength(): int |
| 71 |
{ |
| 72 |
$length = 0; |
| 73 |
foreach ($this->chunks as $chunk) { |
| 74 |
$length += $chunk->getLength(); |
| 75 |
} |
| 76 |
|
| 77 |
return $length; |
| 78 |
} |
| 79 |
|
| 80 |
public function normalize(): string |
| 81 |
{ |
| 82 |
$result = ''; |
| 83 |
foreach ($this->chunks as $chunk) { |
| 84 |
$result .= $chunk->normalize(); |
| 85 |
} |
| 86 |
|
| 87 |
return $result; |
| 88 |
} |
| 89 |
} |