:`, type `definition`, decision 047). * * @since 0.1.0 */ class DefinitionStore { public const MAX = 200; private const TYPE = 'definition'; public function __construct(protected OptionRows $rows) { } /** * Every stored definition, each with its `kind`. * * @since 0.1.0 * @return list> */ public function all(): array { $out = []; foreach ($this->rows->ofType(self::TYPE) as $row) { if (is_array($row['value']) && is_string($row['value']['kind'] ?? null)) { $out[] = $row['value']; } } return $out; } /** * @since 0.1.0 * @return array|null */ public function find(string $kind, string $id): ?array { $value = $this->rows->get(self::key($kind, $id)); return is_array($value) ? $value : null; } /** * Store `$definition` (with its `kind` and `id`), replacing one with the same kind and id. * * @since 0.1.0 * @param array $definition */ public function save(array $definition): bool { $kind = (string) ($definition['kind'] ?? ''); $id = (string) ($definition['id'] ?? ''); if ($this->find($kind, $id) === null && count($this->rows->ofType(self::TYPE)) >= self::MAX) { return false; } return $this->rows->put(self::key($kind, $id), $definition, self::TYPE); } /** * @since 0.1.0 */ public function delete(string $kind, string $id): bool { return $this->find($kind, $id) !== null && $this->rows->delete(self::key($kind, $id)); } private static function key(string $kind, string $id): string { return 'def:' . $kind . ':' . $id; } }