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
| Name | Type | Description |
|---|---|---|
| $file | string | path to the JSON collection file; its directory is created on first save if missing |
| $entityClass | class-string<T> | the entity class rows are rehydrated into via `fromArray()` |
FileRepository::find()
public function find(string|int $id): ?Milpa\Data\EntityInterfaceThe entity stored under `$id`, or `null` when no entity is stored under it.
Parameters
| Name | Type | Description |
|---|---|---|
| $id | string|int |
FileRepository::save()
public function save(Milpa\Data\EntityInterface $entity): string|intPersists `$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
| Name | Type | Description |
|---|---|---|
| $entity | T |
FileRepository::delete()
public function delete(string|int $id): voidRemoves the entity stored under `$id`. A no-op when no entity is stored under it.
Parameters
| Name | Type | Description |
|---|---|---|
| $id | string|int |
FileRepository::all()
public function all(): arrayEvery stored entity, in file order.
FileRepository::nextId()
public function nextId(): intThe 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): arrayStored entities matching every `$criteria` pair by strict equality.
Parameters
| Name | Type | Description |
|---|---|---|
| $criteria | array<string, mixed> |