Skip to content
This repository has been archived by the owner on Jun 9, 2023. It is now read-only.

Commit

Permalink
Updated the xmlAttributesToArray method in MachineAbstract.php to be …
Browse files Browse the repository at this point in the history
…recursive.
  • Loading branch information
nickbart committed Jun 12, 2013
1 parent 9a31fb3 commit 6f041e4
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions Machine/MachineAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author <[email protected]> Nick Bartkowiak
* @copyright (c) 2013 Nick Bartkowiak
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3)
* @version 0.0.2
* @version 0.0.2.5
*
* This file is part of php-plex.
*
Expand All @@ -32,7 +32,7 @@
* @author <[email protected]> Nick Bartkowiak
* @copyright (c) 2012 Nick Bartkowiak
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public Licence (GPLv3)
* @version 0.0.1
* @version 0.0.2.5
*/
abstract class Plex_MachineAbstract implements Plex_MachineInterface
{
Expand Down Expand Up @@ -81,25 +81,48 @@ protected function getBaseUrl()
*
* @param SimpleXMLElement $xmlNodes An XML node to have its attributes
* converted to a useful PHP array.
* @param integer $pass The number of recursive levels down the method has
* run. This is mainly used for determining if we are on our first pass or
* not because the data is picked up slightly differently on the first pass.
*
* @todo Make this recursive.
* @uses Plex_MachineAbstract::xmlAttributesToArray()
*
* @return array An associated array of XML attributes.
*/
protected function xmlAttributesToArray($xml)
protected function xmlAttributesToArray($xml, $pass = 0)
{
if (!$xml) return false;

$array = array();
$i= 0;
foreach($xml as $node) {
foreach($node->attributes() as $key => $value) {

// The first level of attributes are attributes about the request. To
// date I haven't found an immediate need for them, so on pass 0 we just
// ignore those attributes and move straight on to the child elements.
if ($pass > 0) {
foreach($xml->attributes() as $key => $value) {
// For abstraction, everything is casted to string. It is the
// responsibility of the calling method to handle typing.
$array[$i][$key] = (string) $value[0];
$array[$key] = (string) $value[0];
}
$i++;
}

foreach($xml->children() as $element => $child) {
if ($pass > 0) {
// If we are on our second pass then we start to cvare about the
// name of the elements. In this case we index them using it so
// we can get proper recursion.
$array[$element][] = $this->xmlAttributesToArray(
$child,
($pass+1)
);
} else {
// On our first pass, we don' care about the name of the
// element. We only care about the attributes of each individual
// member of the element, so we just send it right through.
$array[] = $this->xmlAttributesToArray($child, ($pass+1));
}
}

return $array;
}

Expand Down

0 comments on commit 6f041e4

Please sign in to comment.