| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace CBOR; |
| 6 |
|
| 7 |
use CBOR\OtherObject\DoublePrecisionFloatObject; |
| 8 |
use CBOR\OtherObject\FalseObject; |
| 9 |
use CBOR\OtherObject\NullObject; |
| 10 |
use CBOR\OtherObject\SinglePrecisionFloatObject; |
| 11 |
use CBOR\OtherObject\TrueObject; |
| 12 |
use InvalidArgumentException; |
| 13 |
use function is_array; |
| 14 |
use function is_float; |
| 15 |
use function is_int; |
| 16 |
use function is_string; |
| 17 |
|
| 18 |
final class Encoder implements EncoderInterface |
| 19 |
{ |
| 20 |
public const FLOAT_FORMAT_SINGLE_PRECISION = 0b0000001; |
| 21 |
|
| 22 |
public const INDEFINITE_BYTE_STRING_LENGTH = 0b00000010; |
| 23 |
|
| 24 |
public const INDEFINITE_TEXT_STRING_LENGTH = 0b00000100; |
| 25 |
|
| 26 |
public const INDEFINITE_LIST_LENGTH = 0b00001000; |
| 27 |
|
| 28 |
public const INDEFINITE_MAP_LENGTH = 0b00010000; |
| 29 |
|
| 30 |
public function encode(mixed $data, int $options = 0): string |
| 31 |
{ |
| 32 |
return (string) $this->processData($data, $options); |
| 33 |
} |
| 34 |
|
| 35 |
private function processData(mixed $data, int $option): CBORObject |
| 36 |
{ |
| 37 |
return match (true) { |
| 38 |
$data instanceof CBORObject => $data, |
| 39 |
is_string($data) => preg_match('//u', $data) === 1 ? $this->processTextString( |
| 40 |
$data, |
| 41 |
$option |
| 42 |
) : $this->processByteString($data, $option), |
| 43 |
is_array($data) => array_is_list($data) ? $this->processList($data, $option) : $this->processMap( |
| 44 |
$data, |
| 45 |
$option |
| 46 |
), |
| 47 |
is_int($data) => $data < 0 ? NegativeIntegerObject::create($data) : UnsignedIntegerObject::create($data), |
| 48 |
is_float($data) => $this->processFloat($data, $option), |
| 49 |
$data === null => NullObject::create(), |
| 50 |
$data === false => FalseObject::create(), |
| 51 |
$data === true => TrueObject::create(), |
| 52 |
default => throw new InvalidArgumentException('Unsupported data type'), |
| 53 |
}; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
private function processList(array $data, int $option): ListObject|IndefiniteLengthListObject |
| 60 |
{ |
| 61 |
$isIndefinite = 0 !== ($option & self::INDEFINITE_LIST_LENGTH); |
| 62 |
$list = $isIndefinite ? IndefiniteLengthListObject::create() : ListObject::create(); |
| 63 |
foreach ($data as $item) { |
| 64 |
$list->add($this->processData($item, $option)); |
| 65 |
} |
| 66 |
|
| 67 |
return $list; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
private function processMap(array $data, int $option): MapObject|IndefiniteLengthMapObject |
| 74 |
{ |
| 75 |
$isIndefinite = 0 !== ($option & self::INDEFINITE_MAP_LENGTH); |
| 76 |
$map = $isIndefinite ? IndefiniteLengthMapObject::create() : MapObject::create(); |
| 77 |
foreach ($data as $key => $value) { |
| 78 |
$map->add($this->processData($key, $option), $this->processData($value, $option)); |
| 79 |
} |
| 80 |
|
| 81 |
return $map; |
| 82 |
} |
| 83 |
|
| 84 |
private function processFloat(float $data, int $option): SinglePrecisionFloatObject|DoublePrecisionFloatObject |
| 85 |
{ |
| 86 |
$isSinglePrecisionFloat = 0 !== ($option & self::FLOAT_FORMAT_SINGLE_PRECISION); |
| 87 |
|
| 88 |
return match (true) { |
| 89 |
$isSinglePrecisionFloat => SinglePrecisionFloatObject::createFromFloat($data), |
| 90 |
default => DoublePrecisionFloatObject::createFromFloat($data), |
| 91 |
}; |
| 92 |
} |
| 93 |
|
| 94 |
private function processTextString(string $data, int $option): TextStringObject|IndefiniteLengthTextStringObject |
| 95 |
{ |
| 96 |
$isIndefinite = 0 !== ($option & self::INDEFINITE_TEXT_STRING_LENGTH); |
| 97 |
$cbor = TextStringObject::create($data); |
| 98 |
|
| 99 |
if (! $isIndefinite) { |
| 100 |
return $cbor; |
| 101 |
} |
| 102 |
|
| 103 |
return IndefiniteLengthTextStringObject::create()->add($cbor); |
| 104 |
} |
| 105 |
|
| 106 |
private function processByteString(string $data, int $option): ByteStringObject|IndefiniteLengthByteStringObject |
| 107 |
{ |
| 108 |
$isIndefinite = 0 !== ($option & self::INDEFINITE_BYTE_STRING_LENGTH); |
| 109 |
$cbor = ByteStringObject::create($data); |
| 110 |
|
| 111 |
if (! $isIndefinite) { |
| 112 |
return $cbor; |
| 113 |
} |
| 114 |
|
| 115 |
return IndefiniteLengthByteStringObject::create()->add($cbor); |
| 116 |
} |
| 117 |
} |