CoreFiles by mySites.guru
OtherObjectManager.php57 lines, 1,401 bytes, from Joomla_6.1.0-alpha2-Alpha-Full_Package.zip
Raw History Compare with 6.1.0-alpha1
md5
9414ef02acc8cca39a9cc229fcc61576
sha1
f308d96b89bc7c9804bc7accf59f36f838c7234a
sha256
c2c366cceb076702c39b2fca7b792329d5f223de2e0fcd75af2c6b6889d25b13
<?php

declare(strict_types=1);

namespace CBOR\OtherObject;

use InvalidArgumentException;
use function array_key_exists;

final class OtherObjectManager implements OtherObjectManagerInterface
{
    /**
     * @param  class-string<OtherObjectInterface>[] $classes
     */
    public function __construct(
        private array $classes = [],
    ) {
    }

    /**
     * @param  class-string<OtherObjectInterface>[] $classes
     */
    public static function create(array $classes = []): self
    {
        return new self($classes);
    }

    /**
     * @param class-string<OtherObjectInterface> $class
     */
    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;
    }

    /**
     * @return class-string<OtherObjectInterface>
     */
    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
    {
        $class = $this->getClassForValue($value);

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

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