Download the PHP package minvws/icore-php-typearray without Composer
On this page you can find all versions of the php package minvws/icore-php-typearray. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package icore-php-typearray
Typed Array
Typed Array provides runtime-checked access to values in PHP arrays. It uses Symfony PropertyAccess paths to read nested data and returns values with explicit PHP types.
This is useful when working with decoded JSON or other arrays containing
mixed values. The package validates values when they are read and does not
cast them to the requested type.
Dependencies
- PHP
^8.2 - Symfony PropertyAccess
^6.0,^7.0, or^8.0
Installation
Install the package with Composer:
Quick start
The values assigned above have the types string, int, bool, and string
respectively.
The examples below use the Composer autoloader and TypeArray import from this
quick start.
Usage
Reading typed values
TypeArray provides getters for the most common PHP value types:
| Method | Return type |
|---|---|
getString() |
string |
getInt() |
int |
getFloat() |
float |
getBool() |
bool |
getTypeArray() |
TypeArray |
The requested value must have the exact PHP type. For example, getInt() does
not convert a numeric string such as "42" to an integer.
Paths
Paths use the Symfony PropertyAccess syntax. Wrap each array key in square brackets and append brackets to read nested values:
See the Symfony PropertyAccess documentation for the complete path syntax and escaping rules.
Default and nullable values
getString(), getInt(), getFloat(), and getTypeArray() accept an optional
default value. The default is returned when the path does not exist or contains
null:
The nullable getters return null when a path does not exist:
getBool() returns false by default for a missing or null value. Pass a
different default as its second argument when needed.
An existing null value passed to getTypeArrayOrNull() produces an empty
TypeArray. A missing path returns null.
Nested arrays and iteration
Use getTypeArray() to keep reading a nested array with typed getters:
When getIterable() encounters a nested array, it yields that value as a
TypeArray instance. Other values are yielded unchanged.
JSON and array conversion
Create a TypeArray directly from JSON:
Use toArray() to retrieve the wrapped array. TypeArray also implements
JsonSerializable, so it can be passed directly to json_encode():
Invalid JSON passed to fromJson() throws JsonException.
Inspecting values
Exceptions
A missing path passed to getString(), getInt(), getFloat(), or
getTypeArray() without a default throws InvalidIndexException. A value with
the wrong PHP type throws IncorrectDataTypeException:
Doctrine DBAL
The package includes the optional type_array type for Doctrine DBAL. Install
DBAL separately when using this integration:
Register the type during application startup:
The type stores a TypeArray as JSON and converts database values back to a TypeArray. See the Doctrine DBAL custom mapping type documentation for integration details.
Development
Install the development dependencies:
Testing
Run the coding-style checks, static analysis, and unit tests with:
License
This package is available under the BSD 3-Clause License.
Part of iCore
This package is part of the iCore project.