jbzoo / data
ArrayObject 对象的扩展版本,用于处理系统设置或仅用于处理数据数组
7.1.1
2024-01-28 08:47 UTC
Requires
- php: ^8.1
- ext-json: *
Requires (Dev)
- jbzoo/toolbox-dev: ^7.1
- jbzoo/utils: ^7.1.1
- symfony/polyfill-ctype: >=1.28.0
- symfony/polyfill-mbstring: >=1.28.0
- symfony/polyfill-php73: >=1.28.0
- symfony/polyfill-php80: >=1.28.0
- symfony/polyfill-php81: >=1.28.0
- symfony/yaml: >=6.4
Suggests
- jbzoo/utils: >=7.1
- symfony/yaml: >=6.4
This package is auto-updated.
Last update: 2024-08-30 01:26:00 UTC
README
ArrayObject 对象的扩展版本,用于处理系统设置或仅用于处理数据数组。
它提供了一种简短的语法,用于日常操作,消除了常见错误。允许您处理各种行和文件格式 - JSON、Yml、Ini、PHP 数组和简单对象。
安装
composer require jbzoo/data
使用方法
与纯 PHP 的比较
了解您的数据
$json = json('{ "some": "thing", "number": 42 }'); dump($json->getSchema(); // [ // "some" => "string", // "number" => "int" // ]
方法
use function JBZoo\Data\data; use function JBZoo\Data\ini; use function JBZoo\Data\json; use function JBZoo\Data\phpArray; use function JBZoo\Data\yml; $config = data([/* Assoc Array */]); // Any PHP-array or simple object, serialized data $config = ini('./configs/some.ini'); // Load configs from ini file (or string, or simple array) $config = yml('./configs/some.yml'); // Yml (or string, or simple array). Parsed with Symfony/Yaml Component. $config = json('./configs/some.json'); // JSON File (or string, or simple array) $config = phpArray('./configs/some.php'); // PHP-file that must return array // Read $config->get('key', 42); // Returns value if it exists oR returns default value $config['key']; // As regular array $config->key; // As regular object // Read nested values without PHP errors $config->find('deep.config.key', 42); // Gets `$config['very']['deep']['config']['key']` OR returns default value // Write $config->set('key', 42); $config['key'] = 42; $config->key = 42; // Isset $config->has('key'); isset($config['key']); isset($config->key); // Unset $config->remove('key'); unset($config['key']); unset($config->key);
过滤值(需要 JBZoo/Utils)
过滤器列表 - JBZoo/Utils/Filter
bool
- 将许多等同于 true 或 false 的英文单词转换为布尔值。int
- 智能转换为整数float
- 智能转换为浮点数digits
- 仅保留 "0-9" 数字alpha
- 仅保留 "a-zA-Z" 字母alphanum
-digits
和alpha
的组合base64
- 返回与 base64 兼容的字符path
- 清理文件系统路径trim
- 扩展 trimarr
- 转换为数组cmd
- 清理系统命令(CLI)email
- 返回清理后的电子邮件或 nullstrip
- 删除标签alias
- Sluggifylow
- 将字符串转换为小写(使用 mbstring 或 symfony polyfill)up
- 将字符串转换为大写(使用 mbstring 或 symfony polyfill)clean
- 返回安全的字符串html
- HTML 转义xml
- XML 转义esc
- 为 UTF-8 转义字符function($value) { return $value; }
- 您的自定义回调函数
$config->get('key', 42, 'int'); // Smart converting to integer $config->find('key', 42, 'float'); // To float $config->find('no', 'yes', 'bool'); // Smart converting popular word to boolean value $config->get('key', 42, 'strip, trim'); // Chain of filters // Your custom handler $config->get('key', 42, function($value) { return (float)str_replace(',', '.', $value); });
实用方法
$config->search($needle); // Find a value also in nested arrays/objects $config->flattenRecursive(); // Return flattened array copy. Keys are <b>NOT</b> preserved.
导出到美观打印格式
echo $config; $result = '' . $config; $result = (string)$config; $result = $config->__toString();
序列化 JSON 对象的示例
{ "empty": "", "zero": "0", "string": " ", "tag": "<a href=\"http:\/\/google.com\">Google.com<\/a>", "array1": { "0": "1", "1": "2" }, "section": { "array2": { "0": "1", "12": "2", "3": "3" } }, "section.nested": { "array3": { "00": "0", "01": "1" } } }
序列化 PHPArray 对象的示例
<?php return array( 'empty' => '', 'zero' => '0', 'string' => ' ', 'tag' => '<a href="http://google.com">Google.com</a>', 'array1' => array( 0 => '1', 1 => '2', ), 'section' => array( 'array2' => array( 0 => '1', 12 => '2', 3 => '3', ), ), 'section.nested' => array( 'array3' => array( '00' => '0', '01' => '1', ), ), );
序列化 Yml 对象的示例
empty: '' zero: '0' string: ' ' tag: '<a href="http://google.com">Google.com</a>' array1: - '1' - '2' section: array2: { 0: '1', 12: '2', 3: '3' } section.nested: array3: ['0', '1']
序列化 Ini 对象的示例
empty = "" zero = "0" string = " " tag = "<a href=\"http://google.com\">Google.com</a>" array1[0] = "1" array1[1] = "2" [section] array2[0] = "1" array2[12] = "2" array2[3] = "3" [section.nested] array3[00] = "0" array3[01] = "1"
序列化 Data 对象的示例
a:7:{s:5:"empty";s:0:"";s:4:"zero";s:1:"0";s:6:"string";s:1:" ";s:3:"tag";s:42:"<a href="http://google.com">Google.com</a>";s:6:"array1";a:2:{i:0;s:1:"1";i:1;s:1:"2";}s:7:"section";a:1:{s:6:"array2";a:3:{i:0;s:1:"1";i:12;s:1:"2";i:3;s:1:"3";}}s:14:"section.nested";a:1:{s:6:"array3";a:2:{s:2:"00";s:1:"0";s:2:"01";s:1:"1";}}}
PHP v7.4 的基准测试信息摘要(执行时间)
所有基准测试都在没有 xdebug 的情况下,使用大型随机数组和 100,000 次迭代执行。
基准测试基于工具 phpbench/phpbench。详情请见此处。
请注意 - 1μs = 1/1,000,000 秒!
基准测试:CreateObject
基准测试:GetUndefinedValue
基准测试:GetValue
基准测试:GetValueInner
单元测试和代码风格检查
make update make test-all
许可证
MIT
另请参阅
- CI-Report-Converter - 转换不同错误报告,以实现与流行 CI 系统的深度兼容。
- Composer-Diff - 查看
composer update
后发生变化的包。 - Composer-Graph - 基于 mermaid-js 的 composer.json 的依赖图可视化。
- Mermaid-PHP - 使用 mermaid 脚本语言生成图表和流程图。
- Utils - 收集有用的 PHP 函数、迷你类和日常片段。
- Image - 该包以面向对象的方式提供尽可能简单的图像操作方法。
- Retry - 一个小巧的PHP库,提供重试/退避功能,支持多种退避策略和抖动支持。
- SimpleTypes - 转换任何值和度量 - 货币、重量、汇率、长度、...