Files
camelia-controller/tests/Fixture/ArrayGateway.php
T
dgarcia 341d1bdc2c feat: initial commit of camelia/controllers
Framework-agnostic REST controller toolkit in vanilla PHP.

- Router with path parameters, 404 and 405 with an Allow header.
- Immutable Request, JsonRequest and Response; nothing is printed.
- CrudController over a ResourceGateway port the application implements.
- Composable Middleware/Pipeline chain.
- Security middlewares: BearerAuth, RequireRole, RateLimit, Cors.
- Transport middlewares: Logging, ContentNegotiation.
- Nine typed HTTP exceptions with snake_case JSON bodies.
- Declares PHP >=8.3 and no dependency on an ORM: ports are local.
- MIT license.
2026-11-15 10:14:27 -03:00

69 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Camelia\Controllers\Tests\Fixture;
use Camelia\Controllers\ResourceGateway;
/**
* In-memory gateway: the library's contract is testable without a database.
*/
final class ArrayGateway implements ResourceGateway
{
/** @param array<int|string, array<string, mixed>> $records */
public function __construct(
private array $records = [],
) {}
/** @return list<array<string, mixed>> */
public function findAll(array $query = []): array
{
return array_values($this->records);
}
/** @return array<string, mixed>|null */
public function find(int|string $id): ?array
{
return $this->records[$id] ?? null;
}
/** @return array<string, mixed> */
public function create(array $values): array
{
$id = count($this->records) + 1;
$record = ['id' => $id] + $values;
$this->records[$id] = $record;
return $record;
}
/** @return array<string, mixed>|null */
public function update(int|string $id, array $values): ?array
{
if (!isset($this->records[$id])) {
return null;
}
$this->records[$id] = ['id' => $id] + $values;
return $this->records[$id];
}
public function delete(int|string $id): bool
{
if (!isset($this->records[$id])) {
return false;
}
unset($this->records[$id]);
return true;
}
public function count(array $query = []): ?int
{
return count($this->records);
}
}