LazyRecord\Exporter\XMLExporter::exportCollection PHP Method

exportCollection() public method

public exportCollection ( BaseCollection $collection ) : DOMDocument
$collection LazyRecord\BaseCollection
return DOMDocument
    public function exportCollection(BaseCollection $collection)
    {
        $dom = new DOMDocument('1.0', 'utf-8');
        $root = $dom->createElement('export');
        $dom->appendChild($root);
        // $this->appendRecord($dom, $root, $record, NULL, true);
        $schema = $collection->getSchema();
        $relations = $schema->getRelations();
        // find foreign many-to-many schema
        foreach ($relations as $rel) {
            if ($rel['type'] === Relationship::MANY_TO_MANY) {
                $junctionRel = $relations[$rel['relation_junction']];
                $junctionSchema = $junctionRel->newForeignSchema();
                $foreignRel = $junctionSchema->getRelation($rel['relation_foreign']);
                $foreignCollection = $foreignRel->newForeignCollection();
                $foreignSchema = $foreignRel->newForeignSchema();
                $collectionRoot = $dom->createElement('collection');
                $collectionRoot->setAttribute('schema', get_class($foreignSchema));
                $collectionRoot->setAttribute('class', get_class($foreignCollection));
                $root->appendChild($collectionRoot);
                foreach ($foreignCollection as $record) {
                    $this->appendRecord($dom, $collectionRoot, $record, $foreignSchema, true);
                }
            }
        }
        $collectionRoot = $dom->createElement('collection');
        $collectionRoot->setAttribute('schema', get_class($schema));
        $collectionRoot->setAttribute('class', get_class($collection));
        $root->appendChild($collectionRoot);
        foreach ($collection as $record) {
            $this->appendRecord($dom, $collectionRoot, $record, $schema, true);
        }
        $dom->formatOutput = true;
        return $dom;
    }

Usage Example

 public function testExportCollection()
 {
     $book = new Book();
     $ret = $book->create(['title' => 'Run & Skate']);
     $this->assertResultSuccess($ret);
     $author = new Author();
     $ret = $author->create(array('name' => 'Z', 'email' => 'z@z', 'identity' => 'z'));
     $this->assertResultSuccess($ret);
     $exporter = new XMLExporter();
     $exporter->exportCollection(new BookCollection());
 }