-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 806088a
Showing
5 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Fawno | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# SimpleXMLExtended | ||
|
||
[![GitHub license](https://img.shields.io/github/license/fawno/SimpleXMLExtended)](https://github.com/fawno/SimpleXMLExtended/blob/master/LICENSE) | ||
[![GitHub release](https://img.shields.io/github/release/fawno/SimpleXMLExtended)](https://github.com/fawno/SimpleXMLExtended/releases) | ||
[![Packagist](https://img.shields.io/packagist/v/fawno/simple-xml-extended)](https://packagist.org/packages/fawno/simple-xml-extended) | ||
|
||
SimpleXMLElement Extended class | ||
SimpleXMLExtended add a new method for create CData nodes. | ||
Also added a new method for output e nice format XML. | ||
|
||
## Requirements | ||
- libxml PHP extension (enabled by default). | ||
- SimpleXML PHP extension (enabled by default). | ||
- DOM PHP extension (enabled by default). | ||
|
||
## Installation | ||
|
||
You can install this plugin into your application using | ||
[composer](https://getcomposer.org): | ||
|
||
``` | ||
composer require fawno/simple-xml-extended | ||
``` | ||
|
||
or, clone/download this repo, and include src/SimpleXMLExtended.php in your project. | ||
|
||
## Usage | ||
|
||
```php | ||
use Fawno\SimpleXMLExtended\SimpleXMLExtended; | ||
|
||
// Get a SimpleXMLExtended object from a DOM node | ||
$xml = simplexml_import_dom($dom, SimpleXMLExtended::class); | ||
|
||
// Interprets an XML file into an SimpleXMLExtended object | ||
$xml = simplexml_load_file($xml_file, SimpleXMLExtended::class); | ||
|
||
// Interprets a string of XML into an SimpleXMLExtended object | ||
$xml = simplexml_load_string($xml_string, SimpleXMLExtended::class); | ||
|
||
// Creates a new SimpleXMLExtended object | ||
$xml = new SimpleXMLExtended($xml_string); | ||
|
||
// Adds a child element to the XML node as cdata | ||
$xml->addChildCData('node_cdata', 'data as cdata'); | ||
|
||
// Return a well-formed and nice formated XML string based on SimpleXMLExtended element | ||
$xml->formatXML() | ||
``` | ||
|
||
## Example | ||
|
||
```php | ||
require 'src/SimpleXMLExtended.php'; | ||
|
||
use Fawno\SimpleXMLExtended\SimpleXMLExtended; | ||
|
||
$root = new SimpleXMLExtended('<?xml version="1.0" encoding="UTF-8"?><root/>'); | ||
|
||
$root->addChildCData('node_cdata', 'data as cdata'); | ||
|
||
print_r($root->asXML()); | ||
/* | ||
Output: | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<root><node_cdata><![CDATA[data as cdata]]></node_cdata></root> | ||
*/ | ||
|
||
print_r($root->formatXML()); | ||
/* | ||
Output: | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<root> | ||
<node_cdata><![CDATA[data as cdata]]></node_cdata> | ||
</root> | ||
*/ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "fawno/simple-xml-extended", | ||
"version": "1.0.0", | ||
"description": "SimpleXMLElement Extended class", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Fawno", | ||
"homepage": "https://github.com/fawno/SimpleXMLExtended/graphs/contributors" | ||
} | ||
], | ||
"support": { | ||
"issues": "https://github.com/fawno/SimpleXMLExtended/issues", | ||
"source": "https://github.com/fawno/SimpleXMLExtended" | ||
}, | ||
"require": { | ||
"php": ">=5.6.0", | ||
"ext-dom": "*", | ||
"ext-simplexml": "*", | ||
"ext-libxml": "*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Fawno\\SimpleXMLExtended\\": "src" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
namespace Fawno\SimpleXMLExtended; | ||
|
||
class SimpleXMLExtended extends \SimpleXMLElement { | ||
private function addCData (string $cdata) { | ||
$node = dom_import_simplexml($this); | ||
$node->appendChild($node->ownerDocument->createCDATASection($cdata)); | ||
} | ||
|
||
public function addChildCData (string $name, string $cdata = null, string $namespace = null) { | ||
$child = $this->addChild($name, null, $namespace); | ||
|
||
if (!empty($cdata)) { | ||
$child->addCData($cdata); | ||
} | ||
|
||
return $child; | ||
} | ||
|
||
public function formatXML (string $filename = null) { | ||
$xmlDocument = new \DOMDocument('1.0'); | ||
$xmlDocument->preserveWhiteSpace = false; | ||
$xmlDocument->formatOutput = true; | ||
$xmlDocument->loadXML($this->asXML()); | ||
|
||
if (empty($filename)) { | ||
return $xmlDocument->saveXML(); | ||
} else { | ||
return $xmlDocument->save($filename); | ||
} | ||
} | ||
} |