protected function loadCachedFile() { if (!$this->cacheFile->isReadable()) { return null; } if ($this->cacheFile->getMTime() < $this->dataFile->getMTime()) { return null; } $jsonString = file_get_contents($this->cacheFile->getPathname()); if (false === $jsonString) { throw new InvalidArgumentException("Can not read file content from '{$this->cacheFile->getPathname()}'!"); } return CiteCollection::loadFromJson($jsonString); }
/** * @param string $fileName * @return CiteCollection */ public static function loadFromXml(SimpleXMLElement $xml) { $collection = new CiteCollection(); foreach ($xml->cite as $cite) { $collection->add(new Cite(array('author' => $cite->author, 'title' => $cite->title, 'text' => $cite->text))); } return $collection; }
public function testLoadFromJson() { $xml = new SimpleXMLElement(file_get_contents(WS_TESTS_FIXURES_DIRECTORY . '/cites.xml')); $collection1 = CiteCollection::loadFromXml($xml); $this->assertEquals(6, $collection1->count()); $collection2 = CiteCollection::loadFromJson($collection1->toJson()); $this->assertInstanceOf('CiteCollection', $collection2); $this->assertEquals(6, $collection2->count()); $this->assertEquals('Thomas Carlyle', $collection2[3]->getAuthor()); $this->assertEquals('', $collection2[3]->getTitle()); $this->assertEquals('Der schlimmste aller Fehler ist, sich keines solchen bewusst zu sein.', $collection2[3]->getText()); $this->assertEquals('Albert Einstein', $collection2[4]->getAuthor()); $this->assertEquals('', $collection2[4]->getTitle()); $this->assertEquals('Phantasie ist wichtiger als Wissen, denn Wissen ist begrenzt.', $collection2[4]->getText()); $this->assertEquals('Bla', $collection2[5]->getAuthor()); $this->assertEquals('Blubb title', $collection2[5]->getTitle()); $this->assertEquals('Albert Einstein', $collection2[5]->getText()); }
/** * @return Cite */ public function getCite() { $index = mt_rand(0, $this->collection->count() - 1); return $this->collection[$index]; }