Attributes & values
Each value helper returns a descriptor of ark4ne/laravel-api-revisions that renders the
value with its type. Its first argument is the key read on the model (the member name by default) or a closure
receiving the model:
public function toAttributes(Request $request): iterable
{
return [
'name' => $this->string(), // `name`
'mail' => $this->string('email'), // `email`
'hash64' => $this->string(
fn(User $user) => base64_encode("{$user->id}-{$user->email}"),
),
];
}
Types
| Helper | Renders |
|---|---|
bool() | a boolean |
integer() | an integer |
float() | a float; ->precision(2) rounds it, ->truncate() truncates |
string() | a string; ->format('email') gives its JSON Schema format |
date() | a date without time, Y-m-d; ->format('d/m/Y') |
datetime() | a date and time, revisions.date_time_format (ATOM) unless ->format() |
time() | a time of day, H:i:s |
decimal() | an exact decimal as a string ("12.50"), ->scale(2) |
uuid(), ulid() | a string of that format |
json() | a JSON string, rendered decoded |
array() | a list of values of any type |
arrayOf($descriptor) | a list of values of one type |
enum(State::class) | the value of a backed enum |
struct([...]) | an object of described members |
mixed() | the value as it is |
Every helper is listed with its modifiers in the value helpers reference.
Null and invalid values
A value is nullable unless ->nullable(false) (or describer.nullable set to false). A value its type refuses (a
null that is not nullable, a string where an integer is declared, an enum value out of its cases) is a value
error: it throws in debug, and is reported and cast otherwise (revisions.strict_values in config/revisions.php).
Enums
enum() renders the value of a backed enum and names its cases in the definition:
enum State: int
{
case ACTIVE = 1;
case INACTIVE = 0;
}
// UserResource
'state' => $this->enum(State::class), // 1
A unit enum has no value: render its name with a string, $this->string(fn(User $user) => $user->role->name).
Lists
arrayOf() casts every item to one type; array() leaves them as they are.
return [
// ["php", "laravel", "123"]
'tags' => $this->arrayOf($this->string()),
// read from `results`
'scores' => $this->arrayOf($this->integer(), 'results'),
// a list of lists
'matrix' => $this->arrayOf($this->arrayOf($this->integer())),
'addresses' => $this->arrayOf($this->struct([
'city' => $this->string(),
'zip' => $this->string(),
])),
'user_ids' => $this->arrayOf(
$this->integer(),
fn(Team $team) => $team->users->pluck('id')->all(),
),
];
A condition on the type of the items is not read for each item: filter the list in its source.
Objects
struct() renders an object of named members, read on the model itself, or on the value of its source:
'address' => $this->struct([
'city' => $this->string(), // the `city` of the model
'zip' => $this->string('postal_code'),
]),
'position' => $this->struct([
'lat' => $this->float(),
'lng' => $this->float(),
], 'coordinates'),
fields[users]=address.city keeps one member of the object, fields[users]=tags.name one member of each object of a
list; the members left out are never read.
Value types of your application
A type of your own is a Descriptor of ark4ne/laravel-api-revisions: it normalises the raw value and declares its
type, read by the tools that document the resources.
use Ark4ne\ApiRevisions\Definition\Kind;
use Ark4ne\ApiRevisions\Definition\TypeDefinition;
use Ark4ne\ApiRevisions\Schema\Descriptor;
use Ark4ne\ApiRevisions\Schema\InvalidValue;
/**
* @extends Descriptor<string>
*/
final class MoneySchema extends Descriptor
{
public function normalize(mixed $value): mixed
{
return is_numeric($value)
? number_format((int) $value / 100, 2, '.', '')
: new InvalidValue($value, Kind::Custom, '0.00');
}
protected function type(): TypeDefinition
{
return TypeDefinition::custom('money', [
'type' => 'string',
'format' => 'money',
]);
}
}
Register it under a name in a service provider:
use Ark4ne\ApiRevisions\Schema\TypeRegistry;
TypeRegistry::register('money', MoneySchema::class);
It is then written $this->money('price') in a resource (declare @method MoneySchema money(...) on your resource for
the IDE and PHPStan), and as the items of a list: $this->arrayOf($this->money(), 'prices').
Documenting a value
->describe('…'), ->example(…) and ->deprecated() document a field. They do not change the document; tools such as
the OpenAPI generator read them.