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.
47 lines
1.2 KiB
PHP
47 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Camelia\Controllers;
|
|
|
|
/**
|
|
* Data access port for a REST resource.
|
|
*
|
|
* The library never talks to a database. An application implements this interface, typically by
|
|
* adapting `Camelia\Orm\Repository`. Records are associative arrays; serializing them is the
|
|
* controller's job, so the gateway stays free of presentation concerns.
|
|
*/
|
|
interface ResourceGateway
|
|
{
|
|
/**
|
|
* @param array<string, mixed> $query
|
|
* @return list<array<string, mixed>>
|
|
*/
|
|
public function findAll(array $query = []): array;
|
|
|
|
/** @return array<string, mixed>|null */
|
|
public function find(int|string $id): ?array;
|
|
|
|
/**
|
|
* @param array<string, mixed> $values
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function create(array $values): array;
|
|
|
|
/**
|
|
* @param array<string, mixed> $values
|
|
* @return array<string, mixed>|null
|
|
*/
|
|
public function update(int|string $id, array $values): ?array;
|
|
|
|
/** Returns true when a record was removed. */
|
|
public function delete(int|string $id): bool;
|
|
|
|
/**
|
|
* Total matching records, or `null` when the gateway cannot count them.
|
|
*
|
|
* @param array<string, mixed> $query
|
|
*/
|
|
public function count(array $query = []): ?int;
|
|
}
|