windwalker / registry
3.5.1
2019-02-05 05:54 UTC
Requires
- php: >=7.1.3
Requires (Dev)
- ext-json: *
- symfony/yaml: 3.*
- windwalker/test: ~3.0
Suggests
- symfony/yaml: Install 3.* if you require YAML support.
- dev-master / 3.x-dev
- 3.5.1
- 3.5
- 3.4.9
- 3.4.8
- 3.4.7
- 3.4.6
- 3.4.5
- 3.4.4
- 3.4.3
- 3.4.2
- 3.4.1
- 3.4
- 3.3.2
- 3.3.1
- 3.3
- 3.2.8
- 3.2.7
- 3.2.6
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1
- 3.0.1
- 3.0
- 3.0-beta2
- 3.0-beta
- 2.1.9
- 2.1.8
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.2
- 2.1.1
- 2.1
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 2.0.0-beta2
- 2.0.0-beta1
- 2.0.0-alpha
- dev-test
This package is not auto-updated.
Last update: 2019-02-20 18:12:03 UTC
README
Windwalker 结构是一个嵌套数组或对象的存储库,帮助我们管理多层结构数据。
通过 Composer 安装
将以下内容添加到您的 composer.json
中的 require 块。
{ "require": { "windwalker/structure": "~3.0" } }
入门
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');
通过结构加载配置
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');
通过 getter & setter 访问结构
获取值
$structure->get('foo'); // Get a non-exists value and return default $structure->get('foo', 'default'); // OR $structure->get('foo') ?: 'default';
设置值
// Set value $structure->set('bar', $value); // Sets a default value if not already assigned. $structure->def('bar', $default);
通过路径访问子值
$json = '{ "parent" : { "child" : "Foo" } }'; $structure = new Structure($json); $structure->get('parent.child'); // return 'Foo' $structure->set('parent.child', $value);
追加 & 预先附加
支持 push / pop / shift / unshift
方法。
$structure->set('foo.bar', array('fisrt', 'second')); $structure->push('foo.bar', 'third'); $structure->get('foo.bar'); // Result: Array(first, second, third)
使用其他分隔符
$structure->setSeparator('/'); $data = $structure->get('foo/bar');
将结构作为数组访问
Structure
类实现了 ArrayAccess
,因此可以像数组一样访问结构的属性。以下是一些示例
// 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.'; }
合并结构
使用 load* 方法合并两个配置文件。
$json1 = '{ "field" : { "keyA" : "valueA", "keyB" : "valueB" } }'; $json2 = '{ "field" : { "keyB" : "a new valueB" } }'; $structure->loadString($json1); $structure->loadString($json2);
输出
Array(
field => Array(
keyA => valueA
keyB => a new valueB
)
)
合并另一个结构
$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);
如果您只想合并第一级,不需要递归
$structure1->merge($structure2, false); // Set param 2 to false that Structure will only merge first level
合并到子节点
$structure->mergeTo('foo.bar', $anotherStructure);
导出到文件。
$structure->toString(); $structure->toString('xml'); $structure->toString('ini');
导出到一维。
$array = array( 'flower' => array( 'sunflower' => 'light', 'sakura' => 'samurai' ) ); $structure = new Structure($array); // Make data to one dimension $flatted = $structure->flatten(); print_r($flatted);
结果
Array
(
[flower.sunflower] => light
[flower.sakura] => samurai
)
使用 YAML
在 composer.json
中添加 Symfony YAML 组件
{ "require-dev": { "symfony/yaml": "~3.0" } }
使用 yaml
格式
$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);