FlagPole/src/Repository/InMemoryFlagRepository.php

53 lines
1.2 KiB
PHP
Raw Normal View History

2025-12-09 22:48:07 +00:00
<?php
declare(strict_types=1);
namespace FlagPole\Repository;
use FlagPole\Flag;
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 = [];
/**
* @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
{
$hydrator = new FlagHydrator();
2025-12-09 22:48:07 +00:00
$items = [];
foreach ($config as $name => $def) {
$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;
}
}