@@ -12,31 +12,31 @@ |
||
| 12 | 12 | abstract class ArrayNode extends AbstractNode implements IteratorAggregate, Countable |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Returns the array to be used the the iterator. |
|
| 17 | - * |
|
| 18 | - * @return array |
|
| 19 | - */ |
|
| 20 | - abstract protected function getIteratorArray(); |
|
| 15 | + /** |
|
| 16 | + * Returns the array to be used the the iterator. |
|
| 17 | + * |
|
| 18 | + * @return array |
|
| 19 | + */ |
|
| 20 | + abstract protected function getIteratorArray(); |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Gets the iterator |
|
| 24 | - * |
|
| 25 | - * @return ArrayIterator |
|
| 26 | - */ |
|
| 27 | - public function getIterator() |
|
| 28 | - { |
|
| 29 | - return new ArrayIterator($this->getIteratorArray()); |
|
| 30 | - } |
|
| 22 | + /** |
|
| 23 | + * Gets the iterator |
|
| 24 | + * |
|
| 25 | + * @return ArrayIterator |
|
| 26 | + */ |
|
| 27 | + public function getIterator() |
|
| 28 | + { |
|
| 29 | + return new ArrayIterator($this->getIteratorArray()); |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Returns the count of the iterator array. |
|
| 34 | - * |
|
| 35 | - * @return int |
|
| 36 | - */ |
|
| 37 | - public function count() |
|
| 38 | - { |
|
| 39 | - return count($this->getIteratorArray()); |
|
| 40 | - } |
|
| 32 | + /** |
|
| 33 | + * Returns the count of the iterator array. |
|
| 34 | + * |
|
| 35 | + * @return int |
|
| 36 | + */ |
|
| 37 | + public function count() |
|
| 38 | + { |
|
| 39 | + return count($this->getIteratorArray()); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | 42 | } |
@@ -12,237 +12,237 @@ |
||
| 12 | 12 | class Tag |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * The name of the tag. |
|
| 17 | - * |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 20 | - protected $name; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * The attributes of the tag. |
|
| 24 | - * |
|
| 25 | - * @var array |
|
| 26 | - */ |
|
| 27 | - protected $attr = []; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Is this tag self closing. |
|
| 31 | - * |
|
| 32 | - * @var bool |
|
| 33 | - */ |
|
| 34 | - protected $selfClosing = false; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Tag noise |
|
| 38 | - */ |
|
| 39 | - protected $noise = ''; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * The encoding class to... encode the tags |
|
| 43 | - * |
|
| 44 | - * @var mixed |
|
| 45 | - */ |
|
| 46 | - protected $encode = null; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Sets up the tag with a name. |
|
| 50 | - * |
|
| 51 | - * @param $name |
|
| 52 | - */ |
|
| 53 | - public function __construct($name) |
|
| 54 | - { |
|
| 55 | - $this->name = $name; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Magic method to get any of the attributes. |
|
| 60 | - * |
|
| 61 | - * @param string $key |
|
| 62 | - * @return mixed |
|
| 63 | - */ |
|
| 64 | - public function __get($key) |
|
| 65 | - { |
|
| 66 | - return $this->getAttribute($key); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Magic method to set any attribute. |
|
| 71 | - * |
|
| 72 | - * @param string $key |
|
| 73 | - * @param mixed $value |
|
| 74 | - */ |
|
| 75 | - public function __set($key, $value) |
|
| 76 | - { |
|
| 77 | - $this->setAttribute($key, $value); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Returns the name of this tag. |
|
| 82 | - * |
|
| 83 | - * @return string |
|
| 84 | - */ |
|
| 85 | - public function name() |
|
| 86 | - { |
|
| 87 | - return $this->name; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Sets the tag to be self closing. |
|
| 92 | - * |
|
| 93 | - * @return $this |
|
| 94 | - */ |
|
| 95 | - public function selfClosing() |
|
| 96 | - { |
|
| 97 | - $this->selfClosing = true; |
|
| 98 | - |
|
| 99 | - return $this; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Checks if the tag is self closing. |
|
| 104 | - * |
|
| 105 | - * @return bool |
|
| 106 | - */ |
|
| 107 | - public function isSelfClosing() |
|
| 108 | - { |
|
| 109 | - return $this->selfClosing; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Sets the encoding type to be used. |
|
| 114 | - * |
|
| 115 | - * @param Encode $encode |
|
| 116 | - */ |
|
| 117 | - public function setEncoding(Encode $encode) |
|
| 118 | - { |
|
| 119 | - $this->encode = $encode; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Sets the noise for this tag (if any) |
|
| 124 | - * |
|
| 125 | - * @param $noise |
|
| 126 | - * @return $this |
|
| 127 | - */ |
|
| 128 | - public function noise($noise) |
|
| 129 | - { |
|
| 130 | - $this->noise = $noise; |
|
| 131 | - |
|
| 132 | - return $this; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Set an attribute for this tag. |
|
| 137 | - * |
|
| 138 | - * @param string $key |
|
| 139 | - * @param mixed $value |
|
| 140 | - * @return $this |
|
| 141 | - */ |
|
| 142 | - public function setAttribute($key, $value) |
|
| 143 | - { |
|
| 144 | - $key = strtolower($key); |
|
| 145 | - if ( ! is_array($value)) { |
|
| 146 | - $value = [ |
|
| 147 | - 'value' => $value, |
|
| 148 | - 'doubleQuote' => true, |
|
| 149 | - ]; |
|
| 150 | - } |
|
| 151 | - $this->attr[$key] = $value; |
|
| 152 | - |
|
| 153 | - return $this; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Sets the attributes for this tag |
|
| 158 | - * |
|
| 159 | - * @param array $attr |
|
| 160 | - * @return $this |
|
| 161 | - */ |
|
| 162 | - public function setAttributes(array $attr) |
|
| 163 | - { |
|
| 164 | - foreach ($attr as $key => $value) { |
|
| 165 | - $this->setAttribute($key, $value); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - return $this; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Returns all attributes of this tag. |
|
| 173 | - * |
|
| 174 | - * @return array |
|
| 175 | - */ |
|
| 176 | - public function getAttributes() |
|
| 177 | - { |
|
| 178 | - $return = []; |
|
| 179 | - foreach ($this->attr as $attr => $info) { |
|
| 180 | - $return[$attr] = $this->getAttribute($attr); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - return $return; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * Returns an attribute by the key |
|
| 188 | - * |
|
| 189 | - * @param string $key |
|
| 190 | - * @return mixed |
|
| 191 | - */ |
|
| 192 | - public function getAttribute($key) |
|
| 193 | - { |
|
| 194 | - if ( ! isset($this->attr[$key])) { |
|
| 195 | - return null; |
|
| 196 | - } |
|
| 197 | - $value = $this->attr[$key]['value']; |
|
| 198 | - if (is_string($value) AND ! is_null($this->encode)) { |
|
| 199 | - // convert charset |
|
| 200 | - $this->attr[$key]['value'] = $this->encode->convert($value); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - return $this->attr[$key]; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Generates the opening tag for this object. |
|
| 208 | - * |
|
| 209 | - * @return string |
|
| 210 | - */ |
|
| 211 | - public function makeOpeningTag() |
|
| 212 | - { |
|
| 213 | - $return = '<'.$this->name; |
|
| 214 | - |
|
| 215 | - // add the attributes |
|
| 216 | - foreach ($this->attr as $key => $info) { |
|
| 217 | - $info = $this->getAttribute($key); |
|
| 218 | - $val = $info['value']; |
|
| 219 | - if (is_null($val)) { |
|
| 220 | - $return .= ' '.$key; |
|
| 221 | - } elseif ($info['doubleQuote']) { |
|
| 222 | - $return .= ' '.$key.'="'.$val.'"'; |
|
| 223 | - } else { |
|
| 224 | - $return .= ' '.$key.'=\''.$val.'\''; |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - if ($this->selfClosing) { |
|
| 229 | - return $return.' />'; |
|
| 230 | - } else { |
|
| 231 | - return $return.'>'; |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Generates the closing tag for this object. |
|
| 237 | - * |
|
| 238 | - * @return string |
|
| 239 | - */ |
|
| 240 | - public function makeClosingTag() |
|
| 241 | - { |
|
| 242 | - if ($this->selfClosing) { |
|
| 243 | - return ''; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - return '</'.$this->name.'>'; |
|
| 247 | - } |
|
| 15 | + /** |
|
| 16 | + * The name of the tag. |
|
| 17 | + * |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | + protected $name; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * The attributes of the tag. |
|
| 24 | + * |
|
| 25 | + * @var array |
|
| 26 | + */ |
|
| 27 | + protected $attr = []; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Is this tag self closing. |
|
| 31 | + * |
|
| 32 | + * @var bool |
|
| 33 | + */ |
|
| 34 | + protected $selfClosing = false; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Tag noise |
|
| 38 | + */ |
|
| 39 | + protected $noise = ''; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * The encoding class to... encode the tags |
|
| 43 | + * |
|
| 44 | + * @var mixed |
|
| 45 | + */ |
|
| 46 | + protected $encode = null; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Sets up the tag with a name. |
|
| 50 | + * |
|
| 51 | + * @param $name |
|
| 52 | + */ |
|
| 53 | + public function __construct($name) |
|
| 54 | + { |
|
| 55 | + $this->name = $name; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Magic method to get any of the attributes. |
|
| 60 | + * |
|
| 61 | + * @param string $key |
|
| 62 | + * @return mixed |
|
| 63 | + */ |
|
| 64 | + public function __get($key) |
|
| 65 | + { |
|
| 66 | + return $this->getAttribute($key); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Magic method to set any attribute. |
|
| 71 | + * |
|
| 72 | + * @param string $key |
|
| 73 | + * @param mixed $value |
|
| 74 | + */ |
|
| 75 | + public function __set($key, $value) |
|
| 76 | + { |
|
| 77 | + $this->setAttribute($key, $value); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Returns the name of this tag. |
|
| 82 | + * |
|
| 83 | + * @return string |
|
| 84 | + */ |
|
| 85 | + public function name() |
|
| 86 | + { |
|
| 87 | + return $this->name; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Sets the tag to be self closing. |
|
| 92 | + * |
|
| 93 | + * @return $this |
|
| 94 | + */ |
|
| 95 | + public function selfClosing() |
|
| 96 | + { |
|
| 97 | + $this->selfClosing = true; |
|
| 98 | + |
|
| 99 | + return $this; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Checks if the tag is self closing. |
|
| 104 | + * |
|
| 105 | + * @return bool |
|
| 106 | + */ |
|
| 107 | + public function isSelfClosing() |
|
| 108 | + { |
|
| 109 | + return $this->selfClosing; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Sets the encoding type to be used. |
|
| 114 | + * |
|
| 115 | + * @param Encode $encode |
|
| 116 | + */ |
|
| 117 | + public function setEncoding(Encode $encode) |
|
| 118 | + { |
|
| 119 | + $this->encode = $encode; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Sets the noise for this tag (if any) |
|
| 124 | + * |
|
| 125 | + * @param $noise |
|
| 126 | + * @return $this |
|
| 127 | + */ |
|
| 128 | + public function noise($noise) |
|
| 129 | + { |
|
| 130 | + $this->noise = $noise; |
|
| 131 | + |
|
| 132 | + return $this; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Set an attribute for this tag. |
|
| 137 | + * |
|
| 138 | + * @param string $key |
|
| 139 | + * @param mixed $value |
|
| 140 | + * @return $this |
|
| 141 | + */ |
|
| 142 | + public function setAttribute($key, $value) |
|
| 143 | + { |
|
| 144 | + $key = strtolower($key); |
|
| 145 | + if ( ! is_array($value)) { |
|
| 146 | + $value = [ |
|
| 147 | + 'value' => $value, |
|
| 148 | + 'doubleQuote' => true, |
|
| 149 | + ]; |
|
| 150 | + } |
|
| 151 | + $this->attr[$key] = $value; |
|
| 152 | + |
|
| 153 | + return $this; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Sets the attributes for this tag |
|
| 158 | + * |
|
| 159 | + * @param array $attr |
|
| 160 | + * @return $this |
|
| 161 | + */ |
|
| 162 | + public function setAttributes(array $attr) |
|
| 163 | + { |
|
| 164 | + foreach ($attr as $key => $value) { |
|
| 165 | + $this->setAttribute($key, $value); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + return $this; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Returns all attributes of this tag. |
|
| 173 | + * |
|
| 174 | + * @return array |
|
| 175 | + */ |
|
| 176 | + public function getAttributes() |
|
| 177 | + { |
|
| 178 | + $return = []; |
|
| 179 | + foreach ($this->attr as $attr => $info) { |
|
| 180 | + $return[$attr] = $this->getAttribute($attr); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + return $return; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * Returns an attribute by the key |
|
| 188 | + * |
|
| 189 | + * @param string $key |
|
| 190 | + * @return mixed |
|
| 191 | + */ |
|
| 192 | + public function getAttribute($key) |
|
| 193 | + { |
|
| 194 | + if ( ! isset($this->attr[$key])) { |
|
| 195 | + return null; |
|
| 196 | + } |
|
| 197 | + $value = $this->attr[$key]['value']; |
|
| 198 | + if (is_string($value) AND ! is_null($this->encode)) { |
|
| 199 | + // convert charset |
|
| 200 | + $this->attr[$key]['value'] = $this->encode->convert($value); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + return $this->attr[$key]; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Generates the opening tag for this object. |
|
| 208 | + * |
|
| 209 | + * @return string |
|
| 210 | + */ |
|
| 211 | + public function makeOpeningTag() |
|
| 212 | + { |
|
| 213 | + $return = '<'.$this->name; |
|
| 214 | + |
|
| 215 | + // add the attributes |
|
| 216 | + foreach ($this->attr as $key => $info) { |
|
| 217 | + $info = $this->getAttribute($key); |
|
| 218 | + $val = $info['value']; |
|
| 219 | + if (is_null($val)) { |
|
| 220 | + $return .= ' '.$key; |
|
| 221 | + } elseif ($info['doubleQuote']) { |
|
| 222 | + $return .= ' '.$key.'="'.$val.'"'; |
|
| 223 | + } else { |
|
| 224 | + $return .= ' '.$key.'=\''.$val.'\''; |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + if ($this->selfClosing) { |
|
| 229 | + return $return.' />'; |
|
| 230 | + } else { |
|
| 231 | + return $return.'>'; |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Generates the closing tag for this object. |
|
| 237 | + * |
|
| 238 | + * @return string |
|
| 239 | + */ |
|
| 240 | + public function makeClosingTag() |
|
| 241 | + { |
|
| 242 | + if ($this->selfClosing) { |
|
| 243 | + return ''; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + return '</'.$this->name.'>'; |
|
| 247 | + } |
|
| 248 | 248 | } |
@@ -12,189 +12,189 @@ |
||
| 12 | 12 | class HtmlNode extends ArrayNode |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Remembers what the innerHtml was if it was scanned previously. |
|
| 17 | - */ |
|
| 18 | - protected $innerHtml = null; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Remembers what the outerHtml was if it was scanned previously. |
|
| 22 | - * |
|
| 23 | - * @var string |
|
| 24 | - */ |
|
| 25 | - protected $outerHtml = null; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Remembers what the text was if it was scanned previously. |
|
| 29 | - * |
|
| 30 | - * @var string |
|
| 31 | - */ |
|
| 32 | - protected $text = null; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Remembers what the text was when we looked into all our |
|
| 36 | - * children nodes. |
|
| 37 | - * |
|
| 38 | - * @var string |
|
| 39 | - */ |
|
| 40 | - protected $textWithChildren = null; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Sets up the tag of this node. |
|
| 44 | - * |
|
| 45 | - * @param $tag |
|
| 46 | - */ |
|
| 47 | - public function __construct($tag) |
|
| 48 | - { |
|
| 49 | - if ( ! $tag instanceof Tag) { |
|
| 50 | - $tag = new Tag($tag); |
|
| 51 | - } |
|
| 52 | - $this->tag = $tag; |
|
| 53 | - parent::__construct(); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Gets the inner html of this node. |
|
| 58 | - * |
|
| 59 | - * @return string |
|
| 60 | - * @throws UnknownChildTypeException |
|
| 61 | - */ |
|
| 62 | - public function innerHtml() |
|
| 63 | - { |
|
| 64 | - if ( ! $this->hasChildren()) { |
|
| 65 | - // no children |
|
| 66 | - return ''; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - if ( ! is_null($this->innerHtml)) { |
|
| 70 | - // we already know the result. |
|
| 71 | - return $this->innerHtml; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - $child = $this->firstChild(); |
|
| 75 | - $string = ''; |
|
| 76 | - |
|
| 77 | - // continue to loop until we are out of children |
|
| 78 | - while ( ! is_null($child)) { |
|
| 79 | - if ($child instanceof TextNode) { |
|
| 80 | - $string .= $child->text(); |
|
| 81 | - } elseif ($child instanceof HtmlNode) { |
|
| 82 | - $string .= $child->outerHtml(); |
|
| 83 | - } else { |
|
| 84 | - throw new UnknownChildTypeException('Unknown child type "'.get_class($child).'" found in node'); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - try { |
|
| 88 | - $child = $this->nextChild($child->id()); |
|
| 89 | - } catch (ChildNotFoundException $e) { |
|
| 90 | - // no more children |
|
| 91 | - $child = null; |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - // remember the results |
|
| 96 | - $this->innerHtml = $string; |
|
| 97 | - |
|
| 98 | - return $string; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Gets the html of this node, including it's own |
|
| 103 | - * tag. |
|
| 104 | - * |
|
| 105 | - * @return string |
|
| 106 | - */ |
|
| 107 | - public function outerHtml() |
|
| 108 | - { |
|
| 109 | - // special handling for root |
|
| 110 | - if ($this->tag->name() == 'root') { |
|
| 111 | - return $this->innerHtml(); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - if ( ! is_null($this->outerHtml)) { |
|
| 115 | - // we already know the results. |
|
| 116 | - return $this->outerHtml; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - $return = $this->tag->makeOpeningTag(); |
|
| 120 | - if ($this->tag->isSelfClosing()) { |
|
| 121 | - // ignore any children... there should not be any though |
|
| 122 | - return $return; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - // get the inner html |
|
| 126 | - $return .= $this->innerHtml(); |
|
| 127 | - |
|
| 128 | - // add closing tag |
|
| 129 | - $return .= $this->tag->makeClosingTag(); |
|
| 130 | - |
|
| 131 | - // remember the results |
|
| 132 | - $this->outerHtml = $return; |
|
| 133 | - |
|
| 134 | - return $return; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Gets the text of this node (if there is any text). Or get all the text |
|
| 139 | - * in this node, including children. |
|
| 140 | - * |
|
| 141 | - * @param bool $lookInChildren |
|
| 142 | - * @return string |
|
| 143 | - */ |
|
| 144 | - public function text($lookInChildren = false) |
|
| 145 | - { |
|
| 146 | - if ($lookInChildren) { |
|
| 147 | - if ( ! is_null($this->textWithChildren)) { |
|
| 148 | - // we already know the results. |
|
| 149 | - return $this->textWithChildren; |
|
| 150 | - } |
|
| 151 | - } elseif ( ! is_null($this->text)) { |
|
| 152 | - // we already know the results. |
|
| 153 | - return $this->text; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - // find out if this node has any text children |
|
| 157 | - $text = ''; |
|
| 158 | - foreach ($this->children as $child) { |
|
| 159 | - /** @var AbstractNode $node */ |
|
| 160 | - $node = $child['node']; |
|
| 161 | - if ($node instanceof TextNode) { |
|
| 162 | - $text .= $child['node']->text; |
|
| 163 | - } elseif ($lookInChildren and |
|
| 164 | - $node instanceof HtmlNode |
|
| 165 | - ) { |
|
| 166 | - $text .= $node->text($lookInChildren); |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - // remember our result |
|
| 171 | - if ($lookInChildren) { |
|
| 172 | - $this->textWithChildren = $text; |
|
| 173 | - } else { |
|
| 174 | - $this->text = $text; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - return $text; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Call this when something in the node tree has changed. Like a child has been added |
|
| 182 | - * or a parent has been changed. |
|
| 183 | - */ |
|
| 184 | - protected function clear() |
|
| 185 | - { |
|
| 186 | - $this->innerHtml = null; |
|
| 187 | - $this->outerHtml = null; |
|
| 188 | - $this->text = null; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Returns all children of this html node. |
|
| 193 | - * |
|
| 194 | - * @return array |
|
| 195 | - */ |
|
| 196 | - protected function getIteratorArray() |
|
| 197 | - { |
|
| 198 | - return $this->getChildren(); |
|
| 199 | - } |
|
| 15 | + /** |
|
| 16 | + * Remembers what the innerHtml was if it was scanned previously. |
|
| 17 | + */ |
|
| 18 | + protected $innerHtml = null; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Remembers what the outerHtml was if it was scanned previously. |
|
| 22 | + * |
|
| 23 | + * @var string |
|
| 24 | + */ |
|
| 25 | + protected $outerHtml = null; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Remembers what the text was if it was scanned previously. |
|
| 29 | + * |
|
| 30 | + * @var string |
|
| 31 | + */ |
|
| 32 | + protected $text = null; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Remembers what the text was when we looked into all our |
|
| 36 | + * children nodes. |
|
| 37 | + * |
|
| 38 | + * @var string |
|
| 39 | + */ |
|
| 40 | + protected $textWithChildren = null; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Sets up the tag of this node. |
|
| 44 | + * |
|
| 45 | + * @param $tag |
|
| 46 | + */ |
|
| 47 | + public function __construct($tag) |
|
| 48 | + { |
|
| 49 | + if ( ! $tag instanceof Tag) { |
|
| 50 | + $tag = new Tag($tag); |
|
| 51 | + } |
|
| 52 | + $this->tag = $tag; |
|
| 53 | + parent::__construct(); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Gets the inner html of this node. |
|
| 58 | + * |
|
| 59 | + * @return string |
|
| 60 | + * @throws UnknownChildTypeException |
|
| 61 | + */ |
|
| 62 | + public function innerHtml() |
|
| 63 | + { |
|
| 64 | + if ( ! $this->hasChildren()) { |
|
| 65 | + // no children |
|
| 66 | + return ''; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + if ( ! is_null($this->innerHtml)) { |
|
| 70 | + // we already know the result. |
|
| 71 | + return $this->innerHtml; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + $child = $this->firstChild(); |
|
| 75 | + $string = ''; |
|
| 76 | + |
|
| 77 | + // continue to loop until we are out of children |
|
| 78 | + while ( ! is_null($child)) { |
|
| 79 | + if ($child instanceof TextNode) { |
|
| 80 | + $string .= $child->text(); |
|
| 81 | + } elseif ($child instanceof HtmlNode) { |
|
| 82 | + $string .= $child->outerHtml(); |
|
| 83 | + } else { |
|
| 84 | + throw new UnknownChildTypeException('Unknown child type "'.get_class($child).'" found in node'); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + try { |
|
| 88 | + $child = $this->nextChild($child->id()); |
|
| 89 | + } catch (ChildNotFoundException $e) { |
|
| 90 | + // no more children |
|
| 91 | + $child = null; |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + // remember the results |
|
| 96 | + $this->innerHtml = $string; |
|
| 97 | + |
|
| 98 | + return $string; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Gets the html of this node, including it's own |
|
| 103 | + * tag. |
|
| 104 | + * |
|
| 105 | + * @return string |
|
| 106 | + */ |
|
| 107 | + public function outerHtml() |
|
| 108 | + { |
|
| 109 | + // special handling for root |
|
| 110 | + if ($this->tag->name() == 'root') { |
|
| 111 | + return $this->innerHtml(); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + if ( ! is_null($this->outerHtml)) { |
|
| 115 | + // we already know the results. |
|
| 116 | + return $this->outerHtml; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + $return = $this->tag->makeOpeningTag(); |
|
| 120 | + if ($this->tag->isSelfClosing()) { |
|
| 121 | + // ignore any children... there should not be any though |
|
| 122 | + return $return; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + // get the inner html |
|
| 126 | + $return .= $this->innerHtml(); |
|
| 127 | + |
|
| 128 | + // add closing tag |
|
| 129 | + $return .= $this->tag->makeClosingTag(); |
|
| 130 | + |
|
| 131 | + // remember the results |
|
| 132 | + $this->outerHtml = $return; |
|
| 133 | + |
|
| 134 | + return $return; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Gets the text of this node (if there is any text). Or get all the text |
|
| 139 | + * in this node, including children. |
|
| 140 | + * |
|
| 141 | + * @param bool $lookInChildren |
|
| 142 | + * @return string |
|
| 143 | + */ |
|
| 144 | + public function text($lookInChildren = false) |
|
| 145 | + { |
|
| 146 | + if ($lookInChildren) { |
|
| 147 | + if ( ! is_null($this->textWithChildren)) { |
|
| 148 | + // we already know the results. |
|
| 149 | + return $this->textWithChildren; |
|
| 150 | + } |
|
| 151 | + } elseif ( ! is_null($this->text)) { |
|
| 152 | + // we already know the results. |
|
| 153 | + return $this->text; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + // find out if this node has any text children |
|
| 157 | + $text = ''; |
|
| 158 | + foreach ($this->children as $child) { |
|
| 159 | + /** @var AbstractNode $node */ |
|
| 160 | + $node = $child['node']; |
|
| 161 | + if ($node instanceof TextNode) { |
|
| 162 | + $text .= $child['node']->text; |
|
| 163 | + } elseif ($lookInChildren and |
|
| 164 | + $node instanceof HtmlNode |
|
| 165 | + ) { |
|
| 166 | + $text .= $node->text($lookInChildren); |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + // remember our result |
|
| 171 | + if ($lookInChildren) { |
|
| 172 | + $this->textWithChildren = $text; |
|
| 173 | + } else { |
|
| 174 | + $this->text = $text; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + return $text; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Call this when something in the node tree has changed. Like a child has been added |
|
| 182 | + * or a parent has been changed. |
|
| 183 | + */ |
|
| 184 | + protected function clear() |
|
| 185 | + { |
|
| 186 | + $this->innerHtml = null; |
|
| 187 | + $this->outerHtml = null; |
|
| 188 | + $this->text = null; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Returns all children of this html node. |
|
| 193 | + * |
|
| 194 | + * @return array |
|
| 195 | + */ |
|
| 196 | + protected function getIteratorArray() |
|
| 197 | + { |
|
| 198 | + return $this->getChildren(); |
|
| 199 | + } |
|
| 200 | 200 | } |
@@ -15,154 +15,154 @@ |
||
| 15 | 15 | class Collection implements IteratorAggregate, ArrayAccess, Countable |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * The collection of Nodes. |
|
| 20 | - * |
|
| 21 | - * @param array |
|
| 22 | - */ |
|
| 23 | - protected $collection = []; |
|
| 18 | + /** |
|
| 19 | + * The collection of Nodes. |
|
| 20 | + * |
|
| 21 | + * @param array |
|
| 22 | + */ |
|
| 23 | + protected $collection = []; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Attempts to call the method on the first node in |
|
| 27 | - * the collection. |
|
| 28 | - * |
|
| 29 | - * @param string $method |
|
| 30 | - * @param array $arguments |
|
| 31 | - * @return mixed; |
|
| 32 | - * @throws EmptyCollectionException |
|
| 33 | - */ |
|
| 34 | - public function __call($method, $arguments) |
|
| 35 | - { |
|
| 36 | - $node = reset($this->collection); |
|
| 37 | - if ($node instanceof AbstractNode) { |
|
| 38 | - return call_user_func_array([$node, $method], $arguments); |
|
| 39 | - } else { |
|
| 40 | - throw new EmptyCollectionException('The collection does not contain any Nodes.'); |
|
| 41 | - } |
|
| 42 | - } |
|
| 25 | + /** |
|
| 26 | + * Attempts to call the method on the first node in |
|
| 27 | + * the collection. |
|
| 28 | + * |
|
| 29 | + * @param string $method |
|
| 30 | + * @param array $arguments |
|
| 31 | + * @return mixed; |
|
| 32 | + * @throws EmptyCollectionException |
|
| 33 | + */ |
|
| 34 | + public function __call($method, $arguments) |
|
| 35 | + { |
|
| 36 | + $node = reset($this->collection); |
|
| 37 | + if ($node instanceof AbstractNode) { |
|
| 38 | + return call_user_func_array([$node, $method], $arguments); |
|
| 39 | + } else { |
|
| 40 | + throw new EmptyCollectionException('The collection does not contain any Nodes.'); |
|
| 41 | + } |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Attempts to apply the magic get to the first node |
|
| 46 | - * in the collection. |
|
| 47 | - * |
|
| 48 | - * @param mixed $key |
|
| 49 | - * @return mixed |
|
| 50 | - * @throws EmptyCollectionException |
|
| 51 | - */ |
|
| 52 | - public function __get($key) |
|
| 53 | - { |
|
| 54 | - $node = reset($this->collection); |
|
| 55 | - if ($node instanceof AbstractNode) { |
|
| 56 | - return $node->$key; |
|
| 57 | - } else { |
|
| 58 | - throw new EmptyCollectionException('The collection does not contain any Nodes.'); |
|
| 59 | - } |
|
| 60 | - } |
|
| 44 | + /** |
|
| 45 | + * Attempts to apply the magic get to the first node |
|
| 46 | + * in the collection. |
|
| 47 | + * |
|
| 48 | + * @param mixed $key |
|
| 49 | + * @return mixed |
|
| 50 | + * @throws EmptyCollectionException |
|
| 51 | + */ |
|
| 52 | + public function __get($key) |
|
| 53 | + { |
|
| 54 | + $node = reset($this->collection); |
|
| 55 | + if ($node instanceof AbstractNode) { |
|
| 56 | + return $node->$key; |
|
| 57 | + } else { |
|
| 58 | + throw new EmptyCollectionException('The collection does not contain any Nodes.'); |
|
| 59 | + } |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Applies the magic string method to the first node in |
|
| 64 | - * the collection. |
|
| 65 | - * |
|
| 66 | - * @return string |
|
| 67 | - * @throws EmptyCollectionException |
|
| 68 | - */ |
|
| 69 | - public function __toString() |
|
| 70 | - { |
|
| 71 | - $node = reset($this->collection); |
|
| 72 | - if ($node instanceof AbstractNode) { |
|
| 73 | - return (string)$node; |
|
| 74 | - } else { |
|
| 75 | - throw new EmptyCollectionException('The collection does not contain any Nodes.'); |
|
| 76 | - } |
|
| 77 | - } |
|
| 62 | + /** |
|
| 63 | + * Applies the magic string method to the first node in |
|
| 64 | + * the collection. |
|
| 65 | + * |
|
| 66 | + * @return string |
|
| 67 | + * @throws EmptyCollectionException |
|
| 68 | + */ |
|
| 69 | + public function __toString() |
|
| 70 | + { |
|
| 71 | + $node = reset($this->collection); |
|
| 72 | + if ($node instanceof AbstractNode) { |
|
| 73 | + return (string)$node; |
|
| 74 | + } else { |
|
| 75 | + throw new EmptyCollectionException('The collection does not contain any Nodes.'); |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Returns the count of the collection. |
|
| 81 | - * |
|
| 82 | - * @return int |
|
| 83 | - */ |
|
| 84 | - public function count() |
|
| 85 | - { |
|
| 86 | - return count($this->collection); |
|
| 87 | - } |
|
| 79 | + /** |
|
| 80 | + * Returns the count of the collection. |
|
| 81 | + * |
|
| 82 | + * @return int |
|
| 83 | + */ |
|
| 84 | + public function count() |
|
| 85 | + { |
|
| 86 | + return count($this->collection); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Returns an iterator for the collection. |
|
| 91 | - * |
|
| 92 | - * @return ArrayIterator |
|
| 93 | - */ |
|
| 94 | - public function getIterator() |
|
| 95 | - { |
|
| 96 | - return new ArrayIterator($this->collection); |
|
| 97 | - } |
|
| 89 | + /** |
|
| 90 | + * Returns an iterator for the collection. |
|
| 91 | + * |
|
| 92 | + * @return ArrayIterator |
|
| 93 | + */ |
|
| 94 | + public function getIterator() |
|
| 95 | + { |
|
| 96 | + return new ArrayIterator($this->collection); |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Set an attribute by the given offset |
|
| 101 | - * |
|
| 102 | - * @param mixed $offset |
|
| 103 | - * @param mixed $value |
|
| 104 | - */ |
|
| 105 | - public function offsetSet($offset, $value) |
|
| 106 | - { |
|
| 107 | - if (is_null($offset)) { |
|
| 108 | - $this->collection[] = $value; |
|
| 109 | - } else { |
|
| 110 | - $this->collection[$offset] = $value; |
|
| 111 | - } |
|
| 112 | - } |
|
| 99 | + /** |
|
| 100 | + * Set an attribute by the given offset |
|
| 101 | + * |
|
| 102 | + * @param mixed $offset |
|
| 103 | + * @param mixed $value |
|
| 104 | + */ |
|
| 105 | + public function offsetSet($offset, $value) |
|
| 106 | + { |
|
| 107 | + if (is_null($offset)) { |
|
| 108 | + $this->collection[] = $value; |
|
| 109 | + } else { |
|
| 110 | + $this->collection[$offset] = $value; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * Checks if an offset exists. |
|
| 116 | - * |
|
| 117 | - * @param mixed $offset |
|
| 118 | - * @return bool |
|
| 119 | - */ |
|
| 120 | - public function offsetExists($offset) |
|
| 121 | - { |
|
| 122 | - return isset($this->collection[$offset]); |
|
| 123 | - } |
|
| 114 | + /** |
|
| 115 | + * Checks if an offset exists. |
|
| 116 | + * |
|
| 117 | + * @param mixed $offset |
|
| 118 | + * @return bool |
|
| 119 | + */ |
|
| 120 | + public function offsetExists($offset) |
|
| 121 | + { |
|
| 122 | + return isset($this->collection[$offset]); |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * Unset a collection Node. |
|
| 127 | - * |
|
| 128 | - * @param mixed $offset |
|
| 129 | - */ |
|
| 130 | - public function offsetUnset($offset) |
|
| 131 | - { |
|
| 132 | - unset($this->collection[$offset]); |
|
| 133 | - } |
|
| 125 | + /** |
|
| 126 | + * Unset a collection Node. |
|
| 127 | + * |
|
| 128 | + * @param mixed $offset |
|
| 129 | + */ |
|
| 130 | + public function offsetUnset($offset) |
|
| 131 | + { |
|
| 132 | + unset($this->collection[$offset]); |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * Gets a node at the given offset, or null |
|
| 137 | - * |
|
| 138 | - * @param mixed $offset |
|
| 139 | - * @return mixed |
|
| 140 | - */ |
|
| 141 | - public function offsetGet($offset) |
|
| 142 | - { |
|
| 143 | - return isset($this->collection[$offset]) ? $this->collection[$offset] : null; |
|
| 144 | - } |
|
| 135 | + /** |
|
| 136 | + * Gets a node at the given offset, or null |
|
| 137 | + * |
|
| 138 | + * @param mixed $offset |
|
| 139 | + * @return mixed |
|
| 140 | + */ |
|
| 141 | + public function offsetGet($offset) |
|
| 142 | + { |
|
| 143 | + return isset($this->collection[$offset]) ? $this->collection[$offset] : null; |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * Returns this collection as an array. |
|
| 148 | - * |
|
| 149 | - * @return array |
|
| 150 | - */ |
|
| 151 | - public function toArray() |
|
| 152 | - { |
|
| 153 | - return $this->collection; |
|
| 154 | - } |
|
| 146 | + /** |
|
| 147 | + * Returns this collection as an array. |
|
| 148 | + * |
|
| 149 | + * @return array |
|
| 150 | + */ |
|
| 151 | + public function toArray() |
|
| 152 | + { |
|
| 153 | + return $this->collection; |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - /** |
|
| 157 | - * Similar to jQuery "each" method. Calls the callback with each |
|
| 158 | - * Node in this collection. |
|
| 159 | - * |
|
| 160 | - * @param callback $callback |
|
| 161 | - */ |
|
| 162 | - public function each($callback) |
|
| 163 | - { |
|
| 164 | - foreach ($this->collection as $key => $value) { |
|
| 165 | - $callback($value, $key); |
|
| 166 | - } |
|
| 167 | - } |
|
| 156 | + /** |
|
| 157 | + * Similar to jQuery "each" method. Calls the callback with each |
|
| 158 | + * Node in this collection. |
|
| 159 | + * |
|
| 160 | + * @param callback $callback |
|
| 161 | + */ |
|
| 162 | + public function each($callback) |
|
| 163 | + { |
|
| 164 | + foreach ($this->collection as $key => $value) { |
|
| 165 | + $callback($value, $key); |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | 168 | } |