Skip to content

Commit ace5caa

Browse files
committed
Some typos corrected, and clearer
1 parent 1383a8f commit ace5caa

File tree

1 file changed

+58
-55
lines changed

1 file changed

+58
-55
lines changed

README.md

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@ _The name comes from the fact that it was initially made to implement a pseudo-s
2121

2222
## Installation
2323

24-
It's a standalone component:
24+
It is a standalone component:
2525

2626
* 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)
2828
* to launch the tests, you'll need [atoum](https://github.com/mageekguy/atoum)
2929

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`
3131

3232
## Basic usage
3333

3434
You have to create a MetaYaml object, and then pass it both the schema and your data as multidimensional php arrays:
3535
```php
36+
use RomaricDrigon\MetaYaml\MetaYaml;
37+
3638
// create object, load schema from an array
3739
$schema = new MetaYaml($schema);
3840

@@ -44,25 +46,27 @@ $schema = new MetaYaml($schema);
4446
$schema->validate_schema(); // return true or throw an exception
4547

4648
// 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
4850

49-
// finally, validate your data according to the schema
51+
// finally, validate your data array according to the schema
5052
$schema->validate($data); // return true or throw an exception
5153
```
5254

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!).
5456

5557
Some loader examples:
5658
```php
5759
use RomaricDrigon\MetaYaml\MetaYaml;
60+
use RomaricDrigon\MetaYaml\Loader\YamlLoader;
61+
use RomaricDrigon\MetaYaml\Loader\XmlLoader; // JsonLoader is already available
5862

5963
// create one loader object
6064
$loader = new JsonLoader(); // Json (will use php json_decode)
6165
$loader = new YamlLoader(); // Yaml using Symfony Yaml component
6266
$loader = new XmlLoader(); // Xml (using php SimpleXml)
6367

