/** * Assign necessary Smarty variables and return a template name * to load in order to display the requested citation format. * For legal values, see getCitationFormats(). Returns null if * format is not supported. * * @param string $format Citation format to display. * * @return string Name of Smarty template file to display. * @access public */ public function getCitation($format) { // Build author list: $authors = array(); $primary = $this->getPrimaryAuthor(); if (!empty($primary)) { $authors[] = $primary; } $authors = array_unique(array_merge($authors, $this->getSecondaryAuthors())); // Collect all details for citation builder: $publishers = $this->getPublishers(); $pubDates = $this->getPublicationDates(); $pubPlaces = $this->getPlacesOfPublication(); $details = array('authors' => $authors, 'title' => $this->getShortTitle(), 'subtitle' => $this->getSubtitle(), 'pubPlace' => count($pubPlaces) > 0 ? $pubPlaces[0] : null, 'pubName' => count($publishers) > 0 ? $publishers[0] : null, 'pubDate' => count($pubDates) > 0 ? $pubDates[0] : null, 'edition' => array($this->getEdition())); // Build the citation: $citation = new CitationBuilder($details); if (in_array($format, $citation->getSupportedCitationFormats())) { return $citation->getCitation($format); } else { return ''; } }
/** * Test citation generation * * @return void * @access public */ public function testCitations() { global $interface; foreach ($this->_citations as $current) { $cb = new CitationBuilder($current['raw']); $tpl = $cb->getAPA(); // Normalize whitespace: $apa = trim(preg_replace("/\\s+/", " ", $interface->fetch($tpl))); $this->assertEquals($current['apa'], $apa); $tpl = $cb->getMLA(); // Normalize whitespace: $mla = trim(preg_replace("/\\s+/", " ", $interface->fetch($tpl))); $this->assertEquals($current['mla'], $mla); // Repeat tests using newer getCitation method: $tpl = $cb->getCitation('APA'); // Normalize whitespace: $apa = trim(preg_replace("/\\s+/", " ", $interface->fetch($tpl))); $this->assertEquals($current['apa'], $apa); $tpl = $cb->getCitation('MLA'); // Normalize whitespace: $mla = trim(preg_replace("/\\s+/", " ", $interface->fetch($tpl))); $this->assertEquals($current['mla'], $mla); } // Test a couple of illegal citation formats: $this->assertEquals('', $cb->getCitation('Citation')); $this->assertEquals('', $cb->getCitation('SupportedCitationFormats')); $this->assertEquals('', $cb->getCitation('badgarbage')); }