Xml.php 4.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php
/*
 *  $Id: Xml.php 1080 2007-02-10 18:17:08Z jwage $
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.phpdoctrine.org>.
20
 */
21

22 23 24 25
/**
 * Doctrine_Parser_Xml
 *
 * @package     Doctrine
26
 * @subpackage  Parser
27
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28
 * @link        www.phpdoctrine.org
29 30
 * @since       1.0
 * @version     $Revision: 1080 $
31
 * @author      Jonathan H. Wage <jwage@mac.com>
32 33 34
 */
class Doctrine_Parser_Xml extends Doctrine_Parser
{
35 36 37 38 39 40 41 42 43 44 45 46 47 48
    /**
     * dumpData
     * 
     * Convert array to xml and dump to specified path or return the xml
     *
     * @param  string $array Array of data to convert to xml
     * @param  string $path  Path to write xml data to
     * @return string $xml
     * @return void
     */
    public function dumpData($array, $path = null)
    {
        $data = $this->arrayToXml($array);
        
meus's avatar
meus committed
49
        return $this->doDump($data, $path);
50 51 52 53 54 55 56 57 58 59 60
    }

    /**
     * arrayToXml
     *
     * @param  string $array        Array to convert to xml    
     * @param  string $rootNodeName Name of the root node
     * @param  string $xml          SimpleXmlElement
     * @return string $asXml        String of xml built from array
     */
    public function arrayToXml($array, $rootNodeName = 'data', $xml = null)
61
    {
Jonathan.Wage's avatar
Jonathan.Wage committed
62 63
        if ($xml === null) {
            $xml = new SimpleXmlElement("<?xml version=\"1.0\" encoding=\"utf-8\"?><$rootNodeName/>");
pookey's avatar
pookey committed
64
        }
65

66
        foreach($array as $key => $value)
Jonathan.Wage's avatar
Jonathan.Wage committed
67 68 69 70 71 72 73 74 75 76 77
        {
            if (is_array($value)) {
                $node = $xml->addChild($key);

                $this->arrayToXml($value, $rootNodeName, $node);
            } else {
                $value = htmlentities($value);

                $xml->addChild($key, $value);
            }
        }
pookey's avatar
pookey committed
78 79
      
      return $xml->asXML();
80
    }
81 82 83 84 85 86 87 88 89

    /**
     * loadData
     *
     * Load xml file and return array of data
     *
     * @param  string $path  Path to load xml data from
     * @return array  $array Array of data converted from xml
     */
90 91
    public function loadData($path)
    {
92
        $contents = $this->doLoad($path);
93
        
94
        $simpleXml = simplexml_load_string($contents);
95
        
96 97
        return $this->prepareData($simpleXml);
    }
98 99 100 101 102 103 104 105 106

    /**
     * prepareData
     *
     * Prepare simple xml to array for return
     *
     * @param  string $simpleXml 
     * @return array  $return
     */
107 108 109 110 111 112 113 114 115
    public function prepareData($simpleXml)
    {
        if ($simpleXml instanceof SimpleXMLElement) {
            $children = $simpleXml->children();
            $return = null;
        }

        foreach ($children as $element => $value) {
            if ($value instanceof SimpleXMLElement) {
116
                $values = (array) $value->children();
117 118 119 120

                if (count($values) > 0) {
                    $return[$element] = $this->prepareData($value);
                } else {
121
                    if ( ! isset($return[$element])) {
122
                        $return[$element] = (string) $value;
123
                    } else {
124
                        if ( ! is_array($return[$element])) {
125
                            $return[$element] = array($return[$element], (string) $value);
126
                        } else {
127
                            $return[$element][] = (string) $value;
128 129 130 131 132 133 134 135 136
                        }
                    }
                }
            }
        }

        if (is_array($return)) {
            return $return;
        } else {
137
            return array();
138 139
        }
    }
meus's avatar
meus committed
140
}