CoreFiles by mySites.guru
SimpleObject.php73 lines, 2,393 bytes, from Joomla_6.3.0-alpha1-Alpha-Full_Package.zip
Raw History Compare with 6.2.0-alpha3
md5
8b51d5f4f6b01beb22924c3d2cf8630a
sha1
4104470a906e7fc7d82a9c51ba9456153cf9356c
sha256
d169e9477d24d8442b34f94b27c37a8ecbc8042cc21083d447e357edb5b72564
<?php

declare(strict_types=1);

namespace CBOR\OtherObject;

use CBOR\Normalizable;
use CBOR\OtherObject as Base;
use CBOR\Utils;
use function chr;
use InvalidArgumentException;
use function ord;
use function strlen;

final class SimpleObject extends Base implements Normalizable
{
    public static function supportedAdditionalInformation(): array
    {
        return [...range(0, 19), 24];
    }

    public static function create(int $value): self|FalseObject|TrueObject|NullObject|UndefinedObject
    {
        switch (true) {
            case $value >= 0 && $value <= 19:
                return new self($value, null);
            case $value === 20:
                return FalseObject::create();
            case $value === 21:
                return TrueObject::create();
            case $value === 22:
                return NullObject::create();
            case $value === 23:
                return UndefinedObject::create();
            case $value <= 31:
                throw new InvalidArgumentException('Invalid simple value. Shall be between 32 and 255.');
            case $value <= 255:
                return new self(24, chr($value));
            default:
                throw new InvalidArgumentException('The value is not a valid simple value.');
        }
    }

    public static function createFromLoadedData(int $additionalInformation, ?string $data): Base
    {
        if ($additionalInformation === 24) {
            if ($data === null) {
                throw new InvalidArgumentException('Invalid simple value. Content data is missing.');
            }
            if (strlen($data) !== 1) {
                throw new InvalidArgumentException('Invalid simple value. Content data is too long.');
            }
            if (ord($data) < 32) {
                throw new InvalidArgumentException('Invalid simple value. Content data must be between 32 and 255.');
            }
        } elseif ($additionalInformation < 20) {
            if ($data !== null) {
                throw new InvalidArgumentException('Invalid simple value. Content data should not be present.');
            }
        }

        return new self($additionalInformation, $data);
    }

    public function normalize(): int
    {
        if ($this->data === null) {
            return $this->getAdditionalInformation();
        }

        return Utils::binToInt($this->data);
    }
}

The same content is in 11 releases, Joomla 6.2.0-alpha2 to 6.3.0-alpha1. See them all, or every version of this file.