1<?php
2
3class CSSPropertyCollection {
4  var $_properties;
5  var $_positions;
6  var $_priorities;
7  var $_max_priority;
8
9  function CSSPropertyCollection() {
10    $this->_properties = array();
11    $this->_positions  = array();
12    $this->_priorities = array();
13    $this->_max_priority = 0;
14  }
15
16  function apply(&$state) {
17    $properties = $this->getPropertiesRaw();
18    foreach ($properties as $property) {
19      $key   = $property->get_code();
20      $value = $property->get_value();
21
22      $handler =& CSS::get_handler($key);
23      $handler->replace($value, $state);
24    };
25  }
26
27  function &copy() {
28    $collection =& new CSSPropertyCollection();
29
30    for ($i = 0, $size = count($this->_properties); $i < $size; $i++) {
31      $property =& $this->_properties[$i];
32      $collection->_properties[] =& $property->copy();
33    };
34
35    $collection->_positions    = $this->_positions;
36    $collection->_priorities   = $this->_priorities;
37    $collection->_max_priority = $this->_max_priority;
38
39    return $collection;
40  }
41
42  function add_property($property) {
43    $this->_max_priority ++;
44
45    $code = $property->get_code();
46
47    /**
48     * Important properties shoud not be overridden with non-important ones
49     */
50    if ($this->is_important($code) &&
51        !$property->is_important()) {
52      return;
53    };
54
55    if (array_key_exists($code, $this->_positions)) {
56      $this->_properties[$this->_positions[$code]] = $property;
57      $this->_priorities[$this->_positions[$code]] = $this->_max_priority;
58    } else {
59      $this->_properties[] = $property;
60      $this->_priorities[] = $this->_max_priority;
61      $this->_positions[$code] = count($this->_priorities)-1;
62    };
63  }
64
65  function contains($code) {
66    return isset($this->_positions[$code]);
67  }
68
69  function getMaxPriority() {
70    return $this->_max_priority;
71  }
72
73  function getPropertiesSortedByPriority() {
74    $properties = $this->_properties;
75    $priorities = $this->_priorities;
76
77    array_multisort($priorities, $properties);
78
79    return $properties;
80  }
81
82  function getPropertiesRaw() {
83    return $this->_properties;
84  }
85
86  function is_important($code) {
87    if (!isset($this->_positions[$code])) {
88      return false;
89    };
90    return $this->_properties[$this->_positions[$code]]->is_important();
91  }
92
93  function &get_property_value($code) {
94    if (!isset($this->_positions[$code])) {
95      $null = null;
96      return $null;
97    };
98
99    if (!isset($this->_properties[$this->_positions[$code]])) {
100      $null = null;
101      return $null;
102    };
103
104    $property =& $this->_properties[$this->_positions[$code]];
105    return $property->get_value();
106  }
107
108  function set_property_value($code, $value) {
109    $this->_properties[$this->_positions[$code]]->set_value($value);
110  }
111
112  /**
113   * Merge two sets of CSS properties, overwriting old values
114   * with values from $collection
115   */
116  function merge($collection) {
117    $properties = $collection->getPropertiesSortedByPriority();
118    foreach ($properties as $property) {
119      $this->add_property($property);
120    };
121  }
122}
123
124?>