Built-in Casters
The following built-in explicit casters are available:
Bag Collections
\Bag\Casts\CollectionOf casts an array to a Collection of Bag objects.
use Bag\Bag;
use Bag\Attributes\Cast;
use Bag\Casts\CollectionOf;
use Bag\Collection;
class MyValue extends Bag {
public function __construct(
#[Cast(CollectionOf::class, MyOtherValue::class)]
public Collection $values,
) {
}
}
$value = MyValue::from([
'values' => [
['name' => 'Davey Shafik', 'age' => 40],
…
],
]);
dump($value->values); // Collection<MyOtherValue>TIP
The CollectionOf caster will use the appropriate Collection class based on the Bag class provided.
Dates & Time
\Bag\Casts\DateTime casts a date/time string to an object that implements the \DateTimeInterface interface.
use Bag\Bag;
use Bag\Attributes\Cast;
use Bag\Casts\DateTime;
use Carbon\CarbonImmutable;
class MyValue extends Bag {
public function __construct(
#[Cast(
DateTime::class,
format: 'u',
outputFormat:
'Y-m-d', strictMode: true
)]
public CarbonImmutable $dateOfBirth,
) {
}
}
$value = MyValue::from([
// Requires a UNIX timestamp input due to strictMode: true
'dateOfBirth' => '454809600',
]);
$value->dateOfBirth; // CarbonImmutable{date: 1984-05-31 00:00:00 UTC}
$value->toArray(); // ['dateOfBirth' => '1984-05-31'] due to outputFormatThe DateTime cast accepts the following parameters:
format- The format of the input and output date/time string (see PHP date/time format)outputFormat- The format of the output date/time string, this will override theformatparameter for output onlystrictMode- Iftrue, the input date/time string must match theformatparameter exactly, otherwise it is parsed as best as possibledateTimeClass- The class to use for the date/time object, must implement\DateTimeInterface, this must be compatible with the property type hint, and defaults to the type hint value
TIP
We recommend using CarbonImmutable as type for all date/time casting.
Money
\Bag\Casts\MoneyFromMinor Casts a number to a \Brick\Money\Money object assuming minor units (e.g. Cents)
use Bag\Bag;
use Bag\Attributes\Cast;
use Bag\Casts\MoneyFromMinor;
use Brick\Money\Money;
class MyValue extends Bag {
public function __construct(
#[Cast(MoneyFromMinor::class, currency: 'USD')]
public Money $amount,
) {
}
}
$value = MyValue::from([
'amount' => 1000,
]);
dump($value->amount); // Money object with a value of 10.00 USD\Bag\Casts\MoneyFromMajor Casts a number to a \Brick\Money\Money object assuming major units (e.g. Dollars)
use Bag\Bag;
use Bag\Attributes\Cast;
use Bag\Casts\MoneyFromMajor;
use Brick\Money\Money;
class MyValue extends Bag {
public function __construct(
#[Cast(MoneyFromMajor::class, currency: 'USD')]
public Money $amount,
) {
}
}
$value = MyValue::from([
'amount' => 1000,
]);
dump($value->amount); // Money object with a value of 1,000.00 USDBoth MoneyFromMajor and MoneyFromMinor accept the following parameters:
currency- The currency code to use for the\Brick\Money\Moneyobject, either a 3-letter ISO 4217 code or a\PrinsFrank\Standards\Currency\CurrencyAlpha3enum case.currencyProperty- The input parameter to use for the currency codelocale- The locale to use for formatting the money object, defaults toen_US
TIP
Best practices recommend that you always handle money as strings of minor units, however if you are working with user input it's most likely in major units. We recommend using MoneyFromMajor as the input caster and MoneyFromMinor as the output caster for handling user input.