1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8class Math_Formula_Element implements ArrayAccess, Iterator, Countable 9{ 10 private $type; 11 private $children; 12 13 function __construct($type, array $children = []) 14 { 15 $this->type = $type; 16 $this->children = $children; 17 } 18 19 function addChild($child) 20 { 21 $this->children[] = $child; 22 } 23 24 function offsetExists($offset) 25 { 26 return is_int($offset) && isset($this->children[$offset]); 27 } 28 29 function offsetGet($offset) 30 { 31 if (isset($this->children[$offset])) { 32 return $this->children[$offset]; 33 } 34 } 35 36 function offsetSet($offset, $value) 37 { 38 } 39 40 function offsetUnset($offset) 41 { 42 } 43 44 function __get($name) 45 { 46 foreach ($this->children as $child) { 47 if ($child instanceof Math_Formula_Element && $child->type == $name) { 48 return $child; 49 } 50 } 51 } 52 53 function getType() 54 { 55 return $this->type; 56 } 57 58 function current() 59 { 60 $key = key($this->children); 61 return $this->children[$key]; 62 } 63 64 function next() 65 { 66 next($this->children); 67 } 68 69 function rewind() 70 { 71 reset($this->children); 72 } 73 74 function key() 75 { 76 return key($this->children); 77 } 78 79 function valid() 80 { 81 return false !== current($this->children); 82 } 83 84 function count() 85 { 86 return count($this->children); 87 } 88 89 function getExtraValues(array $allowedKeys) 90 { 91 $extra = []; 92 93 foreach ($this->children as $child) { 94 if ($child instanceof self) { 95 if (! in_array($child->type, $allowedKeys)) { 96 $extra[] = "({$child->type} ...)"; 97 } 98 } else { 99 $extra[] = $child; 100 } 101 } 102 103 if (count($extra)) { 104 return $extra; 105 } 106 } 107} 108