jaytaph/typearray

Typed Array

Maintainers

Details

github.com/jaytaph/typearray

Installs: 10 812

Dependents: 0

Suggesters: 0

Security: 0

Stars: 3

Watchers: 3

Forks: 0

v0.0.5 2023-06-08 08:20 UTC

This package is auto-updated.

Last update: 2025-07-08 13:04:33 UTC


README

Simple wrapper around property-accessor to fetch elements in a typed way. This package is created because I was fiddling around with a lot of json-arrays that were not typed and phpstan had a big issue with that.

So, instead of having mixed[] $myarray, we can use TypeArray, and fetch elements with:

$arr = new TypeArray(['foo' => 'string', 'bar' => 4 ]);

$arr->getString('[foo]');  // always returns a string
$arr->getString('[notexists]', 'defaultval');   // Returns default val when not found

$arr->getInt('[bar]');  // returns int(4)
$arr->getInt('[foo]');  // Returns InvalidTypeException as this element is a string, not an int

See symfony propertyaccess component for the path syntax used.