You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+58-55Lines changed: 58 additions & 55 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,18 +21,20 @@ _The name comes from the fact that it was initially made to implement a pseudo-s
21
21
22
22
## Installation
23
23
24
-
It's a standalone component:
24
+
It is a standalone component:
25
25
26
26
* the core requires PHP >= 5.3.3
27
-
* to use the YamlLoader, you'll need the Symfony component [Yaml](https://github.com/symfony/Yaml) (standalone component, does not require Symfony2)
27
+
* to use the YamlLoader, you will need the Symfony component [Yaml](https://github.com/symfony/Yaml) (standalone component, does not require Symfony2)
28
28
* to launch the tests, you'll need [atoum](https://github.com/mageekguy/atoum)
29
29
30
-
To install all these packages, the easiest way is to use [composer](http://getcomposer.org): put composer.phar in root folder, and then run `./composer.phar --update`
30
+
To install all these packages, you can use [composer](http://getcomposer.org): just do `composer --update`
31
31
32
32
## Basic usage
33
33
34
34
You have to create a MetaYaml object, and then pass it both the schema and your data as multidimensional php arrays:
35
35
```php
36
+
use RomaricDrigon\MetaYaml\MetaYaml;
37
+
36
38
// create object, load schema from an array
37
39
$schema = new MetaYaml($schema);
38
40
@@ -44,25 +46,27 @@ $schema = new MetaYaml($schema);
44
46
$schema->validate_schema(); // return true or throw an exception
45
47
46
48
// you could also have done this at init
47
-
$schema = new MetaYaml($schema, true); // will load AND validate
49
+
$schema = new MetaYaml($schema, true); // will load AND validate the schema
48
50
49
-
// finally, validate your data according to the schema
51
+
// finally, validate your data array according to the schema
50
52
$schema->validate($data); // return true or throw an exception
51
53
```
52
54
53
-
You can use any of the provided loaders to obtain these arrays (yep, you can validate Xml using a schema from an Yaml file!).
55
+
You can use any of the provided loaders to obtain these arrays (yep, you can validate XML using a schema from an Yaml file!).
54
56
55
57
Some loader examples:
56
58
```php
57
59
use RomaricDrigon\MetaYaml\MetaYaml;
60
+
use RomaricDrigon\MetaYaml\Loader\YamlLoader;
61
+
use RomaricDrigon\MetaYaml\Loader\XmlLoader; // JsonLoader is already available
58
62
59
63
// create one loader object
60
64
$loader = new JsonLoader(); // Json (will use php json_decode)
61
65
$loader = new YamlLoader(); // Yaml using Symfony Yaml component
62
66
$loader = new XmlLoader(); // Xml (using php SimpleXml)
A schema file will define the array structure (which elements are allowed, where), some attributes (required, can be empty...) and the possible values for these elements (or their type).
75
79
76
-
Here's a simple example of a schema, using Yaml syntax:
80
+
Here's a simple example of a schema, using Yaml syntax:
77
81
```yaml
78
-
root: # root is always required node ; no prefix here
79
-
_type: array # each element must always have a _type
80
-
_children: # array nodes have a _children, defining their children
81
-
fleurs:
82
+
root: # root is always required (note no prefix here)
83
+
_type: array # each element must always have a '_type'
84
+
_children: # array nodes have a '_children' node, defining their children
85
+
flowers:
82
86
_type: array
83
87
_required: true # optional, default false
84
88
_children:
85
89
rose:
86
90
_required: true
87
91
_type: text
88
-
violette:
92
+
violet:
89
93
_type: text
90
-
# -> only rose and violette are allowed children of fleurs
94
+
# -> only rose and violet are allowed children of flowers
91
95
```
92
96
93
97
And a valid Yaml file :
94
98
```yaml
95
-
fleurs:
96
-
rose: une rose
97
-
violette: une violette
99
+
flowers:
100
+
rose: "a rose"
101
+
violet: "a violet flower"
98
102
```
99
103
100
-
We'll continue with Yaml examples; if you're not familiar with the syntax, you may want to take a look at it's [Wikipedia page](http://en.wikipedia.org/wiki/YAML).
101
-
Of courses the same structures are possible with Json and XML, because the core is the same ; take a look at examples in test/data/ folder.
104
+
We will continue with Yaml examples; if you're not familiar with the syntax, you may want to take a look at its [Wikipedia page](http://en.wikipedia.org/wiki/YAML).
105
+
Of course the same structure is possible with Json or XML, because the core is the same. Take a look at examples in `test/data/` folder.
102
106
103
107
### Schema structure
104
108
105
-
A schema file must have a `root` node, which will described the first-level content.
106
-
You can optionally define a `prefix`; by defaults it's `_` (`_type`, `_required`...).
107
-
You have to define a `partials` node if you want to use this feature.
109
+
A schema file must have a `root` node, which will describe the first-level content.
110
+
You can optionally define a `prefix`; by default it is `_` (`_type`, `_required`...).
111
+
112
+
You have to define a `partials` node if you want to use this feature (learn more about it below).
108
113
109
-
So a basic schema file:
114
+
A basic schema file:
110
115
```yaml
111
116
root:
112
117
# here put the elements who will be in the file
113
-
# note that root can be anything: an array, a number, a prototype...
114
-
prefix: my_ # so it's gonna be my_type, my_required, my_children...
118
+
# note that root can have any type: an array, a number, a prototype...
119
+
prefix: my_ # so it's gonna be 'my_type', 'my_required', 'my_children'...
115
120
partials:
116
121
block:
117
122
# here I define a partial called block
@@ -133,7 +138,7 @@ Those types are available:
133
138
* `boolean`: boolean value
134
139
* `pattern`: check if the value matches the regular expression provided in `_pattern`, which is a [PCRE regex](http://www.php.net/manual/en/reference.pcre.pattern.syntax.php)
135
140
* `enum`: enumeration ; list accepted values in `_values` node
136
-
* `array`: array; define children in a _children node; array children must have named keys; any extra key will provoke an error
141
+
* `array`: array; define children in a _children node; array's children must have determined named keys; any extra key will cause an error
137
142
* `prototype`: define a repetition of items whose name/index is not important. You must give children's type in `_prototype` node.
138
143
* `choice`: child node can be any of the nodes provided in `_choices`. Keys in `_choices` array are not important (as long as they are unique). In each choice, it's best to put the discriminating field in first.
139
144
* `partial`: "shortcut" to a block described in `partials` root node. Provide partial name in `_partial`
@@ -142,9 +147,8 @@ You can specify additional attributes:
142
147
143
148
* general attributes:
144
149
* `_required`: this node must always be defined (by default false)
145
-
* `_not_empty` for text, number and array nodes: they can't be empty (by default false). Respective empty values are `''`, `0` (as a string, an integer or a float), `array()`. To test for null values, use `_required` instead.
146
-
* `_strict` with text, number, boolean and enum will enforce a strict type check (respectively, with a string, an integer or a float, a boolean, any of these values).
147
-
Watch out when using these with a parser which may not be type-aware (such as the XML one; Yaml and Json should be ok)
150
+
* `_not_empty` for text, number and array nodes: they can't be empty (by default false). Respective empty values are `''`, `0` (as a string, an integer or a float), `array()`. To test for `null` values, use `_required` instead.
151
+
* `_strict` with text, number, boolean and enum will enforce a strict type check (respectively, with a string, an integer or a float, a boolean, any of these values). Be careful when using these with a parser which may not be type-aware (such as the XML one; Yaml and json should be ok)
* `_ignore_extra_keys`: the node can contain children whose keys are not listed in `_children`; they'll be ignored
@@ -157,32 +161,32 @@ Here's a comprehensive example:
157
161
root:
158
162
_type: array
159
163
_children:
160
-
texte:
164
+
SomeText:
161
165
_type: text
162
-
_not_empty: true
163
-
enume:
166
+
_not_empty: true # so !== ''
167
+
SomeEnum:
164
168
_type: enum
165
169
_values:
166
170
- windows
167
171
- mac
168
172
- linux
169
-
entier:
173
+
SomeNumber:
170
174
_type: number
171
175
_strict: true
172
-
booleen:
176
+
SomeBool:
173
177
_type: boolean
174
-
prototype_array:
178
+
SomePrototypeArray:
175
179
_type: prototype
176
180
_prototype:
177
181
_type: array
178
182
_children:
179
-
texte:
183
+
SomeOtherText:
180
184
_type: text
181
-
_is_required: true
182
-
paragraph:
185
+
_is_required: true # can't be null
186
+
SomeParagraph:
183
187
_type: partial
184
-
_partial: block
185
-
test_choice:
188
+
_partial: aBlock # cf 'partials' below
189
+
SomeChoice:
186
190
_type: choice
187
191
_choices:
188
192
1:
@@ -192,20 +196,21 @@ root:
192
196
- linux
193
197
2:
194
198
_type: number
195
-
regex:
199
+
# so our node must be either #1 or #2
200
+
SomeRegex:
196
201
_type: pattern
197
202
_pattern: /e/
198
203
partials:
199
-
block:
204
+
aBlock:
200
205
_type: array
201
206
_children:
202
-
line_1:
207
+
Line1:
203
208
_type: text
204
209
```
205
210
206
211
### More information
207
212
208
-
For more examples, look inside test/data folder.
213
+
For more examples, look inside `test/data` folder.
209
214
In each folder, you have an .yml file and its schema. There's also a XML example.
210
215
211
216
If you're curious about an advanced usage, you can check `data/MetaSchema.json`: schema files are validated using this schema (an yep, the schema validates successfully itself!)
@@ -215,7 +220,6 @@ If you're curious about an advanced usage, you can check `data/MetaSchema.json`:
215
220
Each node can have a `_description` attribute, containing some human-readable text.
216
221
You can retrieve the documentation about a node (its type, description, other attributes...) like this:
217
222
```php
218
-
// create object, load schema from an array
219
223
// it's recommended to validate the schema before reading documentation
@@ -247,7 +251,7 @@ If the targeted node is inside a choice, the result will differ slightly:
247
251
array(
248
252
'name' => 'test', // name of current node, from the choice key in the schema
249
253
'node' => array(
250
-
'_is_choice' => 'true', // important: so we know next keys are choices
254
+
'_is_choice' => 'true', // important: so we know next keys are choices
251
255
0 => array(
252
256
'_type' => 'array' // and so on, for first choice
253
257
),
@@ -259,9 +263,9 @@ array(
259
263
'prefix' => '_'
260
264
)
261
265
```
262
-
This behavior allow us to handle imbricated choices, without loosing data (you'll an array level for each level, is set with the flag `_is_choice`)
266
+
This behavior allow us to handle imbricated choices, without loosing data (you have an array level for each choice level, and you can check the flag `_is_choice`)
263
267
264
-
If you pass an invalid path (eg a named node does not exist), it will throw an exception.
268
+
If you pass an invalid path (e.g. no node with the name you gave exist), it will throw an exception.
265
269
266
270
## Notes on XML support
267
271
@@ -275,7 +279,7 @@ Thus, the following conventions are enforced by the XML loader:
275
279
* if a node has both attribute(s) and a text content, text content will be stored under key `_value`
276
280
* multiple child node with the same name will be overwritten, only the last will be retained; except if they have a `_key` attribute, which will be used thus
277
281
* namespaces are not supported
278
-
* empty nodes are ditched
282
+
* empty nodes are skipped
279
283
280
284
Let's take an example:
281
285
```xml
@@ -317,11 +321,11 @@ array('fleurs' =>
317
321
318
322
## XSD generator
319
323
320
-
_**Please note this feature is still experimental**_
324
+
_**Please note this feature is still experimental!**_
321
325
322
326
MetaYaml can try to generate a [XML Schema Definition](http://en.wikipedia.org/wiki/XML_Schema) from a MetaYaml schema.
323
327
You may want to use this file to pre-validate XML input, or to use in another context (client-side...).
324
-
The same conventions (cf above) will be used.
328
+
The same conventions (cf. above) will be used.
325
329
326
330
Usage example :
327
331
```php
@@ -335,7 +339,6 @@ $generator = new XsdGenerator();
A few limitations, some relative to XML Schema, apply:
340
343
* `root` node must be an `array`
341
344
* an element can't have a name beginning by a number
@@ -354,10 +357,10 @@ To launch tests, just run in a shell `./bin/test --test-all`
354
357
## Extending
355
358
356
359
You may want to write your own loader, using anything else.
357
-
Take a look at any class in Loader/ folder, it's pretty easy: you have to implement the LoaderInterface, and may want to extend Loader class (so you don't have to write `loadFromFile()`).
360
+
Take a look at any class in `Loader/ folder`, it's pretty easy: you have to implement the LoaderInterface, and may want to extend Loader class (so you don't have to write `loadFromFile()`).
358
361
359
362
## Thanks
360
363
361
-
Thanks to [Riad Benguella](https://github.com/youknowriad) and [Julien Bianchi](https://github.com/jubianchi) for their help & advices.
364
+
Thanks to [Riad Benguella](https://github.com/youknowriad) and [Julien Bianchi](https://github.com/jubianchi) for their help & advice.
0 commit comments