00001 <?php
00002
00011 abstract class SXMLListModel extends SXMLModel implements Countable, Iterator, ArrayAccess {
00012 private $listAttribute = '';
00013 private $listChildAttribute = '';
00014
00024 protected function registerListAttribute($la, $childName) {
00025 $this->listAttribute = $la;
00026 $this->listChildAttribute = $childName;
00027 }
00028
00036 public function getListAttribute() {
00037 return $this->listAttribute;
00038 }
00039
00047 public function getListChildAttribute() {
00048 return $this->listChildAttribute;
00049 }
00050
00059 protected function setXMLImpl($xml) {
00060 $la = $this->listAttribute;
00061 $lca = $this->listChildAttribute;
00062
00063 $laType = $this->getAttributeType($la);
00064
00065 $list = array();
00066 foreach($xml->children() as $child) {
00067 if($child->getName() == $lca) {
00068 $list[] = new $laType($child);
00069 }
00070 else {
00071 $type = $this->getAttributeType($child->getName());
00072 switch($type) {
00073 case 'string':
00074 $this->set($child->getName(), (string)$child);
00075 break;
00076 case 'int':
00077 $this->set($child->getName(), (int)$child);
00078 break;
00079 case 'float':
00080 $this->set($child->getName(), (float)$child);
00081 break;
00082 default:
00083 $this->set($child->getName(), new $type($child));
00084 break;
00085 }
00086 }
00087 }
00088 $this->set($la, $list);
00089
00090 return true;
00091 }
00092
00102 protected function getXMLImpl($root, $forceRegen) {
00103 $la = $this->listAttribute;
00104
00105 $xml = "<$root>";
00106
00107 foreach($this->getAttributes(true) as $attr => $info) {
00108 if($attr == $la) {
00109 $list = $this->get($la);
00110 foreach($list as $item) {
00111 $xml .= $item->getXML(array('forceRegen' => $forceRegen, 'asString' => true));
00112 }
00113 }
00114 else {
00115 switch($info['type']) {
00116 case 'string':
00117 case 'int':
00118 case 'float':
00119 $xml .= "<$attr>" . htmlspecialchars((string)$this->get($attr)) . "</$attr>";
00120 break;
00121 default:
00122 $obj = $this->get($attr);
00123 if($obj)
00124 $subXML = $obj->getXML(array('forceRegen' => $forceRegen, 'asString' => true));
00125 else
00126 $subXML = '';
00127 if($subXML === false) {
00128 $this->setWarning("Could not convert '$attr'");
00129 $this->getWarningFrom($obj);
00130 $this->getErrorFrom($obj);
00131 }
00132 else
00133 $xml .= $subXML;
00134 }
00135 }
00136 }
00137
00138 $xml .= "</$root>";
00139
00140 return $xml;
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00160 public function offsetSet($offset, $value) {
00161 $la = $this->listAttribute;
00162 $this->$la[$offset] = $value;
00163 }
00164
00173 public function offsetExists($offset) {
00174 $num = count($this->get($this->listAttribute));
00175 if($offset < 0 || $offset >= $count)
00176 return false;
00177 else
00178 return true;
00179 }
00180
00189 public function offsetUnset($offset) {
00190 $la = $this->listAttribute;
00191 unset($this->$la[$offset]);
00192 }
00193
00202 public function offsetGet($offset) {
00203 $la = $this->listAttribute;
00204 return $this->$la[$offset];
00205 }
00206
00207
00208
00216 public function count() {
00217 return count($this->get($this->listAttribute));
00218 }
00219
00220
00221
00222 private $position = 0;
00223
00231 public function current() {
00232 $la = $this->listAttribute;
00233 $array = $this->$la;
00234 return $array[$this->position];
00235 }
00236
00244 public function key() {
00245 return $this->position;
00246 }
00247
00255 public function next() {
00256 $this->position++;
00257 }
00258
00266 public function rewind() {
00267 $this->position = 0;
00268 }
00269
00277 public function valid() {
00278 $num = count($this->get($this->listAttribute));
00279 if($this->position < 0 || $this->position >= $num)
00280 return false;
00281 else
00282 return true;
00283 }
00284 }
00285
00286 ?>