windwalker/structure
Composer 安装命令:
composer require windwalker/structure
包简介
Windwalker Structure package
README 文档
README
Windwalker Structure is a storage of nested array or object, help us manage multi-level structures data.
Installation via Composer
Add this to the require block in your composer.json.
{
"require": {
"windwalker/structure": "~3.0"
}
}
Supported Formats
Getting Started
use Windwalker\Structure\Structure; $structure = new Structure; // Set a value in the structure. $structure->set('foo', 'bar'); // Get a value from the structure; $value = $structure->get('foo');
Load config by Structure
use Windwalker\Structure\Structure; $structure = new Structure; // Load by string $structure->loadString('{"foo" : "bar"}'); $structure->loadString('<root></root>', 'xml'); // Load by object or array $structure->load($object); // Load by file $structure->loadFile($root . '/config/config.json', 'json');
Accessing a Structure by getter & setter
Get value
$structure->get('foo'); // Get a non-exists value and return default $structure->get('foo', 'default'); // OR $structure->get('foo') ?: 'default';
Set value
// Set value $structure->set('bar', $value); // Sets a default value if not already assigned. $structure->def('bar', $default);
Accessing children value by path
$json = '{ "parent" : { "child" : "Foo" } }'; $structure = new Structure($json); $structure->get('parent.child'); // return 'Foo' $structure->set('parent.child', $value);
Append & Prepend
Support push / pop / shift / unshift methods.
$structure->set('foo.bar', array('fisrt', 'second')); $structure->push('foo.bar', 'third'); $structure->get('foo.bar'); // Result: Array(first, second, third)
Use other separator
$structure->setSeparator('/'); $data = $structure->get('foo/bar');
Accessing a Structure as an Array
The Structure class implements ArrayAccess so the properties of the structure can be accessed as an array. Consider the following examples:
// Set a value in the structure. $structure['foo'] = 'bar'; // Get a value from the structure; $value = $structure['foo']; // Check if a key in the structure is set. if (isset($structure['foo'])) { echo 'Say bar.'; }
Merge Structure
Using load* methods to merge two config files.
$json1 = '{ "field" : { "keyA" : "valueA", "keyB" : "valueB" } }'; $json2 = '{ "field" : { "keyB" : "a new valueB" } }'; $structure->loadString($json1); $structure->loadString($json2);
Output
Array(
field => Array(
keyA => valueA
keyB => a new valueB
)
)
Merge Another Structure
$object1 = '{ "foo" : "foo value", "bar" : { "bar1" : "bar value 1", "bar2" : "bar value 2" } }'; $object2 = '{ "foo" : "foo value", "bar" : { "bar2" : "new bar value 2" } }'; $structure1 = new Structure(json_decode($object1)); $structure2 = new Structure(json_decode($object2)); $structure1->merge($structure2);
If you just want to merge first level, do not hope recursive:
$structure1->merge($structure2, false); // Set param 2 to false that Structure will only merge first level
Merge to a child node:
$structure->mergeTo('foo.bar', $anotherStructure);
Dump to file.
$structure->toString(); $structure->toString('xml'); $structure->toString('ini');
Dump to one dimension
$array = array( 'flower' => array( 'sunflower' => 'light', 'sakura' => 'samurai' ) ); $structure = new Structure($array); // Make data to one dimension $flatted = $structure->flatten(); print_r($flatted);
The result:
Array
(
[flower.sunflower] => light
[flower.sakura] => samurai
)
Using YAML
Add Symfony YAML component in composer.json
{
"require-dev": {
"symfony/yaml": "^4.0||^5.0"
}
}
Using yaml format
$structure->loadFile($yamlFile, 'yaml'); $structure->loadString('foo: bar', 'yaml'); // Convert to string $structure->toString('yaml');
StructureHelper
use Windwalker\Structure\StructureHelper; StructureHelper::loadFaile($file, $format); // File to array StructureHelper::loadString($string, $format); // String to array StructureHelper::toString($array, $format); // Array to string // Use format class $json = StructureHelper::getFormatClass('json'); // Get JsonFormat $string = $json::structToString($array);
windwalker/structure 适用场景与选型建议
windwalker/structure 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.74M 次下载、GitHub Stars 达 41, 最近一次更新时间为 2016 年 07 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「structure」 「windwalker」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 windwalker/structure 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 windwalker/structure 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 windwalker/structure 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
Simple, immutable data structures
Extension provide very simply use enum for models (and others) in yii2
Windwalker Queue package
TYPO3 extension providing content elements powered by gridelements and bootstrap. Available elements: container, columns, tabs, accordion, tile unit and card.
Windwalker Promise package
统计信息
- 总下载量: 4.74M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 43
- 点击次数: 16
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: LGPL-2.0-or-later
- 更新时间: 2016-07-03