Doctrine 1.2.4
Doctrine_Parser_Xml Class Reference

Inherits Doctrine_Parser.

Public Member Functions

 doDump ($data, $path=null)
 
 doLoad ($path)
 
 dumpData ($array, $path=null, $charset=null)
 
 loadData ($path)
 
 prepareData ($simpleXml)
 

Static Public Member Functions

static arrayToXml ($array, $rootNodeName= 'data', $xml=null, $charset=null)
 
static dump ($array, $type= 'xml', $path=null, $charset=null)
 
static getParser ($type)
 
static load ($path, $type= 'xml')
 

Detailed Description

Definition at line 33 of file Xml.php.

Member Function Documentation

static Doctrine_Parser_Xml::arrayToXml (   $array,
  $rootNodeName = 'data',
  $xml = null,
  $charset = null 
)
static

arrayToXml

Parameters
string$arrayArray to convert to xml
string$rootNodeNameName of the root node
string$xmlSimpleXmlElement
Returns
string $asXml String of xml built from array

Definition at line 61 of file Xml.php.

{
if ($xml === null) {
$xml = new SimpleXmlElement("<?xml version=\"1.0\" encoding=\"utf-8\"?><$rootNodeName/>");
}
foreach($array as $key => $value)
{
$key = preg_replace('/[^a-z]/i', '', $key);
if (is_array($value) && ! empty($value)) {
$node = $xml->addChild($key);
foreach ($value as $k => $v) {
if (is_numeric($v)) {
unset($value[$k]);
$node->addAttribute($k, $v);
}
}
self::arrayToXml($value, $rootNodeName, $node, $charset);
} else if (is_int($key)) {
$xml->addChild($value, 'true');
} else {
$charset = $charset ? $charset : 'utf-8';
if (strcasecmp($charset, 'utf-8') !== 0 && strcasecmp($charset, 'utf8') !== 0) {
$value = iconv($charset, 'UTF-8', $value);
}
$value = htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
$xml->addChild($key, $value);
}
}
return $xml->asXML();
}
Doctrine_Parser::doDump (   $data,
  $path = null 
)
inherited

doDump

Parameters
string$data
string$path
Returns
void

Definition at line 145 of file Parser.php.

{
if ($path !== null) {
return file_put_contents($path, $data);
} else {
return $data;
}
}
Doctrine_Parser::doLoad (   $path)
inherited

doLoad

Get contents whether it is the path to a file file or a string of txt. Either should allow php code in it.

Parameters
string$path
Returns
void

Definition at line 120 of file Parser.php.

{
ob_start();
if ( ! file_exists($path)) {
$contents = $path;
$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'dparser_' . microtime();
file_put_contents($path, $contents);
}
include($path);
// Fix #1569. Need to check if it's still all valid
$contents = ob_get_clean(); //iconv("UTF-8", "UTF-8", ob_get_clean());
return $contents;
}
static Doctrine_Parser::dump (   $array,
  $type = 'xml',
  $path = null,
  $charset = null 
)
staticinherited

dump

Interface for pulling and dumping data to a file

Parameters
string$array
string$path
string$type
string$charsetThe charset of the data being dumped
Returns
void
Author
Jonathan H. Wage

Definition at line 104 of file Parser.php.

{
$parser = self::getParser($type);
return $parser->dumpData($array, $path, $charset);
}
Doctrine_Parser_Xml::dumpData (   $array,
  $path = null,
  $charset = null 
)

dumpData

Convert array to xml and dump to specified path or return the xml

Parameters
string$arrayArray of data to convert to xml
string$pathPath to write xml data to
string$charsetThe charset of the data being dumped
Returns
string $xml
void

Definition at line 46 of file Xml.php.

{
$data = self::arrayToXml($array, 'data', null, $charset);
return $this->doDump($data, $path);
}
static Doctrine_Parser::getParser (   $type)
staticinherited

getParser

Get instance of the specified parser

Parameters
string$type
Returns
void
Author
Jonathan H. Wage

Definition at line 68 of file Parser.php.

{
$class = 'Doctrine_Parser_'.ucfirst($type);
return new $class;
}
static Doctrine_Parser::load (   $path,
  $type = 'xml' 
)
staticinherited

load

Interface for loading and parsing data from a file

Parameters
string$path
string$type
Returns
void
Author
Jonathan H. Wage

Definition at line 85 of file Parser.php.

{
$parser = self::getParser($type);
return (array) $parser->loadData($path);
}
Doctrine_Parser_Xml::loadData (   $path)

loadData

Load xml file and return array of data

Parameters
string$pathPath to load xml data from
Returns
array $array Array of data converted from xml

Definition at line 105 of file Xml.php.

{
$contents = $this->doLoad($path);
$simpleXml = simplexml_load_string($contents);
return $this->prepareData($simpleXml);
}
Doctrine_Parser_Xml::prepareData (   $simpleXml)

prepareData

Prepare simple xml to array for return

Parameters
string$simpleXml
Returns
array $return

Definition at line 122 of file Xml.php.

{
if ($simpleXml instanceof SimpleXMLElement) {
$children = $simpleXml->children();
$return = null;
}
foreach ($children as $element => $value) {
if ($value instanceof SimpleXMLElement) {
$values = (array) $value->children();
if (count($values) > 0) {
$return[$element] = $this->prepareData($value);
} else {
if ( ! isset($return[$element])) {
$return[$element] = (string) $value;
} else {
if ( ! is_array($return[$element])) {
$return[$element] = array($return[$element], (string) $value);
} else {
$return[$element][] = (string) $value;
}
}
}
}
}
if (is_array($return)) {
return $return;
} else {
return array();
}
}

The documentation for this class was generated from the following file: