コード例 #1
0
 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);
 }
コード例 #2
0
 /**
  * @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;
 }
コード例 #3
0
 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());
 }
コード例 #4
0
 /**
  * @return Cite
  */
 public function getCite()
 {
     $index = mt_rand(0, $this->collection->count() - 1);
     return $this->collection[$index];
 }