Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alphp committed Dec 15, 2019
0 parents commit 806088a
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
21 changes: 21 additions & 0 deletions LICENSE
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.
77 changes: 77 additions & 0 deletions README.md
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>
*/
```
27 changes: 27 additions & 0 deletions composer.json
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"
}
}
}
32 changes: 32 additions & 0 deletions src/SimpleXMLExtended.php
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);
}
}
}

0 comments on commit 806088a

Please sign in to comment.