2025-12-09 22:48:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace FlagPole\Repository;
|
|
|
|
|
|
|
|
|
|
use FlagPole\Flag;
|
2026-02-12 06:54:30 +00:00
|
|
|
use FlagPole\FlagHydrator;
|
|
|
|
|
use FlagPole\Rule;
|
2025-12-09 22:48:07 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Simple in-memory repository. Useful for bootstrapping or tests.
|
|
|
|
|
*/
|
|
|
|
|
final class InMemoryFlagRepository implements FlagRepositoryInterface
|
|
|
|
|
{
|
|
|
|
|
/** @var array<string, Flag> */
|
|
|
|
|
private array $flags = [];
|
|
|
|
|
|
|
|
|
|
/**
|
2026-02-12 06:54:30 +00:00
|
|
|
* @param array<string, array{enabled?:bool|null, rolloutPercentage?:int|null, allowList?:list<string>, rules?:list<array{attribute:string, operator:string, value:mixed}>, targetingKey?:string|null}> $config
|
2025-12-09 22:48:07 +00:00
|
|
|
*/
|
|
|
|
|
public static function fromArray(array $config): self
|
|
|
|
|
{
|
2026-02-12 06:54:30 +00:00
|
|
|
$hydrator = new FlagHydrator();
|
2025-12-09 22:48:07 +00:00
|
|
|
$items = [];
|
|
|
|
|
foreach ($config as $name => $def) {
|
2026-02-12 06:54:30 +00:00
|
|
|
$items[$name] = $hydrator->hydrate((string)$name, $def);
|
2025-12-09 22:48:07 +00:00
|
|
|
}
|
|
|
|
|
return new self($items);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array<string, Flag> $flags
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(array $flags = [])
|
|
|
|
|
{
|
|
|
|
|
$this->flags = $flags;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function get(string $name): ?Flag
|
|
|
|
|
{
|
|
|
|
|
return $this->flags[$name] ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return iterable<string, Flag>
|
|
|
|
|
*/
|
|
|
|
|
public function all(): iterable
|
|
|
|
|
{
|
|
|
|
|
return $this->flags;
|
|
|
|
|
}
|
|
|
|
|
}
|