2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* XML handling for Cake.
|
|
|
|
*
|
|
|
|
* The methods in these classes enable the datasources that use XML to work.
|
|
|
|
*
|
2010-07-26 21:31:39 -03:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 17:46:59 +11:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 14:18:20 -05:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 14:18:20 -05:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 17:00:11 +11:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
|
|
|
* @since CakePHP v .0.10.3.1400
|
2009-11-06 17:51:51 +11:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2010-07-26 21:31:39 -03:00
|
|
|
class Xml {
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-08-26 01:18:15 +00:00
|
|
|
/**
|
2010-07-26 23:42:05 -03:00
|
|
|
* Initialize SimpleXMLElement from a given XML string, file path, URL or array.
|
2008-08-26 01:18:15 +00:00
|
|
|
*
|
2010-08-23 00:01:55 -04:00
|
|
|
* ### Usage:
|
|
|
|
*
|
|
|
|
* Building XML from a string:
|
|
|
|
*
|
|
|
|
* `$xml = Xml::build('<example>text</example>');`
|
|
|
|
*
|
|
|
|
* Building XML from a file path:
|
|
|
|
*
|
|
|
|
* `$xml = Xml::build('/path/to/an/xml/file.xml');`
|
|
|
|
*
|
|
|
|
* Building from a remote URL:
|
|
|
|
*
|
|
|
|
* `$xml = Xml::build('http://example.com/example.xml');`
|
|
|
|
*
|
|
|
|
* Building from an array:
|
|
|
|
*
|
|
|
|
* {{{
|
|
|
|
* $value = array(
|
|
|
|
* 'tags' => array(
|
|
|
|
* 'tag' => array(
|
|
|
|
* array(
|
|
|
|
* 'id' => '1',
|
|
|
|
* 'name' => 'defect'
|
|
|
|
* ),
|
|
|
|
* array(
|
|
|
|
* 'id' => '2',
|
|
|
|
* 'name' => 'enhancement'
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
* $xml = Xml::build($value);
|
|
|
|
* }}}
|
|
|
|
*
|
|
|
|
* When building XML from an array ensure that there is only one top level element.
|
|
|
|
*
|
2010-07-26 23:42:05 -03:00
|
|
|
* @param mixed $input XML string, a path to a file, an URL or an array
|
2010-07-26 21:31:39 -03:00
|
|
|
* @return object SimpleXMLElement
|
2010-08-23 00:01:55 -04:00
|
|
|
* @throws Exception
|
2008-08-26 01:18:15 +00:00
|
|
|
*/
|
2010-07-26 21:31:39 -03:00
|
|
|
public static function build($input) {
|
|
|
|
if (is_array($input) || is_object($input)) {
|
|
|
|
return self::fromArray((array)$input);
|
|
|
|
} elseif (strstr($input, "<")) {
|
|
|
|
return new SimpleXMLElement($input);
|
|
|
|
} elseif (file_exists($input) || strpos($input, 'http://') === 0 || strpos($input, 'https://') === 0 ) {
|
|
|
|
return new SimpleXMLElement($input, null, true);
|
|
|
|
} elseif (!is_string($input)) {
|
|
|
|
throw new Exception(__('Invalid input.'));
|
2008-08-26 01:18:15 +00:00
|
|
|
}
|
2010-07-26 21:31:39 -03:00
|
|
|
throw new Exception(__('XML cannot be read.'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2010-07-26 23:42:05 -03:00
|
|
|
/**
|
2010-08-23 00:01:55 -04:00
|
|
|
* Transform an array into a SimpleXMLElement
|
|
|
|
*
|
|
|
|
* Using the following data:
|
|
|
|
*
|
|
|
|
* {{{
|
|
|
|
* $value = array(
|
|
|
|
* 'root' => array(
|
|
|
|
* 'tag' => array(
|
|
|
|
* 'id' => 1,
|
2010-09-07 18:12:35 -03:00
|
|
|
* 'value' => 'defect',
|
|
|
|
* '@' => 'description'
|
2010-08-23 00:01:55 -04:00
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
* }}}
|
|
|
|
*
|
|
|
|
* Calling `Xml::fromArray($value, 'tags');` Will generate:
|
|
|
|
*
|
2010-09-07 18:12:35 -03:00
|
|
|
* `<root><tag><id>1</id><value>defect</value>description</tag></root>`
|
2010-08-23 00:01:55 -04:00
|
|
|
*
|
|
|
|
* And calling `Xml::fromArray($value, 'attribute');` Will generate:
|
|
|
|
*
|
2010-09-07 18:12:35 -03:00
|
|
|
* `<root><tag id="1" value="defect">description</tag></root>`
|
2010-07-26 23:42:05 -03:00
|
|
|
*
|
|
|
|
* @param array $input Array with data
|
2010-08-23 00:01:55 -04:00
|
|
|
* @param string $format If create childs ('tags') or attributes ('attribute').
|
2010-07-26 23:42:05 -03:00
|
|
|
* @return object SimpleXMLElement
|
|
|
|
*/
|
2010-09-06 23:11:45 -03:00
|
|
|
public static function fromArray($input, $format = 'tags') {
|
2010-07-26 21:31:39 -03:00
|
|
|
if (!is_array($input) || count($input) !== 1) {
|
|
|
|
throw new Exception(__('Invalid input.'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-07-26 21:31:39 -03:00
|
|
|
$key = key($input);
|
|
|
|
if (is_integer($key)) {
|
|
|
|
throw new Exception(__('The key of input must be alphanumeric'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-09-08 19:18:58 -03:00
|
|
|
$dom = new DOMDocument('1.0');
|
|
|
|
self::_fromArray($dom, $dom, $input, $format);
|
|
|
|
return new SimpleXMLElement($dom->saveXML());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2010-07-26 23:42:05 -03:00
|
|
|
/**
|
2010-09-08 19:18:58 -03:00
|
|
|
* Recursive method to create childs from array
|
2010-07-26 23:42:05 -03:00
|
|
|
*
|
2010-09-08 19:18:58 -03:00
|
|
|
* @param object $dom Handler to DOMDocument
|
|
|
|
* @param object $node Handler to DOMElement (child)
|
|
|
|
* @param array $data Array of data to append to the $node.
|
2010-08-23 00:01:55 -04:00
|
|
|
* @param string $format Either 'attribute' or 'tags'. This determines where nested keys go.
|
2010-07-26 23:42:05 -03:00
|
|
|
* @return void
|
|
|
|
*/
|
2010-09-08 19:18:58 -03:00
|
|
|
protected function _fromArray(&$dom, &$node, &$data, $format) {
|
|
|
|
if (empty($data) || !is_array($data)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-09-08 19:18:58 -03:00
|
|
|
foreach ($data as $key => $value) {
|
2010-07-26 21:31:39 -03:00
|
|
|
if (is_string($key)) {
|
|
|
|
if (!is_array($value)) {
|
|
|
|
if (is_bool($value)) {
|
|
|
|
$value = (int)$value;
|
|
|
|
} elseif ($value === null) {
|
|
|
|
$value = '';
|
|
|
|
}
|
2010-08-23 00:48:34 -03:00
|
|
|
if ($key[0] !== '@' && $format === 'tags') {
|
2010-09-08 19:18:58 -03:00
|
|
|
$child = $dom->createElement($key, $value);
|
|
|
|
$node->appendChild($child);
|
2010-07-26 21:31:39 -03:00
|
|
|
} else {
|
2010-08-23 00:48:34 -03:00
|
|
|
if ($key[0] === '@') {
|
|
|
|
$key = substr($key, 1);
|
|
|
|
}
|
2010-09-08 19:18:58 -03:00
|
|
|
$attribute = $dom->createAttribute($key);
|
|
|
|
$attribute->appendChild($dom->createTextNode($value));
|
|
|
|
$node->appendChild($attribute);
|
2008-06-03 18:41:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-08-23 00:48:34 -03:00
|
|
|
if ($key[0] === '@') {
|
|
|
|
throw new Exception(__('Invalid array'));
|
|
|
|
}
|
2010-07-26 21:31:39 -03:00
|
|
|
if (array_keys($value) === range(0, count($value) - 1)) { // List
|
|
|
|
foreach ($value as $item) {
|
2010-08-23 01:33:06 -03:00
|
|
|
if (array_key_exists('@', $item)) {
|
2010-09-08 19:18:58 -03:00
|
|
|
$child = $dom->createElement($key, (string)$item['@']);
|
2010-08-23 01:33:06 -03:00
|
|
|
unset($item['@']);
|
|
|
|
} else {
|
2010-09-08 19:18:58 -03:00
|
|
|
$child = $dom->createElement($key);
|
2010-08-23 01:33:06 -03:00
|
|
|
}
|
2010-09-08 19:18:58 -03:00
|
|
|
self::_fromArray($dom, $child, $item, $format);
|
|
|
|
$node->appendChild($child);
|
2010-07-26 21:31:39 -03:00
|
|
|
}
|
|
|
|
} else { // Struct
|
2010-08-23 01:33:06 -03:00
|
|
|
if (array_key_exists('@', $value)) {
|
2010-09-08 19:18:58 -03:00
|
|
|
$child = $dom->createElement($key, (string)$value['@']);
|
2010-08-23 01:33:06 -03:00
|
|
|
unset($value['@']);
|
|
|
|
} else {
|
2010-09-08 19:18:58 -03:00
|
|
|
$child = $dom->createElement($key);
|
2010-08-23 01:33:06 -03:00
|
|
|
}
|
2010-09-08 19:18:58 -03:00
|
|
|
self::_fromArray($dom, $child, $value, $format);
|
|
|
|
$node->appendChild($child);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2010-07-26 21:31:39 -03:00
|
|
|
throw new Exception(__('Invalid array'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-07-26 21:31:39 -03:00
|
|
|
* Returns this XML structure as a array.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-07-26 23:42:05 -03:00
|
|
|
* @param object $simpleXML SimpleXMLElement instance
|
2010-07-26 21:31:39 -03:00
|
|
|
* @return array Array representation of the XML structure.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-26 21:31:39 -03:00
|
|
|
public static function toArray($simpleXML) {
|
|
|
|
if (!($simpleXML instanceof SimpleXMLElement)) {
|
|
|
|
throw new Exception(__('The input is not instance of SimpleXMLElement.'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-07-26 21:31:39 -03:00
|
|
|
$result = array();
|
2010-07-28 19:46:35 -03:00
|
|
|
$namespaces = array_merge(array('' => ''), $simpleXML->getNamespaces(true));
|
2010-09-07 00:31:50 -03:00
|
|
|
self::_toArray($simpleXML, $result, '', array_keys($namespaces));
|
2010-07-26 21:31:39 -03:00
|
|
|
return $result;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2010-07-26 23:42:05 -03:00
|
|
|
/**
|
|
|
|
* Recursive method to toArray
|
|
|
|
*
|
|
|
|
* @param object $xml SimpleXMLElement object
|
|
|
|
* @param array $parentData Parent array with data
|
2010-09-07 00:31:50 -03:00
|
|
|
* @param string $ns Namespace of current child
|
2010-07-28 19:46:35 -03:00
|
|
|
* @param array $namespaces List of namespaces in XML
|
2010-07-26 23:42:05 -03:00
|
|
|
* @return void
|
|
|
|
*/
|
2010-09-07 00:31:50 -03:00
|
|
|
protected static function _toArray($xml, &$parentData, $ns, $namespaces) {
|
2010-07-26 21:31:39 -03:00
|
|
|
$data = array();
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2010-07-28 19:46:35 -03:00
|
|
|
foreach ($namespaces as $namespace) {
|
|
|
|
foreach ($xml->attributes($namespace, true) as $key => $value) {
|
2010-09-07 00:31:50 -03:00
|
|
|
if (!empty($namespace)) {
|
|
|
|
$key = $namespace . ':' . $key;
|
|
|
|
}
|
2010-08-23 00:24:56 -03:00
|
|
|
$data['@' . $key] = (string)$value;
|
2010-07-28 19:46:35 -03:00
|
|
|
}
|
2008-06-21 15:23:58 +00:00
|
|
|
|
2010-07-28 19:46:35 -03:00
|
|
|
foreach ($xml->children($namespace, true) as $child) {
|
2010-09-07 00:31:50 -03:00
|
|
|
self::_toArray($child, $data, $namespace, $namespaces);
|
2010-07-28 19:46:35 -03:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-07-28 18:53:36 -03:00
|
|
|
$asString = trim((string)$xml);
|
2010-07-26 21:31:39 -03:00
|
|
|
if (empty($data)) {
|
2010-07-28 18:53:36 -03:00
|
|
|
$data = $asString;
|
|
|
|
} elseif (!empty($asString)) {
|
2010-08-23 01:33:06 -03:00
|
|
|
$data['@'] = $asString;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-11-26 18:21:21 +00:00
|
|
|
|
2010-09-07 00:31:50 -03:00
|
|
|
if (!empty($ns)) {
|
|
|
|
$ns .= ':';
|
|
|
|
}
|
|
|
|
$name = $ns . $xml->getName();
|
2010-07-26 21:31:39 -03:00
|
|
|
if (isset($parentData[$name])) {
|
|
|
|
if (!is_array($parentData[$name]) || !isset($parentData[$name][0])) {
|
|
|
|
$parentData[$name] = array($parentData[$name]);
|
2008-08-10 16:26:43 +00:00
|
|
|
}
|
2010-07-26 21:31:39 -03:00
|
|
|
$parentData[$name][] = $data;
|
|
|
|
} else {
|
|
|
|
$parentData[$name] = $data;
|
2008-08-10 16:26:43 +00:00
|
|
|
}
|
2008-08-26 01:18:15 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2010-07-26 21:31:39 -03:00
|
|
|
}
|