jaytaph / typearray
Typed Array
Installs: 5 692
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: >=8.0.2
- symfony/property-access: ^6.0
Requires (Dev)
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.6
Suggests
- doctrine/dbal: For using type_array directly with Doctrine
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.