6468
// the usage is the same then
65-
$array = $loader->load($some_string);
69+
$array = $loader->load('SOME STRING...');
6670
// or you can load from a file
6771
$array = $loader->loadFromFile('path/to/file');
6872
```
@@ -73,45 +77,46 @@ $array = $loader->loadFromFile('path/to/file');
7377

7478
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).
7579

76-
Here's a simple example of a schema, using Yaml syntax :
80+
Here's a simple example of a schema, using Yaml syntax:
7781
```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:
8286
_type: array
8387
_required: true # optional, default false
8488
_children:
8589
rose:
8690
_required: true
8791
_type: text
88-
violette:
92+
violet:
8993
_type: text
90-
# -> only rose and violette are allowed children of fleurs
94+
# -> only rose and violet are allowed children of flowers
9195
```
9296

9397
And a valid Yaml file :
9498
```yaml
95-
fleurs:
96-
rose: une rose
97-
violette: une violette
99+
flowers:
100+
rose: "a rose"
101+
violet: "a violet flower"
98102
```
99103
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.
102106

103107
### Schema structure
104108

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).
108113

109-
So a basic schema file:
114+
A basic schema file:
110115
```yaml
111116
root:
112117
# 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'...
115120
partials:
116121
block:
117122
# here I define a partial called block
@@ -133,7 +138,7 @@ Those types are available:
133138
* `boolean`: boolean value
134139
* `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)
135140
* `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
137142
* `prototype`: define a repetition of items whose name/index is not important. You must give children's type in `_prototype` node.
138143
* `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.
139144
* `partial`: "shortcut" to a block described in `partials` root node. Provide partial name in `_partial`
@@ -142,9 +147,8 @@ You can specify additional attributes:
142147

143148
* general attributes:
144149
* `_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)
148152
* `_description`: full-text description, cf. [Documentation generator](#documentation-generator)
149153
* only for array nodes:
150154
* `_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:
157161
root:
158162
_type: array
159163
_children:
160-
texte:
164+
SomeText:
161165
_type: text
162-
_not_empty: true
163-
enume:
166+
_not_empty: true # so !== ''
167+
SomeEnum:
164168
_type: enum
165169
_values:
166170
- windows
167171
- mac
168172
- linux
169-
entier:
173+
SomeNumber:
170174
_type: number
171175
_strict: true
172-
booleen:
176+
SomeBool:
173177
_type: boolean
174-
prototype_array:
178+
SomePrototypeArray:
175179
_type: prototype
176180
_prototype:
177181
_type: array
178182
_children:
179-
texte:
183+
SomeOtherText:
180184
_type: text
181-
_is_required: true
182-
paragraph:
185+
_is_required: true # can't be null
186+
SomeParagraph:
183187
_type: partial
184-
_partial: block
185-
test_choice:
188+
_partial: aBlock # cf 'partials' below
189+
SomeChoice:
186190
_type: choice
187191
_choices:
188192
1:
@@ -192,20 +196,21 @@ root:
192196
- linux
193197
2:
194198
_type: number
195-
regex:
199+
# so our node must be either #1 or #2
200+
SomeRegex:
196201
_type: pattern
197202
_pattern: /e/
198203
partials:
199-
block:
204+
aBlock:
200205
_type: array
201206
_children:
202-
line_1:
207+
Line1:
203208
_type: text
204209
```
205210

206211
### More information
207212

208-
For more examples, look inside test/data folder.
213+
For more examples, look inside `test/data` folder.
209214
In each folder, you have an .yml file and its schema. There's also a XML example.
210215

211216
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`:
215220
Each node can have a `_description` attribute, containing some human-readable text.
216221
You can retrieve the documentation about a node (its type, description, other attributes...) like this:
217222
```php
218-
// create object, load schema from an array
219223
// it's recommended to validate the schema before reading documentation
220224
$schema = new MetaYaml($schema, true);
221225
@@ -225,7 +229,7 @@ $schema->getDocumentationForNode();
225229
// get documentation about a child node 'test' in an array 'a_test' under root
226230
$schema->getDocumentationForNode(array('a_test', 'test'));
227231
228-
// finally, if you cant to unfold (follow) all partials, set second argument to true
232+
// finally, if you want to unfold (follow) all partials, set second argument to true
229233
$schema->getDocumentationForNode(array('a_test', 'test'), true);
230234
// watch out there's no loop inside partials!
231235
```
@@ -247,7 +251,7 @@ If the targeted node is inside a choice, the result will differ slightly:
247251
array(
248252
'name' => 'test', // name of current node, from the choice key in the schema
249253
'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
251255
0 => array(
252256
'_type' => 'array' // and so on, for first choice
253257
),
@@ -259,9 +263,9 @@ array(
259263
'prefix' => '_'
260264
)
261265
```
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`)
263267

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.
265269

266270
## Notes on XML support
267271

@@ -275,7 +279,7 @@ Thus, the following conventions are enforced by the XML loader:
275279
* if a node has both attribute(s) and a text content, text content will be stored under key `_value`
276280
* 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
277281
* namespaces are not supported
278-
* empty nodes are ditched
282+
* empty nodes are skipped
279283

280284
Let's take an example:
281285
```xml
@@ -317,11 +321,11 @@ array('fleurs' =>
317321

318322
## XSD generator
319323

320-
_**Please note this feature is still experimental**_
324+
_**Please note this feature is still experimental!**_
321325

322326
MetaYaml can try to generate a [XML Schema Definition](http://en.wikipedia.org/wiki/XML_Schema) from a MetaYaml schema.
323327
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.
325329

326330
Usage example :
327331
```php
@@ -335,7 +339,6 @@ $generator = new XsdGenerator();
335339
$my_xsd_string = $generator->build($schema, true);
336340
```
337341

338-
Note this feature is still experimental.
339342
A few limitations, some relative to XML Schema, apply:
340343
* `root` node must be an `array`
341344
* 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`
354357
## Extending
355358

356359
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()`).
358361

359362
## Thanks
360363

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.
362365

363366
[Top](#metayaml)

0 commit comments

Comments
 (0)