Skip to content

Core\Arr::shapeAs

Core\Arr::shapeAs<T>(array<mixed> $a): TCore\Arr::shapeAs<T>(
    array<mixed> $a,
): T

Reads an array as the type you write at the call site. Form and query values arrive as text.

The type goes between angle brackets. It can be a shape, such as { name: string, age: int }, or a class with #[Core\Json\Derive]. A class inside a shape or an array needs the attribute too. Each field the type names is converted with as, and other keys are left out. Write array<T> to read a whole list in one call.

A field can also be a time value or a Core\Uuid, read from text. A date or a time is its ISO text, such as 2026-05-14. A Duration is text such as 1h30m, and a Zone is a name such as Europe/Vienna.

Good to know: a missing field, or a value its type cannot take, throws a ParseError. The error lists every field that failed at its own path, so a form can show all its problems at once.

The examples below read a form, order lines, a search query and a date with a time.

Parameters

  • $aarray<mixed>The loose values to read: a form, a query string, a decoded document, or an array a program built. Text from outside the program, such as a form, is tainted. You must check tainted text before you use it. So a text field of T that such text fills must be tainted too.

Return value

Returns T. A new T where every field has its declared type. For an array<T>, one T per entry, in the array's own order.

Errors

  • ParseErrorA field that T requires is missing, or its value cannot be converted to the declared type. Every failed field is one issue on the error, at its own dotted path. A form can then show the whole list of problems.
  • LogicErrorT is a class without #[Json\Derive], so there is no field list to read $a against.

Changelog

VersionChange
0.0.6A Core\Time\Date, TimeOfDay, DateTime, Duration, Zone or Core\Uuid is now a JSON string, as Instant already was. A time zone is written as its name, such as "Europe/Vienna". A text that is not valid throws a ParseError that names its key.
0.0.6Some decoders take a type, such as Core\Json::decodeAs and Core\Request::jsonAs. They check every class inside a shape or an array of that type, at any depth. Core\Request::jsonAs, queryParametersAs and formFieldsAs give the error E0810 when such a class has a plain string property.

Examples

A form read into the shape you want

<?nvs
// Shows how to read a posted form into the shape the rest of the program
// wants. Every value in a form is text, and each field the shape names is
// converted to the type it declares.
type Signup = { name: string, age: int };
// The form as it arrived. The shape does not name `csrf`, so that key is left
// out of the result.
array<string> $form = ['name' => 'Ada', 'age' => '36', 'csrf' => 't0ken'];
Signup $signup = Core\Arr::shapeAs<Signup>($form);
// `age` is a whole number now, so you can count with it. This prints
// `Ada is 36 and will be 37 next year`.
echo $signup->name, ' is ', $signup->age, ' and will be ', $signup->age + 1, " next year\n";
Output
Ada is 36 and will be 37 next year

A whole list in one call

<?nvs
// Shows how to read a whole list in one call. `array<T>` reads every entry of
// the array as the type `T`, and the result keeps the order of the input.
type Line = { sku: string, qty: int };
array<Line> $lines = Core\Arr::shapeAs<array<Line>>([
['sku' => 'A-1', 'qty' => '2'],
['sku' => 'B-7', 'qty' => '5'],
]);
// Each `qty` is a whole number, so this loop can add them up. It prints one
// line per entry, and then `total 7`.
int $total = 0;
foreach ($lines as (Line) $line) {
echo $line->sku, ': ', $line->qty, "\n";
$total = $total + $line->qty;
}
echo 'total ', $total, "\n";
Output
A-1: 2
B-7: 5
total 7

A search page reads its query

<?nvs
// Shows how a search page reads its query parameters. The type here is a
// class, so the rest of the program works with an object instead of loose
// text. A class needs `#[Core\Json\Derive]` to be read this way.
#[Core\Json\Derive]
class Search
{
public string $term;
public int $page;
public ?int $maxPrice;
public function constructor(string $term, int $page, ?int $maxPrice)
{
$this->term = $term;
$this->page = $page;
$this->maxPrice = $maxPrice;
}
}
// The query string of `/shoes?term=boots&page=2&maxPrice=80`, split into an
// array.
array<string> $query = ['term' => 'boots', 'page' => '2', 'maxPrice' => '80'];
try {
Search $search = Core\Arr::shapeAs<Search>($query);
// This prints `boots, page 2, under 80`.
echo $search->term, ', page ', $search->page, ', under ', $search->maxPrice ?? 0, "\n";
} catch (ParseError $bad) {
// A visitor can type anything into a query string. Every field that did
// not fit is one entry here, at its own path.
foreach ($bad->issues as ({ path: string, message: string }) $issue) {
echo $issue->path, ': ', $issue->message, "\n";
}
}
Output
boots, page 2, under 80

A form with a date and a time

<?nvs
// Shows how a posted form with a date and a time is read into `Core` time classes.
// A booking form sends every field as a string.
array<string> $form = ['day' => '2026-05-14', 'opens' => '08:30', 'zone' => 'Europe/Vienna'];
try {
var $slot = Core\Arr::shapeAs<{ day: Core\Time\Date, opens: Core\Time\TimeOfDay, zone: Core\Time\Zone }>($form);
// This prints `2026-05-14 at 08:30:00`.
echo $slot->day->toIso(), ' at ', $slot->opens->toIso(), "\n";
} catch (ParseError $bad) {
foreach ($bad->issues as ({ path: string, message: string }) $issue) {
echo $issue->path, ': ', $issue->message, "\n";
}
}
// A visitor types the date in another format. The field is listed with its problem.
try {
Core\Arr::shapeAs<{ day: Core\Time\Date }>(['day' => '14.05.2026']);
} catch (ParseError $bad) {
echo $bad->issues['0']->path, ': ', $bad->issues['0']->message, "\n";
}
Output
2026-05-14 at 08:30:00
day: expected a date as a string such as 2026-01-05, found a string