Skip to content

Commit

Permalink
Introduces the '->times()' method.
Browse files Browse the repository at this point in the history
* [+] ->times() is part of the family.
  • Loading branch information
daniele-orlando committed Jan 8, 2016
1 parent dfb84f4 commit ae8dd7c
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 144 deletions.
8 changes: 7 additions & 1 deletion documents/Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
[+]: new [~]: changed [-]: removed [#]: fixed [@]: internal


1.11: (2016-01-07)
1.12: (2016-01-08)
introduces the '->times()' method.

* [+] ->times() is part of the family.


1.11:
supports the special syntax '@<attribute>' and '@' for setting attributes and
text content when adding elements using an array.

Expand Down
150 changes: 71 additions & 79 deletions documents/Getting-Started.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,68 +393,35 @@ flexibility.
> ```
## Exporting The Document
## Iterating
The document can be exported as `DOMDocument`.
Iterations can be performed without interrupting the fluid flow.
```php
$dom = $book->dom();
```
Or as XML string.
`->each()` iterates the results of a query or of an insertion method.
```php
$xml = $book->xml();
```

```xml
<?xml version="1.0" encoding="UTF-8"?>
<book>
<title>The Theory Of Everything</title>
<author>S. Hawking</author>
<description/>
<chapters>
<chapter>Ideas About The Universe</chapter>
<chapter>The Expanding Universe</chapter>
</chapters>
<cover>
<h1>The Theory Of Everything</h1>
<img src="http://goo.gl/kO3Iov"/>
</cover>
</book>
```

The XML declaration can be removed from the output string too.
```php
$xml = $book->xml(true);
```
$book->query('//chapter')
->each(function($chapter, $domnode, $index) {
$chapter->attr('id', $index + 1);
```xml
<book>
<title>The Theory Of Everything</title>
<author>S. Hawking</author>
<description/>
<chapters>
<chapter>Ideas About The Universe</chapter>
<chapter>The Expanding Universe</chapter>
</chapters>
<cover>
<h1>The Theory Of Everything</h1>
<img src="http://goo.gl/kO3Iov"/>
</cover>
</book>
echo $domnode->nodeValue;
});
```
Not only the entire document but even only specific nodes (with their content)<br/>
can be exported.
`->times($n)` repeats the following method call `$n` times. If a callable is<br/>
passed as second argument, the callable is called `$n` times.

```php
$xml = $book->query('//chapter')->xml();
$book->query('//chapters')
->times(2)
->add('chapter');
```

```xml
<chapter>Ideas About The Universe</chapter>
<chapter>The Expanding Universe</chapter>
```php
$book->query('//chapters')
->times(2, function($chapters, $index) {
$chapters->add('chapter', [ 'id' => $index + 5 ]);
});
```


Expand All @@ -465,15 +432,6 @@ what is already convenient to use.

To access the node content after a query we can use the DOMNode own methods.

```php
$book->query('//chapter')
->each(function($fluid, $domnode, $index) {
$fluid->attr('uuid', random_uuid());

echo $domnode->nodeValue;
});
```

Accessing the query result as array or iterating it returns the DOMNode unwrapped.

```php
Expand Down Expand Up @@ -553,19 +511,8 @@ echo $book->xml();
```xml
<?xml version="1.0" encoding="UTF-8"?>
<book type="science">
<title lang="en">The Theory Of Everything</title>
<author>S. Hawking</author>
<chapters lang="en">
<chapter id="123" first="" lang="en">Ideas About The Universe</chapter>
<chapter id="321" lang="en">The Expanding Universe</chapter>
<chapter id="432" lang="en">Black Holes</chapter>
<chapter id="234" lang="en" last="">Black Holes Ain't So Black</chapter>
</chapters>
<cover>
<h1>The Theory Of Everything</h1>
<img src="http://goo.gl/kO3Iov"/>
</cover>
<book>
...
<xhtml:h1 xmlns:xhtml="http://www.w3.org/1999/xhtml">
<svg:shape xmlns:svg="http://www.w3.org/2000/svg"/>
</xhtml:h1>
Expand Down Expand Up @@ -612,7 +559,7 @@ Which is the same of
$food->query('//egg')->remove(); // Removes all the eggs.
```

Quering and removing with relative XPath can be used too.
Querying and removing with relative XPath can be done too.

```php
$food->query('/doc')->remove('egg'); // Removes all the eggs.
Expand All @@ -626,6 +573,52 @@ $food->query('/doc')->remove('egg'); // Removes all the eggs.
> ```
## Exporting The Document
The document can be exported as `DOMDocument`.
```php
$dom = $book->dom();
```
Or as XML string.

```php
$xml = $book->xml();
```

```xml
<?xml version="1.0" encoding="UTF-8"?>
<book>
...
</book>
```

The XML declaration can be removed from the output string too.

```php
$xml = $book->xml(true);
```

```xml
<book>
...
</book>
```

Not only the entire document but even only specific nodes (with their content)<br/>
can be exported.

```php
$xml = $book->query('//chapter')->xml();
```

```xml
<chapter lang="en" first="">Ideas About The Universe</chapter>
<chapter lang="en" last="">The Expanding Universe</chapter>
```


## Importing Existing Documents

FluidXML provides an easy way to import existing XML documents from a variety of formats.
Expand Down Expand Up @@ -700,13 +693,12 @@ $doc->add($dom) // A DOMDocument/DOMNode/DOMNodeList instanc
->add($fluidxml->query('//p')); // A FluidXml query instance.
```
Crazy things are possible too and I will not stop you from doing them.
Crazy things are possible too using arrays and I will not stop you from doing them.

```php
$doc->add([ 'aNode',
'domDoc' => $dom,
'file' => fluidify('path/to/file.xml'),
'simple' => $simplexml ]);
$doc->add([ 'dom' => $dom,
'simple' => $simplexml,
'file' => fluidify('path/to/file.xml') ]);
```


Expand Down
Loading

0 comments on commit ae8dd7c

Please sign in to comment.