mschindler83 / array-access
轻松访问数组
v1.5.1
2021-07-28 10:02 UTC
Requires
- php: >=7.4
- ext-json: *
- opis/json-schema: ^1.0
Requires (Dev)
- phpunit/phpunit: ^8.5
README
简化数组访问处理的库。需要 PHP >= 7.4
安装
composer require mschindler83/array-access
特性
- 安全地从给定数组中访问类型值
- 可选的 JSON 模式验证
- 支持日期时间解析
- 在获取值时定义自己的验证回调函数
- 以“点注释”的形式创建新的数组
- 轻松将值写入数组的特定路径
使用示例
创建访问对象
从数组创建访问对象并访问值
$array = [
'key1' => [
'key2' => [
'key3' => 'the-value'
],
],
];
$access = ArrayAccess::create($array);
try {
// Get the string value at the given path
$value = $access->string('key1', 'key2', 'key3');
// This will fail with an exception because we try to get an integer at the given path
$invalidValue = $access->int('key1', 'key2', 'key3');
} catch (ArrayAccessFailed $e) {
// handle errors
echo $e->getMessage();
}
从“点注释”创建数组
$access = ArrayAccess::newFromDotAnnotation(
SimpleDotAnnotation::create('key1.key2.2.key3', 'the-value-1'),
SimpleDotAnnotation::create('key1.key2.2.key4', 'the-value-2')
);
$plainArray = $access->data();
普通数组将包含
Array
(
[key1] => Array
(
[key2] => Array
(
[2] => Array
(
[key3] => the-value-1
[key4] => the-value-2
)
)
)
)
具有 JSON 模式验证的数组访问
$data = [
'key1' => 'value1',
'key2' => true,
];
$access = ArrayAccess::createWithJsonSchemaValidation($data, \file_get_contents('json-schema.json'));
JSON 模式:<json-schema.json>
{
"$schema": "https://json-schema.fullstack.org.cn/draft-07/schema#",
"type": "object",
"properties": {
"key1": {
"type": "string",
"minLength": 3,
"maxLength": 64,
"pattern": "^[a-zA-Z0-9\\-]+(\\s[a-zA-Z0-9\\-]+)*$"
},
"key2": {
"type": "boolean"
}
},
"required": ["key1", "key2"],
"additionalProperties": false
}
如果验证失败,将抛出 ArrayAccessValidationFailed
异常。您可以通过在异常上调用 errorMapping()
方法来获取验证错误的 ArrayAccess
对象。
访问值
$array = [
'root' => [
'string-value' => 'the-value',
'int-value' => 10,
'float-value' => 9.99,
'bool-value' => true,
'array-value' => [1, 2, 3],
'datetime-value' => '2020-01-01 12:00:00',
'object-value' => new \stdClass(),
'custom' => 'Choice 1',
],
];
// Create the access object
$access = ArrayAccess::create($array);
// This will return the string "the-value"
$access->string('root', 'string-value');
// This will return the integer "10"
$access->int('root', 'int-value');
// This will return the float "9.99"
$access->float('root', 'float-value');
// This will return the bool "true"
$access->bool('root', 'bool-value');
// This will return the array "[1, 2, 3]"
$access->array('root', 'array-value');
// This will return a new ArrayAccess object
$access->arrayAccess('root', 'array-value');
// This will return a \DateTimeImmutable object
$access->dateTimeImmutable('Y-m-d H:i:s', 'root', 'datetime-value');
// This will return a \DateTime object
$access->dateTime('Y-m-d H:i:s', 'root', 'datetime-value');
// This will return the \stdClass object
$access->objectOfType(\stdClass::class, 'root', 'object-value');
// This will return a mixed, depending on the array content, but only if the custom validation passes
// In this case it will return the string "Choice 1"
$access->callback(
function ($value) {
return in_array($value, ['Choice 1', 'Choice 2', 'Choice 3']);
},
'root', 'custom'
);
写入路径
$access = ArrayAccess::create([]);
$access->writeAtPath('the-value', 'at', 'some', 'path');
$theValue = $access->string('at', 'some', 'path');