Skip to content
docsv0.1.0

FileRepository

File-JSON {@see RepositoryInterface}: the whole collection lives in one JSON file, keyed by id, read-modify-write on every mutation — the pattern the `example-agent-ready-blog`'s `JsonPostStorage` proved for a single entity, generalized here to any {@see EntityInterface}. Zero DB, zero dependency: a flat file is the entire durability story. A fresh `FileRepository` pointed at the same path — a different process, a different request — reads back exactly what the last write left, because every read and write goes through the file itself rather than an in-memory cache.

FileRepository::__construct()

public function __construct(string $file, string $entityClass):

Parameters

Parameters of __construct()
NameTypeDescription
$filestringpath to the JSON collection file; its directory is created on first save if missing
$entityClassclass-string<T>the entity class rows are rehydrated into via `fromArray()`

FileRepository::find()

public function find(string|int $id): ?Milpa\Data\EntityInterface

The entity stored under `$id`, or `null` when no entity is stored under it.

Parameters

Parameters of find()
NameTypeDescription
$idstring|int

FileRepository::save()

public function save(Milpa\Data\EntityInterface $entity): string|int

Persists `$entity` to the file. When `$entity->id()` is `null`, a fresh id is assigned via {@see self::nextId()}. The written row always carries the id actually used under the key `'id'`, regardless of what `$entity->toArray()` returned for it.

Parameters

Parameters of save()
NameTypeDescription
$entityT

FileRepository::delete()

public function delete(string|int $id): void

Removes the entity stored under `$id`. A no-op when no entity is stored under it.

Parameters

Parameters of delete()
NameTypeDescription
$idstring|int

FileRepository::all()

public function all(): array

Every stored entity, in file order.

FileRepository::nextId()

public function nextId(): int

The id to assign to the next entity saved without one of its own — one past the highest integer id currently in the file, or `1` when the file holds no integer id.

FileRepository::query()

public function query(array $criteria): array

Stored entities matching every `$criteria` pair by strict equality.

Parameters

Parameters of query()
NameTypeDescription
$criteriaarray<string, mixed>