Download the PHP package nitier/caster without Composer
On this page you can find all versions of the php package nitier/caster. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nitier/caster
More information about nitier/caster
Files in nitier/caster
Package caster
Short Description Strict, convenient casting helpers for PHP (mixed -> typed).
License MIT
Informations about the package caster
Caster
Strict and expressive casting helpers for PHP. The goal is to replace chains of isset, is_*
and ad-hoc checks with a single call that either returns a typed value or throws a descriptive
exception.
Features
- Scalar casters (
int,float,bool,string,nonEmptyString, trimmed variants, etc.). - Date/time helpers for
DateTimeImmutable. - Enum support for backed enums (
enum,nullableEnum,tryEnum). - Array utilities (
listOf,arrayOf, nullable and try-* variants). - JSON decoding with validation.
- Payload facades (
fromArray,fromArrayNullable) to extract and cast fields in one step. - “Soft”
try*methods that returnnullon failure instead of throwing. - Convenience guards (
required,requiredNonEmptyString,requiredNonEmptyList, …).
Installation
Basic Usage
If casting fails, a CastException is thrown with field context (when provided). Nullable variants
(nullableInt, nullableFloat, …) treat null and whitespace-only strings as “empty”.
Strings and Booleans
Date & Time
Enums
Soft try* Helpers
When you prefer a nullable result over exceptions:
Each try* helper wraps the strict version and returns null on failure.
Payload Helpers
Extract values from an associative array and cast them on the spot:
fromArraythrowsMissingValueExceptionwhen the key is missing.fromArrayNullablereturnsnullwhen the key is missing or empty.requiredNonEmpty*helpers ensure presence and non-empty content for strings/lists/maps.
Collections
listOf and arrayOf accept a caster callback applied to every item:
Use requiredNonEmptyList/requiredNonEmptyArray when the collection must contain at least one item.
JSON
You can pass $assoc, $depth and $flags just like json_decode.
Required Guards
Caster::required($value, 'field') ensures a value is not null. For common cases you can use:
Testing
The project includes an extensive PHPUnit suite. Run it with:
Static analysis:
Code style:
License
MIT ©2025 Nitier