Skip to content

Commit 5481f77

Browse files
committed
Reader docs
1 parent f3c4036 commit 5481f77

File tree

9 files changed

+226
-20
lines changed

9 files changed

+226
-20
lines changed

CONTRIBUTING.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Contributing
2+
3+
VeeWee/XML is an open source, community-driven project. If you'd like to contribute,
4+
feel free to do this, but remember to follow this few simple rules:
5+
6+
## Branching strategy
7+
8+
- __Always__ base your changes on the `master` branch (all new development happens here),
9+
- When you create Pull Request, always select `master` branch as target, otherwise it
10+
will be closed (this is selected by default).
11+
12+
## Coverage
13+
14+
- All classes and functions that are added to the project .should be covered by Tests.
15+
- All classes and functions need to be documented
16+
17+
## Code style / Formatting
18+
19+
- All code in the `src` folder must follow the PSR-2 standard that is enforced by code styling tools.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2021-MAX_INT VeeWee
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,40 @@
1-
# WIP - currently just a playground
1+
# <XML />
22

3-
![Here be dragons](https://media.giphy.com/media/Zb0asRm15HqCbgShD4/giphy.gif)
3+
*XML without worries*
4+
5+
This package aims to provide all tools for dealing with XML in PHP without worries.
6+
You will find a type-safe, declarative API that deals with errors for you!
7+
8+
9+
## Installation
10+
11+
```
12+
composer require veewee/xml
13+
```
14+
15+
## Components
16+
17+
* [DOM](docs/dom.md): Operate on XML documents through the DOM API
18+
* [Reader](docs/reader.md): Memory-safe XML reader.
19+
* [XSLT](docs/xslt.md): Transform XML documents into something else.
20+
21+
## Roadmap
22+
23+
These components are not implemented yet, but have been thought about.
24+
Stay tuned if you want to use these!
25+
26+
* Advanced XSLT setup (XSLT2, XSLT3 ?)
27+
* Writer
28+
* SimpleXML
29+
* Saxon/C?
30+
31+
## About
32+
33+
### Submitting bugs and feature requests
34+
35+
Bugs and feature request are tracked on [GitHub](https://github.com/veewee/xml/issues).
36+
Please take a look at our rules before [contributing your code](CONTRIBUTING.md).
37+
38+
### License
39+
40+
veewee/xml is licensed under the [MIT License](LICENSE).

docs/dom.md

Whitespace-only changes.

docs/reader.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Reader Component
2+
3+
The Reader component can be used to detect patterns in a memory-safe way inside a big XML document.
4+
It uses matchers to determine if you care about specific elements or not.
5+
As a result, the reader provides a generator of XML strings that match your matchers!
6+
7+
## Example
8+
9+
```php
10+
use VeeWee\Xml\Dom\Document;
11+
use VeeWee\Xml\Reader\Reader;
12+
use VeeWee\Xml\Reader\Matcher;
13+
14+
$reader = Reader::fromXmlFile('large-data.xml');
15+
$provider = $reader->provide(
16+
Matcher\all(
17+
Matcher\node_name('item'),
18+
Matcher\node_attribute('locale', 'nl-BE')
19+
)
20+
);
21+
22+
foreach ($provider as $nlItem) {
23+
$dom = Document::fromXmlString($nlItem);
24+
// Do something with it
25+
}
26+
```
27+
28+
## Reader
29+
30+
The Reader consists out of following composable blocks:
31+
32+
- [Configurators](#configurators)
33+
- [Loaders](#loaders)
34+
- [Matchers](#matchers)
35+
36+
### Configurators
37+
38+
You can configure how a reader behaves based on configurators.
39+
This package provides following configurators:
40+
41+
* TODO : currently no configurators are added...
42+
43+
### Loaders
44+
45+
#### xml_file_loader
46+
47+
```php
48+
use VeeWee\Xml\Reader\Reader;
49+
50+
$reader = Reader::fromXmlFile('some-file.xml', ...$configurators);
51+
```
52+
53+
#### xml_string_loader
54+
55+
```php
56+
use VeeWee\Xml\Reader\Reader;
57+
58+
$reader = Reader::fromXmlString('<xml />', ...$configurators);
59+
```
60+
61+
#### Writing your own loader
62+
63+
A loader can be any callable that is able to create an `XMLReader`;
64+
65+
```php
66+
namespace VeeWee\Xml\Reader\Loader;
67+
68+
use XMLReader;
69+
70+
interface Loader
71+
{
72+
public function __invoke(): XMLReader;
73+
}
74+
```
75+
76+
### Matchers
77+
78+
#### all
79+
80+
All provided matchers need to match in order for this matcher to succceed:
81+
82+
```php
83+
Matcher\all(
84+
Matcher\node_name('item'),
85+
Matcher\node_attribute('locale', 'nl-BE')
86+
);
87+
```
88+
89+
#### node_attribute
90+
91+
Matches current element on attribute `locale="nl-BE"`.
92+
93+
```php
94+
Matcher\node_attribute('locale', 'nl-BE');
95+
```
96+
97+
#### node_name
98+
99+
Matches current element on node name `<item />`.
100+
101+
```php
102+
Matcher\node_name('item');
103+
```
104+
105+
#### Writing your own matcher
106+
107+
A matcher can be any callable that takes a `NodeSequence` as input and returns a `bool` that specifies if it matches or not:
108+
109+
```php
110+
namespace VeeWee\Xml\Reader\Matcher;
111+
112+
use VeeWee\Xml\Reader\Node\NodeSequence;
113+
114+
interface Matcher
115+
{
116+
publict function __invoke(NodeSequence $sequence): bool;
117+
}
118+
```
119+
120+
The `NodeSequence` class can be seen as the breadcrumbs of current XML.
121+
It points to current element and all of its attributes.
122+
You can select the current element, its parent or a complete sequence of elements until the current one in order to match.
123+

docs/xslt.md

Whitespace-only changes.

examples/reader/provider.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/Xml/Reader/Loader/Loader.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace VeeWee\Xml\Reader\Loader;
6+
7+
use XMLReader;
8+
9+
interface Loader
10+
{
11+
public function __invoke(): XMLReader;
12+
}

src/Xml/Reader/Matcher/Matcher.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace VeeWee\Xml\Reader\Matcher;
6+
7+
use VeeWee\Xml\Reader\Node\NodeSequence;
8+
9+
interface Matcher
10+
{
11+
public function __invoke(NodeSequence $nodeSequence): bool;
12+
}

0 commit comments

Comments
 (0)