Vous êtes connecté en tant que anonymous Se Deconnecter
Browse code

Application modulaire fonctionnelle !

Emmanuel ROY authored on 12/08/2019 15:10:25
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,285 +0,0 @@
1
-# Windwalker Structure
2
-
3
-Windwalker Structure is a storage of nested array or object, help us manage multi-level structures data.
4
-
5
-## Installation via Composer
6
-
7
-Add this to the require block in your `composer.json`.
8
-
9
-``` json
10
-{
11
-    "require": {
12
-        "windwalker/structure": "~3.0"
13
-    }
14
-}
15
-```
16
-
17
-## Supported Formats
18
-
19
-- JSON
20
-- PHP file (return array or class)
21
-- JSON
22
-- [HJSON](https://hjson.org/)
23
-- YAML
24
-- [TOML](https://github.com/toml-lang/toml)
25
-- XML
26
-- INI
27
-
28
-## Getting Started
29
-
30
-``` php
31
-use Windwalker\Structure\Structure;
32
-
33
-$structure = new Structure;
34
-
35
-// Set a value in the structure.
36
-$structure->set('foo', 'bar');
37
-
38
-// Get a value from the structure;
39
-$value = $structure->get('foo');
40
-
41
-```
42
-
43
-## Load config by Structure
44
-
45
-``` php
46
-use Windwalker\Structure\Structure;
47
-
48
-$structure = new Structure;
49
-
50
-// Load by string
51
-$structure->loadString('{"foo" : "bar"}');
52
-
53
-$structure->loadString('<root></root>', 'xml');
54
-
55
-// Load by object or array
56
-$structure->load($object);
57
-
58
-// Load by file
59
-$structure->loadFile($root . '/config/config.json', 'json');
60
-```
61
-
62
-## Accessing a Structure by getter & setter
63
-
64
-### Get value
65
-
66
-``` php
67
-$structure->get('foo');
68
-
69
-// Get a non-exists value and return default
70
-$structure->get('foo', 'default');
71
-
72
-// OR
73
-
74
-$structure->get('foo') ?: 'default';
75
-```
76
-
77
-### Set value
78
-
79
-``` php
80
-// Set value
81
-$structure->set('bar', $value);
82
-
83
-// Sets a default value if not already assigned.
84
-$structure->def('bar', $default);
85
-```
86
-
87
-### Accessing children value by path
88
-
89
-``` php
90
-$json = '{
91
-	"parent" : {
92
-		"child" : "Foo"
93
-	}
94
-}';
95
-
96
-$structure = new Structure($json);
97
-
98
-$structure->get('parent.child'); // return 'Foo'
99
-
100
-$structure->set('parent.child', $value);
101
-```
102
-
103
-### Append & Prepend
104
-
105
-Support `push / pop / shift / unshift` methods.
106
-
107
-``` php
108
-$structure->set('foo.bar', array('fisrt', 'second'));
109
-
110
-$structure->push('foo.bar', 'third');
111
-
112
-$structure->get('foo.bar');
113
-// Result: Array(first, second, third)
114
-```
115
-
116
-### Use other separator
117
-
118
-``` php
119
-$structure->setSeparator('/');
120
-
121
-$data = $structure->get('foo/bar');
122
-```
123
-
124
-## Accessing a Structure as an Array
125
-
126
-The `Structure` class implements `ArrayAccess` so the properties of the structure can be accessed as an array. Consider the following examples:
127
-
128
-``` php
129
-// Set a value in the structure.
130
-$structure['foo'] = 'bar';
131
-
132
-// Get a value from the structure;
133
-$value = $structure['foo'];
134
-
135
-// Check if a key in the structure is set.
136
-if (isset($structure['foo']))
137
-{
138
-	echo 'Say bar.';
139
-}
140
-```
141
-
142
-## Merge Structure
143
-
144
-#### Using load* methods to merge two config files.
145
-
146
-``` php
147
-$json1 = '{
148
-    "field" : {
149
-        "keyA" : "valueA",
150
-        "keyB" : "valueB"
151
-    }
152
-}';
153
-
154
-$json2 = '{
155
-    "field" : {
156
-        "keyB" : "a new valueB"
157
-    }
158
-}';
159
-
160
-$structure->loadString($json1);
161
-$structure->loadString($json2);
162
-```
163
-
164
-Output
165
-
166
-```
167
-Array(
168
-    field => Array(
169
-        keyA => valueA
170
-        keyB => a new valueB
171
-    )
172
-)
173
-```
174
-
175
-#### Merge Another Structure
176
-
177
-``` php
178
-$object1 = '{
179
-	"foo" : "foo value",
180
-	"bar" : {
181
-		"bar1" : "bar value 1",
182
-		"bar2" : "bar value 2"
183
-	}
184
-}';
185
-
186
-$object2 = '{
187
-	"foo" : "foo value",
188
-	"bar" : {
189
-		"bar2" : "new bar value 2"
190
-	}
191
-}';
192
-
193
-$structure1 = new Structure(json_decode($object1));
194
-$structure2 = new Structure(json_decode($object2));
195
-
196
-$structure1->merge($structure2);
197
-```
198
-
199
-If you just want to merge first level, do not hope recursive:
200
-
201
-``` php
202
-$structure1->merge($structure2, false); // Set param 2 to false that Structure will only merge first level
203
-```
204
-
205
-Merge to a child node:
206
-
207
-``` php
208
-$structure->mergeTo('foo.bar', $anotherStructure);
209
-```
210
-
211
-## Dump to file.
212
-
213
-``` php
214
-$structure->toString();
215
-
216
-$structure->toString('xml');
217
-
218
-$structure->toString('ini');
219
-```
220
-
221
-## Dump to one dimension
222
-
223
-``` php
224
-$array = array(
225
-    'flower' => array(
226
-        'sunflower' => 'light',
227
-        'sakura' => 'samurai'
228
-    )
229
-);
230
-
231
-$structure = new Structure($array);
232
-
233
-// Make data to one dimension
234
-
235
-$flatted = $structure->flatten();
236
-
237
-print_r($flatted);
238
-```
239
-
240
-The result:
241
-
242
-```
243
-Array
244
-(
245
-    [flower.sunflower] => light
246
-    [flower.sakura] => samurai
247
-)
248
-```
249
-
250
-## Using YAML
251
-
252
-Add Symfony YAML component in `composer.json`
253
-
254
-``` json
255
-{
256
-	"require-dev": {
257
-		"symfony/yaml": "~3.0"
258
-	}
259
-}
260
-```
261
-
262
-Using `yaml` format
263
-
264
-``` php
265
-$structure->loadFile($yamlFile, 'yaml');
266
-
267
-$structure->loadString('foo: bar', 'yaml');
268
-
269
-// Convert to string
270
-$structure->toString('yaml');
271
-```
272
-
273
-## StructureHelper
274
-
275
-``` php
276
-use Windwalker\Structure\StructureHelper;
277
-
278
-StructureHelper::loadFaile($file, $format); // File to array
279
-StructureHelper::loadString($string, $format); // String to array
280
-StructureHelper::toString($array, $format); // Array to string
281
-
282
-// Use format class
283
-$json = StructureHelper::getFormatClass('json'); // Get JsonFormat
284
-$string = $json::structToString($array);
285
-```
Browse code

initial commit

Emmanuel ROY authored on 09/08/2019 08:39:02
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,285 @@
1
+# Windwalker Structure
2
+
3
+Windwalker Structure is a storage of nested array or object, help us manage multi-level structures data.
4
+
5
+## Installation via Composer
6
+
7
+Add this to the require block in your `composer.json`.
8
+
9
+``` json
10
+{
11
+    "require": {
12
+        "windwalker/structure": "~3.0"
13
+    }
14
+}
15
+```
16
+
17
+## Supported Formats
18
+
19
+- JSON
20
+- PHP file (return array or class)
21
+- JSON
22
+- [HJSON](https://hjson.org/)
23
+- YAML
24
+- [TOML](https://github.com/toml-lang/toml)
25
+- XML
26
+- INI
27
+
28
+## Getting Started
29
+
30
+``` php
31
+use Windwalker\Structure\Structure;
32
+
33
+$structure = new Structure;
34
+
35
+// Set a value in the structure.
36
+$structure->set('foo', 'bar');
37
+
38
+// Get a value from the structure;
39
+$value = $structure->get('foo');
40
+
41
+```
42
+
43
+## Load config by Structure
44
+
45
+``` php
46
+use Windwalker\Structure\Structure;
47
+
48
+$structure = new Structure;
49
+
50
+// Load by string
51
+$structure->loadString('{"foo" : "bar"}');
52
+
53
+$structure->loadString('<root></root>', 'xml');
54
+
55
+// Load by object or array
56
+$structure->load($object);
57
+
58
+// Load by file
59
+$structure->loadFile($root . '/config/config.json', 'json');
60
+```
61
+
62
+## Accessing a Structure by getter & setter
63
+
64
+### Get value
65
+
66
+``` php
67
+$structure->get('foo');
68
+
69
+// Get a non-exists value and return default
70
+$structure->get('foo', 'default');
71
+
72
+// OR
73
+
74
+$structure->get('foo') ?: 'default';
75
+```
76
+
77
+### Set value
78
+
79
+``` php
80
+// Set value
81
+$structure->set('bar', $value);
82
+
83
+// Sets a default value if not already assigned.
84
+$structure->def('bar', $default);
85
+```
86
+
87
+### Accessing children value by path
88
+
89
+``` php
90
+$json = '{
91
+	"parent" : {
92
+		"child" : "Foo"
93
+	}
94
+}';
95
+
96
+$structure = new Structure($json);
97
+
98
+$structure->get('parent.child'); // return 'Foo'
99
+
100
+$structure->set('parent.child', $value);
101
+```
102
+
103
+### Append & Prepend
104
+
105
+Support `push / pop / shift / unshift` methods.
106
+
107
+``` php
108
+$structure->set('foo.bar', array('fisrt', 'second'));
109
+
110
+$structure->push('foo.bar', 'third');
111
+
112
+$structure->get('foo.bar');
113
+// Result: Array(first, second, third)
114
+```
115
+
116
+### Use other separator
117
+
118
+``` php
119
+$structure->setSeparator('/');
120
+
121
+$data = $structure->get('foo/bar');
122
+```
123
+
124
+## Accessing a Structure as an Array
125
+
126
+The `Structure` class implements `ArrayAccess` so the properties of the structure can be accessed as an array. Consider the following examples:
127
+
128
+``` php
129
+// Set a value in the structure.
130
+$structure['foo'] = 'bar';
131
+
132
+// Get a value from the structure;
133
+$value = $structure['foo'];
134
+
135
+// Check if a key in the structure is set.
136
+if (isset($structure['foo']))
137
+{
138
+	echo 'Say bar.';
139
+}
140
+```
141
+
142
+## Merge Structure
143
+
144
+#### Using load* methods to merge two config files.
145
+
146
+``` php
147
+$json1 = '{
148
+    "field" : {
149
+        "keyA" : "valueA",
150
+        "keyB" : "valueB"
151
+    }
152
+}';
153
+
154
+$json2 = '{
155
+    "field" : {
156
+        "keyB" : "a new valueB"
157
+    }
158
+}';
159
+
160
+$structure->loadString($json1);
161
+$structure->loadString($json2);
162
+```
163
+
164
+Output
165
+
166
+```
167
+Array(
168
+    field => Array(
169
+        keyA => valueA
170
+        keyB => a new valueB
171
+    )
172
+)
173
+```
174
+
175
+#### Merge Another Structure
176
+
177
+``` php
178
+$object1 = '{
179
+	"foo" : "foo value",
180
+	"bar" : {
181
+		"bar1" : "bar value 1",
182
+		"bar2" : "bar value 2"
183
+	}
184
+}';
185
+
186
+$object2 = '{
187
+	"foo" : "foo value",
188
+	"bar" : {
189
+		"bar2" : "new bar value 2"
190
+	}
191
+}';
192
+
193
+$structure1 = new Structure(json_decode($object1));
194
+$structure2 = new Structure(json_decode($object2));
195
+
196
+$structure1->merge($structure2);
197
+```
198
+
199
+If you just want to merge first level, do not hope recursive:
200
+
201
+``` php
202
+$structure1->merge($structure2, false); // Set param 2 to false that Structure will only merge first level
203
+```
204
+
205
+Merge to a child node:
206
+
207
+``` php
208
+$structure->mergeTo('foo.bar', $anotherStructure);
209
+```
210
+
211
+## Dump to file.
212
+
213
+``` php
214
+$structure->toString();
215
+
216
+$structure->toString('xml');
217
+
218
+$structure->toString('ini');
219
+```
220
+
221
+## Dump to one dimension
222
+
223
+``` php
224
+$array = array(
225
+    'flower' => array(
226
+        'sunflower' => 'light',
227
+        'sakura' => 'samurai'
228
+    )
229
+);
230
+
231
+$structure = new Structure($array);
232
+
233
+// Make data to one dimension
234
+
235
+$flatted = $structure->flatten();
236
+
237
+print_r($flatted);
238
+```
239
+
240
+The result:
241
+
242
+```
243
+Array
244
+(
245
+    [flower.sunflower] => light
246
+    [flower.sakura] => samurai
247
+)
248
+```
249
+
250
+## Using YAML
251
+
252
+Add Symfony YAML component in `composer.json`
253
+
254
+``` json
255
+{
256
+	"require-dev": {
257
+		"symfony/yaml": "~3.0"
258
+	}
259
+}
260
+```
261
+
262
+Using `yaml` format
263
+
264
+``` php
265
+$structure->loadFile($yamlFile, 'yaml');
266
+
267
+$structure->loadString('foo: bar', 'yaml');
268
+
269
+// Convert to string
270
+$structure->toString('yaml');
271
+```
272
+
273
+## StructureHelper
274
+
275
+``` php
276
+use Windwalker\Structure\StructureHelper;
277
+
278
+StructureHelper::loadFaile($file, $format); // File to array
279
+StructureHelper::loadString($string, $format); // String to array
280
+StructureHelper::toString($array, $format); // Array to string
281
+
282
+// Use format class
283
+$json = StructureHelper::getFormatClass('json'); // Get JsonFormat
284
+$string = $json::structToString($array);
285
+```