/** * * @param SimpleXMLElement $xml * @param PiBX_AST_Collection $ast * @param <type> $parentObject * @return mixed[] The collection entries */ private function parseAnonymCollection(SimpleXMLElement $xml, PiBX_AST_Collection $ast, $parentObject) { $count = $ast->countChildren(); $list = array(); for ($i = 0; $i < $count; $i++) { $child = $ast->get($i); if ($child instanceof PiBX_AST_Structure) { $name = $child->getName(); $structAst = $this->binding->getASTForName($name); $class = $structAst->getType(); // somehow xpath didn't work well with node names containing a dash // so fetch the nodes with "{}", e.g. "{'node-containing-a-dash'}" $itemCount = count($xml->{$name}); if ($itemCount > 0) { // collection items/nodes are directly in the current node for ($j = 0; $j < $itemCount; $j++) { $newObject = new $class(); $listNode = $xml->{$name}[$j]; $list[] = $this->parseXml($listNode, $structAst, $newObject); } } } else { throw new RuntimeException('Invalid <collection>'); } } return $list; }
private function marshalCollection($object, PiBX_AST_Collection $ast) { $getter = $ast->getGetMethod(); $collectionItems = $object->{$getter}(); $lastNode = $this->parentDomNode; $this->parentDomNode = $this->currentDomNode; foreach ($collectionItems as &$item) { if ($ast->hasChildren()) { $childrenCount = $ast->countChildren(); for ($i = 0; $i < $childrenCount; $i++) { $child = $ast->get($i); if (is_object($item)) { // TODO is abstract mapping collection? $classToMarshal = get_class($item); $classAst = $this->binding->getASTForClass($classToMarshal); $structureName = $child->getName(); $classAst->setName($structureName); $this->marshalObject($item, $classAst); } else { // this collection is just a list of (scalar) values $this->marshalObject($item, $child); } } } } $this->parentDomNode = $lastNode; }