/** * Save a record to disk. * * @param string $id ID of record to save. * @param object $record Record to save (in SimpleXML format). * * @return void */ protected function getRecordXML($id, $record) { if (!isset($record->metadata)) { throw new \Exception("Unexpected missing record metadata."); } // Extract the actual metadata from inside the <metadata></metadata> tags; // there is probably a cleaner way to do this, but this simple method avoids // the complexity of dealing with namespaces in SimpleXML: $xml = trim($record->metadata->asXML()); preg_match('/^<metadata([^\\>]*)>/', $xml, $extractedNs); $xml = preg_replace('/(^<metadata[^\\>]*>)|(<\\/metadata>$)/m', '', $xml); // If we are supposed to inject any values, do so now inside the first // tag of the file: $insert = ''; if (!empty($this->injectId)) { $insert .= "<{$this->injectId}>" . htmlspecialchars($id) . "</{$this->injectId}>"; } if (!empty($this->injectDate)) { $insert .= "<{$this->injectDate}>" . htmlspecialchars((string) $record->header->datestamp) . "</{$this->injectDate}>"; } if (!empty($this->injectSetSpec)) { if (isset($record->header->setSpec)) { foreach ($record->header->setSpec as $current) { $insert .= "<{$this->injectSetSpec}>" . htmlspecialchars((string) $current) . "</{$this->injectSetSpec}>"; } } } if (!empty($this->injectSetName)) { if (isset($record->header->setSpec)) { foreach ($record->header->setSpec as $current) { $name = $this->setNames[(string) $current]; $insert .= "<{$this->injectSetName}>" . htmlspecialchars($name) . "</{$this->injectSetName}>"; } } } if (!empty($this->injectHeaderElements)) { foreach ($this->injectHeaderElements as $element) { if (isset($record->header->{$element})) { $insert .= $record->header->{$element}->asXML(); } } } if (!empty($insert)) { $xml = preg_replace('/>/', '>' . $insert, $xml, 1); } $xml = $this->fixNamespaces($xml, $record->getDocNamespaces(), isset($extractedNs[1]) ? $extractedNs[1] : ''); return trim($xml); }
/** * Save a record to disk. * * @param string $id ID of record to save. * @param object $recordObj Record to save (in SimpleXML format). * * @return string */ public function format($id, $recordObj) { if (!isset($recordObj->metadata)) { throw new \Exception("Unexpected missing record metadata."); } $raw = trim($recordObj->metadata->asXML()); // Extract the actual metadata from inside the <metadata></metadata> tags; // there is probably a cleaner way to do this, but this simple method avoids // the complexity of dealing with namespaces in SimpleXML: $record = preg_replace('/(^<metadata[^\\>]*>)|(<\\/metadata>$)/m', '', $raw); // Collect attributes (for proper namespace resolution): $metadataAttributes = $this->extractMetadataAttributes($raw, $record); // If we are supposed to inject any values, do so now inside the first // tag of the file: $insert = $this->getIdAdditions($id) . $this->getHeaderAdditions($recordObj->header); $xml = !empty($insert) ? preg_replace('/>/', '>' . $insert, $record, 1) : $record; // Build the final record: return trim($this->fixNamespaces($xml, $recordObj->getDocNamespaces(), $metadataAttributes)); }