CoreFiles by mySites.guru
OtherObjectManager.php47 lines, 1,123 bytes, from Joomla_5.4.11-Stable-Full_Package.zip
Raw History Compare with 5.4.10
md5
a551306e2eded8971e8ddec070c83725
sha1
3bcc77b4d877d58e3bf74cb2c415f6b1805d6542
sha256
88d9ce3bd8fbec52c4e53fc65c7c19b215d040b3e903404d5cb6ae93d978a358
<?php

declare(strict_types=1);

namespace CBOR\OtherObject;

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

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 104 releases, Joomla 5.1.0-beta1 to 6.1.0-alpha1. See them all, or every version of this file.