function finalize()
{
$metadata = $this->metadata->finalize($this->bookVersion, $this->date);
foreach ($this->metadata->namespaces as $ns => $nsuri) {
$this->addNamespace($ns, $nsuri);
}
$opf = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" . "<package xmlns=\"http://www.idpf.org/2007/opf\"\n";
foreach ($this->namespaces as $ns => $uri) {
$opf .= "\txmlns:{$ns}=\"{$uri}\"\n";
}
if ($this->bookVersion === EPub::BOOK_VERSION_EPUB3 && sizeof($this->prefixes) > 0) {
$opf .= "\tprefix=\"";
$addSpace = false;
foreach ($this->prefixes as $name => $uri) {
if ($addSpace) {
$opf .= " ";
} else {
$addSpace = true;
}
$opf .= "{$name}: {$uri}";
}
$opf .= "\"\n";
}
$opf .= "\tunique-identifier=\"" . $this->ident . "\" version=\"" . $this->bookVersion . "\">\n";
$opf .= $metadata;
$opf .= $this->manifest->finalize($this->bookVersion);
$opf .= $this->spine->finalize();
if ($this->guide->length() > 0) {
$opf .= $this->guide->finalize();
}
return $opf . "</package>\n";
}