CoreFiles by mySites.guru
AlgorithmManager.php77 lines, 1,653 bytes, from Joomla_5.2.0-alpha1-Alpha-Full_Package.zip
Raw History Compare with 5.1.0-alpha4
md5
b29f934576b9a393b55f7688c347b416
sha1
3967604f8ecd34c55e78fa41012c3c7dd42b676e
sha256
015e7d047596683866e81bddc6d9589d97341ba4f2dbe7ff1b2549c8f5d07a6c
<?php

declare(strict_types=1);

namespace Jose\Component\Core;

use InvalidArgumentException;
use function array_key_exists;

class AlgorithmManager
{
    /**
     * @var array<string, Algorithm>
     */
    private array $algorithms = [];

    /**
     * @param Algorithm[] $algorithms
     */
    public function __construct(array $algorithms)
    {
        foreach ($algorithms as $algorithm) {
            $this->add($algorithm);
        }
    }

    /**
     * Returns true if the algorithm is supported.
     *
     * @param string $algorithm The algorithm
     */
    public function has(string $algorithm): bool
    {
        return array_key_exists($algorithm, $this->algorithms);
    }

    /**
     * @return array<string, Algorithm>
     */
    public function all(): array
    {
        return $this->algorithms;
    }

    /**
     * Returns the list of names of supported algorithms.
     *
     * @return string[]
     */
    public function list(): array
    {
        return array_keys($this->algorithms);
    }

    /**
     * Returns the algorithm if supported, otherwise throw an exception.
     *
     * @param string $algorithm The algorithm
     */
    public function get(string $algorithm): Algorithm
    {
        if (! $this->has($algorithm)) {
            throw new InvalidArgumentException(sprintf('The algorithm "%s" is not supported.', $algorithm));
        }

        return $this->algorithms[$algorithm];
    }

    /**
     * Adds an algorithm to the manager.
     */
    public function add(Algorithm $algorithm): void
    {
        $name = $algorithm->name();
        $this->algorithms[$name] = $algorithm;
    }
}

The same content is in 17 releases, Joomla 5.1.0-beta1 to 5.2.0-alpha1. See them all, or every version of this file.