windwalker / structure
Windwalker Structure package
Package info
github.com/ventoviro/windwalker-structure
Type:windwalker-package
pkg:composer/windwalker/structure
Requires
- php: >=7.1.3
Requires (Dev)
- ext-json: *
- laktak/hjson: ^2.1
- symfony/yaml: ^4.0||^5.0
- windwalker/test: ~3.0
- yosymfony/toml: ^1.0
Suggests
- laktak/hjson: Install ~2.0 if you require HJSON support.
- symfony/yaml: Install 3.* if you require YAML support.
- yosymfony/toml: Install ~1.0 if you require TOML support.
- 3.x-dev
- dev-master / 3.x-dev
- 3.5.23
- 3.5.22
- 3.5.21
- 3.5.20
- 3.5.19
- 3.5.18
- 3.5.17
- 3.5.16
- 3.5.15
- 3.5.14
- 3.5.13
- 3.5.12
- 3.5.11
- 3.5.10
- 3.5.9
- 3.5.8
- 3.5.7
- 3.5.6
- 3.5.5
- 3.5.4
- 3.5.3
- 3.5.2
- 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 auto-updated.
Last update: 2026-03-18 18:03:19 UTC
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);