1<?php
2/**
3 * Package for building SVG graphics.
4 *
5 * Copyright 2002-2007 The Horde Project (http://www.horde.org/)
6 *
7 * @author  Chuck Hagenbuch <chuck@horde.org>
8 * @package XML_SVG
9 * @license http://www.fsf.org/copyleft/lgpl.html
10 */
11
12/**
13 * XML_SVG_Element
14 *
15 * This is the base class for the different SVG Element
16 * Objects. Extend this class to create a new SVG Element.
17 *
18 * @package XML_SVG
19 */
20class XML_SVG_Element
21{
22
23    var $_elements = null;
24    var $_style = null;
25    var $_transform = null;
26    var $_id = null;
27
28    function XML_SVG_Element($params = array())
29    {
30        foreach ($params as $p => $v) {
31            $param = '_' . $p;
32            $this->$param = $v;
33        }
34    }
35
36    /**
37     * Most SVG elements can contain child elements. This method calls
38     * the printElement method of any child element added to this
39     * object by use of the addChild method.
40     */
41    function printElement()
42    {
43        // Loop and call.
44        if (is_array($this->_elements)) {
45            foreach ($this->_elements as $child) {
46                $child->printElement();
47            }
48        }
49    }
50
51    /**
52     * This method adds an object reference (or value, if $copy is
53     * true) to the _elements array.
54     */
55    function addChild(&$element, $copy = false)
56    {
57        if ($copy) {
58            $this->_elements[] = &$element->copy();
59        } else {
60            $this->_elements[] = &$element;
61        }
62    }
63
64    /**
65     * This method sends a message to the passed element requesting to
66     * be added as a child.
67     */
68    function addParent(&$parent)
69    {
70        if (is_subclass_of($parent, 'XML_SVG_Element')) {
71            $parent->addChild($this);
72        }
73    }
74
75    function copy()
76    {
77        if (version_compare(zend_version(), '2', '>')) {
78            return clone($this);
79        } else {
80            $xml_svg = $this;
81            return $xml_svg;
82        }
83    }
84
85    /**
86     * Print each of the passed parameters, if they are set.
87     */
88    function printParams()
89    {
90        foreach (func_get_args() as $param) {
91            $_param = '_' . $param;
92            if (isset($this->$_param)) {
93                switch ($param) {
94                case 'filter':
95                    echo ' filter="url(#' . $this->$_param . ')"';
96                    break;
97
98                default:
99                    echo ' ' . str_replace('_', '-', $param) . '="' . $this->$_param . '"';
100                    break;
101                }
102            }
103        }
104    }
105
106    // Set any named attribute of an element to a value.
107    function setParam($param, $value)
108    {
109        $attr = '_' . $param;
110        $this->$attr = $value;
111    }
112
113    // Get any named attribute of an element.
114    function getParam($param)
115    {
116        $attr = '_' . $param;
117        if (isset($this->$attr)) {
118            return $this->$attr;
119        } else {
120            return null;
121        }
122    }
123
124    // Print out the object for debugging.
125    function debug()
126    {
127        echo '<pre>'; var_dump($this); echo '</pre>';
128    }
129
130}
131
132