Loheide-eu-mitarbeiterplatt.../vendor/spomky-labs/cbor-php/src/Encoder.php
Friederich Loheide 9c0f8de791
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m14s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m12s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m7s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m19s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 1m32s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 1m22s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 1m32s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 1m35s
CI / Build-Artefakte aktuell (pull_request) Failing after 1m7s
Phase 0: Fundament für Inhaus
- Laravel 13 mit Filament 5 (Admin), Inertia 3 + Vue 3 + Vuetify 4 (Material 3)
- Anmeldung über Fortify (Login, Passwort vergessen/ändern), keine Selbstregistrierung
- Profil mit Profilbild (WebP in festen Größen), Personenseiten, Header mit Avatar-Menü
- Material-3-Farbschema aus der Primärfarbe, hell/dunkel/System pro Person
- Basis-Branding (Portalname, Logo, Primärfarbe) im Admin unter „Erscheinungsbild“
- Benutzerverwaltung im Admin, Zugang vorläufig über is_admin
- vendor/ (ohne Dev-Pakete) und Frontend-Build mitversioniert, Dev-Pakete in vendor-dev/
- PHPUnit-Tests gegen MySQL/MariaDB, Forgejo-CI-Matrix, Docker-Entwicklungsumgebung
- VERSION, CHANGELOG.md und CONTRIBUTING.md mit Release-Ablauf

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-25 20:35:30 +00:00

118 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
namespace CBOR;
use function array_is_list;
use CBOR\OtherObject\DoublePrecisionFloatObject;
use CBOR\OtherObject\FalseObject;
use CBOR\OtherObject\NullObject;
use CBOR\OtherObject\SinglePrecisionFloatObject;
use CBOR\OtherObject\TrueObject;
use InvalidArgumentException;
use function is_array;
use function is_float;
use function is_int;
use function is_string;
final class Encoder implements EncoderInterface
{
public const FLOAT_FORMAT_SINGLE_PRECISION = 0b0000001;
public const INDEFINITE_BYTE_STRING_LENGTH = 0b00000010;
public const INDEFINITE_TEXT_STRING_LENGTH = 0b00000100;
public const INDEFINITE_LIST_LENGTH = 0b00001000;
public const INDEFINITE_MAP_LENGTH = 0b00010000;
public function encode(mixed $data, int $options = 0): string
{
return (string) $this->processData($data, $options);
}
private function processData(mixed $data, int $option): CBORObject
{
return match (true) {
$data instanceof CBORObject => $data,
is_string($data) => preg_match('//u', $data) === 1 ? $this->processTextString(
$data,
$option
) : $this->processByteString($data, $option),
is_array($data) => array_is_list($data) ? $this->processList($data, $option) : $this->processMap(
$data,
$option
),
is_int($data) => $data < 0 ? NegativeIntegerObject::create($data) : UnsignedIntegerObject::create($data),
is_float($data) => $this->processFloat($data, $option),
$data === null => NullObject::create(),
$data === false => FalseObject::create(),
$data === true => TrueObject::create(),
default => throw new InvalidArgumentException('Unsupported data type'),
};
}
/**
* @param array<array-key, mixed> $data
*/
private function processList(array $data, int $option): ListObject|IndefiniteLengthListObject
{
$isIndefinite = 0 !== ($option & self::INDEFINITE_LIST_LENGTH);
$list = $isIndefinite ? IndefiniteLengthListObject::create() : ListObject::create();
foreach ($data as $item) {
$list->add($this->processData($item, $option));
}
return $list;
}
/**
* @param array<array-key, mixed> $data
*/
private function processMap(array $data, int $option): MapObject|IndefiniteLengthMapObject
{
$isIndefinite = 0 !== ($option & self::INDEFINITE_MAP_LENGTH);
$map = $isIndefinite ? IndefiniteLengthMapObject::create() : MapObject::create();
foreach ($data as $key => $value) {
$map->add($this->processData($key, $option), $this->processData($value, $option));
}
return $map;
}
private function processFloat(float $data, int $option): SinglePrecisionFloatObject|DoublePrecisionFloatObject
{
$isSinglePrecisionFloat = 0 !== ($option & self::FLOAT_FORMAT_SINGLE_PRECISION);
return match (true) {
$isSinglePrecisionFloat => SinglePrecisionFloatObject::createFromFloat($data),
default => DoublePrecisionFloatObject::createFromFloat($data),
};
}
private function processTextString(string $data, int $option): TextStringObject|IndefiniteLengthTextStringObject
{
$isIndefinite = 0 !== ($option & self::INDEFINITE_TEXT_STRING_LENGTH);
$cbor = TextStringObject::create($data);
if (! $isIndefinite) {
return $cbor;
}
return IndefiniteLengthTextStringObject::create()->add($cbor);
}
private function processByteString(string $data, int $option): ByteStringObject|IndefiniteLengthByteStringObject
{
$isIndefinite = 0 !== ($option & self::INDEFINITE_BYTE_STRING_LENGTH);
$cbor = ByteStringObject::create($data);
if (! $isIndefinite) {
return $cbor;
}
return IndefiniteLengthByteStringObject::create()->add($cbor);
}
}