aveiv/mixed-value

MixedValue provides easy array access and the ability to convert/cast values

v0.3.2 2021-12-04 06:16 UTC

This package is auto-updated.

Last update: 2024-04-28 20:47:53 UTC


README

Build Status Latest Stable Version Total Downloads License

MixedValue provides easy array access and the ability to convert/cast values.

Installation

composer require aveiv/mixed-value

Access to array

$mixed = new MixedValue([
    'good' => [
        'path' => 'value',
    ]
]);

$mixed['good']['path']->getValue(); // returns "value"
$mixed['invalid']['path']->getValue(); // throws MissingValueException

$mixed['invalid']['path']->findValue(); // returns null
$mixed['invalid']['path']->findValue() ?? 'default'; // returns "default"

Checking value types

$mixed = new MixedValue([
    'array_val' => [],
    'bool_val' => true,
    'float_val' => 1.0,
    'int_val' => 1,
    'numeric_val' => '99.99',
    'str_val' => 'string',
]);

$mixed['array_val']->isArray()->getValue(); // returns []
$mixed['bool_val']->isBool()->getValue(); // returns true
$mixed['float_val']->isFloat()->getValue(); // returns 1.0
$mixed['int_val']->isInt()->getValue(); // returns 1
$mixed['numeric_val']->isNumeric()->getValue(); // returns '99.99'
$mixed['str_val']->isString()->getValue(); // returns "string"

$mixed['str_val']->isInt()->getValue(); // throws UnexpectedValueException

Converting/casting values

Default converters use PHP casting rules. UnexpectedValueException is thrown if a value cannot be converted.

$mixed = new MixedValue([
    'id' => '99',
    'name' => 'Mary',
    'birthdate' => '1990-01-01',
    'balance' => '999.99',
    'isActive' => 1,
    
    'array_data' => [],
]);

$mixed['id']->toInt()->getValue(); // returns 99
$mixed['name']->toString()->getValue(); // returns "Mary"
$mixed['birthdate']->toDateTime()->getValue(); // returns DateTime("1990-01-01")
$mixed['balance']->toFloat()->getValue(); // returns 999.99
$mixed['isActive']->toBool()->getValue(); // returns true

$mixed['array_data']->toString()->getValue(); // throws UnexpectedValueException

Processing array elements

$mixed = new MixedValue([
    'mixed_arr' => [1, 2, '3', 4, 5],
]);

$mixed['mixed_arr']
    ->map(fn(MixedValue $el) => $el->toInt()->getValue())
    ->getValue(); // returns [1, 2, 3, 4, 5]

Use custom value processors

class StripSpacesProcessor implements ValueProcessorInterface
{
    public function __invoke($value)
    {
        if (!is_string($value)) {
            throw new UnexpectedValueException('Value must be a string');
        }
        return str_replace(' ', '', $value);
    }
}

$mixed = new MixedValue([
    'bad_float' => '9 999.99',
]);
$mixed->registerValueProcessor('stripSpaces', new StripSpacesProcessor());

$mixed['bad_float']->stripSpaces()->toFloat()->getValue(); // return 9999.99