| 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 InvalidArgumentException; |
| 17 |
|
| 18 |
final class ByteStringWithChunkObject extends AbstractCBORObject |
| 19 |
{ |
| 20 |
private const MAJOR_TYPE = 0b010; |
| 21 |
private const ADDITIONAL_INFORMATION = 0b00011111; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var ByteStringObject[] |
| 25 |
*/ |
| 26 |
private $chunks = []; |
| 27 |
|
| 28 |
public function __construct() |
| 29 |
{ |
| 30 |
parent::__construct(self::MAJOR_TYPE, self::ADDITIONAL_INFORMATION); |
| 31 |
} |
| 32 |
|
| 33 |
public function __toString(): string |
| 34 |
{ |
| 35 |
$result = parent::__toString(); |
| 36 |
foreach ($this->chunks as $chunk) { |
| 37 |
$result .= (string) $chunk; |
| 38 |
} |
| 39 |
$bin = hex2bin('FF'); |
| 40 |
if (false === $bin) { |
| 41 |
throw new InvalidArgumentException('Unable to convert the data'); |
| 42 |
} |
| 43 |
$result .= $bin; |
| 44 |
|
| 45 |
return $result; |
| 46 |
} |
| 47 |
|
| 48 |
public function add(ByteStringObject $chunk): void |
| 49 |
{ |
| 50 |
$this->chunks[] = $chunk; |
| 51 |
} |
| 52 |
|
| 53 |
public function append(string $chunk): void |
| 54 |
{ |
| 55 |
$this->add(new ByteStringObject($chunk)); |
| 56 |
} |
| 57 |
|
| 58 |
public function getValue(): string |
| 59 |
{ |
| 60 |
$result = ''; |
| 61 |
foreach ($this->chunks as $chunk) { |
| 62 |
$result .= $chunk->getValue(); |
| 63 |
} |
| 64 |
|
| 65 |
return $result; |
| 66 |
} |
| 67 |
|
| 68 |
public function getLength(): int |
| 69 |
{ |
| 70 |
$length = 0; |
| 71 |
foreach ($this->chunks as $chunk) { |
| 72 |
$length += $chunk->getLength(); |
| 73 |
} |
| 74 |
|
| 75 |
return $length; |
| 76 |
} |
| 77 |
|
| 78 |
public function getNormalizedData(bool $ignoreTags = false): string |
| 79 |
{ |
| 80 |
$result = ''; |
| 81 |
foreach ($this->chunks as $chunk) { |
| 82 |
$result .= $chunk->getNormalizedData($ignoreTags); |
| 83 |
} |
| 84 |
|
| 85 |
return $result; |
| 86 |
} |
| 87 |
} |
| 88 |
|