CoreFiles by mySites.guru
OtherObjectManager.php47 lines, 1,123 bytes, from Joomla_5.0.0-alpha1-Alpha-Full_Package.zip
Raw History Compare with 4.4.0-alpha4
md5
69e5ed25ae1d0994094131a16aef4ed7
sha1
ad743e8d14762dcafa468c8989e7f9596bffe94d
sha256
18e5c8ec717d8af3d96fbe4b4f42b969282ca4e9cde711a4af3a3d4a44ebcaea
<?php

declare(strict_types=1);

namespace CBOR\OtherObject;

use function array_key_exists;
use CBOR\OtherObject;
use InvalidArgumentException;

class OtherObjectManager implements OtherObjectManagerInterface
{
    /**
     * @var string[]
     */
    private array $classes = [];

    public static function create(): self
    {
        return new self();
    }

    public function add(string $class): self
    {
        foreach ($class::supportedAdditionalInformation() as $ai) {
            if ($ai < 0) {
                throw new InvalidArgumentException('Invalid additional information.');
            }
            $this->classes[$ai] = $class;
        }

        return $this;
    }

    public function getClassForValue(int $value): string
    {
        return array_key_exists($value, $this->classes) ? $this->classes[$value] : GenericObject::class;
    }

    public function createObjectForValue(int $value, ?string $data): OtherObjectInterface
    {
        /** @var OtherObject $class */
        $class = $this->getClassForValue($value);

        return $class::createFromLoadedData($value, $data);
    }
}

The same content is in 22 releases, Joomla 5.0.0-alpha1 to 5.1.0-alpha4. See them all, or every version of this file.