jaytaph / typearray
类型数组
v0.0.5
2023-06-08 08:20 UTC
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
这是一个围绕属性访问器的简单包装器,用于以类型化的方式获取元素。创建此包的原因是因为我在处理许多未类型化的json数组时遇到了很多问题,而phpstan在这方面也有很大问题。
因此,我们可以使用 TypeArray
代替 mixed[] $myarray
,并使用以下方式获取元素
$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
有关使用的路径语法,请参阅symfony propertyaccess组件。