root/cleversvg/trunk/elements/csGroup.class.php

Revision 336, 3.0 kB (checked in by nperriault, 10 months ago)

Clever Svg:

  • refs #40: more elements tests
  • Removed depth management, as svg handle it natively

Warning: breaks BC from 0.5.

  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * SVG Group class
4  *
5  * @author     Nicolas Perriault <nperriault@gmail.com>
6  * @package    cleversvg
7  * @subpackage elements
8  */
9 class csGroup extends csElementsContainer
10 {
11
12   /**
13    * Definitions and elements container
14    * @var csElementsContainer
15    */
16   protected $container = null;
17
18   /**
19    * SVG XML node name
20    * @var string
21    */
22   protected $xml_node_name = 'g';
23
24   /**
25    * Class constructor
26    *
27    * @param  mixed  $id     The DOM id of the element
28    * @param  array  $attrs  More attributes to affect
29    */
30   public function __construct($id = null, $attrs = array())
31   {
32     $this->setId($id);
33     $this->attributes = array_merge($attrs, $this->attributes);
34     $this->container = new csElementsContainer();
35   }
36
37   /**
38    * Computes DOMXML Node
39    *
40    * @param  boolean  $embedded  Is SVG element embedded ?
41    * @return DOMElement
42    */
43   public function compile($embedded = false)
44   {
45     $group_node = parent::compile($embedded);
46     $dom = $this->getDomDocument();
47
48     # Prefix
49     $prefix = $embedded === true ? 'svg:' : '';
50
51     // Add elements to current group node
52     foreach ($this->getElements() as $element)
53     {
54       $node_element = $dom->importNode($element->compile($embedded), true);
55       $group_node->appendChild($node_element);
56     }
57
58     // Add used definitions
59     foreach ($this->used_definitions as $used_definition)
60     {
61       $element    = $used_definition[0];
62       if (!$element instanceof csBaseElement)
63       {
64         throw new csException('A definition must be a csBaseElement');
65       }
66       $attributes = $used_definition[1];
67       $use_node   = $dom->createElement($prefix.'use');
68       $use_node->setAttribute('xlink:href', '#'.$element->getId());
69       foreach ($attributes as $attr_name => $attr_value)
70       {
71         $use_node->setAttribute($attr_name, $attr_value);
72       }
73       $group_node->appendChild($use_node);
74     }
75
76     return $group_node;
77   }
78
79   /**
80    * Adds a SVG shape or group to current SVG Document at a certain depth
81    *
82    * @param  mixed    $element  SVG element to add
83    * @throws csException
84    */
85   public function addElement($element)
86   {
87     return $this->container->addElement($element);
88   }
89
90   /**
91    * Retrieve an contained element instance by its DOM id
92    *
93    * @param  string  $id
94    * @return csBaseElement
95    * @throw  csException
96    */
97   public function getElementById($id)
98   {
99     return $this->container->getElementById($id);
100   }
101
102   /**
103    * Returns an array containing current group elements
104    *
105    * @return array
106    */
107   public function getElements()
108   {
109     return $this->container->getElements();
110   }
111
112   /**
113    * Checks if a DOM id already exists in current SVG Document
114    *
115    * @param  string  $id
116    * @return boolean
117    */
118   public function hasId($id)
119   {
120     return $this->container->hasId($id);
121   }
122
123   /**
124    * Uses a definition in current group
125    *
126    * @param  mixed  $element
127    * @param  array  $attrs    More atributes to add to &lt;use/&gt; tag
128    */
129   public function useDefinition($definition, $attrs = array())
130   {
131     return $this->container->useDefinition($definition, $attrs);
132   }
133
134 }
135
Note: See TracBrowser for help on using the browser.