kurianvarkey/helper-buddy
最新稳定版本:v1.0.2
Composer 安装命令:
composer require kurianvarkey/helper-buddy
包简介
Helper class to validate and correct JSON attribute types, ensuring proper structure and defaults for string, int, float, and boolean.
README 文档
README
Installation
You can install the package via composer:
composer require kurianvarkey/helper-buddy
JsonBuddy
A json helper class to validate json structure. You can validate json structure by providing a list of keys and types.
- Supported types: string, array, object, int, float, decimal, boolean / bool
- Format: "path" => "type:default" (default is optional and only used when type is string, int, float, bool)
- Note: if the final attribute in the path doesn't exist, it will be created
For example
{
"name": null,
"structure": [
{
"rows": [
{
"columns": [
{
"price": null,
"data": []
},
{
"price": 2,
"data": []
}
]
},
{
"columns": [
{
"price": 1,
"data": {
"name": "data 1"
}
}
]
}
]
}
]
}
<?php use Net4ideas\HelperBuddy\JsonBuddy; $keys = [ "name" => "string:''", "structure.*.rows.*.columns.*.data" => "object", "structure.0.rows.0.columns.0.price" => "float:0", ]; $correctedJson = JsonBuddy::validateStructure($json, $keys);
will output:
{
"name": "",
"structure": [
{
"rows": [
{
"columns": [
{
"price": 0,
"data": {}
},
{
"price": 2,
"data": {}
}
]
},
{
"columns": [
{
"price": 1,
"data": {
"name": "data 1"
}
}
]
}
]
}
]
}
ArrayBuddy
A array helper class to remove null values recursively.
<?php use Net4ideas\HelperBuddy\ArrayBuddy; $input = [ "name" => "Jack", "age" => 40, "address" => null, "hobbies" => [ "reading", "writing", null ] ]; $result = ArrayBuddy::removeNullValues($input); /* Output: [ "name" => "Jack", "age" => 40, "hobbies" => [ "reading", "writing" ] ] */
Another method to replace null values with a string recursively.
<?php use Net4ideas\HelperBuddy\ArrayBuddy; $input = [ "name" => "Jack", "age" => 40, "address" => null, "hobbies" => [ "reading", "writing", null ] ]; $result = ArrayBuddy::replaceNullsWithString($input, 'default'); /* Output: [ "name" => "Jack", "age" => 40, "address" => "default", "hobbies" => [ "reading", "writing", "default" ] ] */
统计信息
- 总下载量: 29
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-02