diff --git a/src/VCardParser.php b/src/VCardParser.php index 260e132..059b0f3 100644 --- a/src/VCardParser.php +++ b/src/VCardParser.php @@ -21,6 +21,8 @@ * Original code is available at: http://code.google.com/p/zendvcard/ */ +use Iterator; + /** * VCard PHP Class to parse .vcard files. * @@ -32,9 +34,8 @@ * @author ruzicka.jan * @author Wouter Admiraal */ -class VCardParser +class VCardParser implements Iterator { - /** * The raw VCard content. * @@ -49,6 +50,13 @@ class VCardParser */ protected $vcardObjects; + /** + * The iterator position. + * + * @var int + */ + protected $position; + /** * Helper function to parse a file directly. * @@ -69,9 +77,48 @@ public function __construct($content) { $this->content = $content; $this->vcardObjects = array(); + $this->rewind(); $this->parse(); } + public function rewind() + { + $this->position = 0; + } + + public function current() + { + if ($this->valid()) { + return $this->getCardAtIndex($this->position); + } + } + + public function key() + { + return $this->position; + } + + public function next() + { + $this->position++; + } + + public function valid() + { + return !empty($this->vcardObjects[$this->position]); + } + + /** + * Fetch all the imported VCards. + * + * @return array + * A list of VCard card data objects. + */ + public function getCards() + { + return $this->vcardObjects; + } + /** * Fetch the imported VCard at the specified index. * diff --git a/tests/VCardParserTest.php b/tests/VCardParserTest.php index e84abe5..3cff9ff 100644 --- a/tests/VCardParserTest.php +++ b/tests/VCardParserTest.php @@ -12,7 +12,6 @@ */ class VCardParserTest extends \PHPUnit_Framework_TestCase { - /** * @expectedException OutOfBoundsException */ @@ -200,11 +199,39 @@ public function testVcardDB() $this->assertEquals($parser->getCardAtIndex(1)->fullname, "Ipsum Lorem"); } + public function testIteration() + { + // Prepare a VCard DB. + $db = ''; + $vcard = new VCard(); + $vcard->addName("Admiraal", "Wouter"); + $db .= $vcard->buildVCard(); + + $vcard = new VCard(); + $vcard->addName("Lorem", "Ipsum"); + $db .= $vcard->buildVCard(); + + $parser = new VCardParser($db); + foreach ($parser as $i => $card) { + $this->assertEquals($card->fullname, $i == 0 ? "Wouter Admiraal" : "Ipsum Lorem"); + } + } + public function testFromFile() { $parser = VCardParser::parseFromFile(__DIR__ . '/example.vcf'); - $this->assertEquals($parser->getCardAtIndex(0)->firstname, "Wouter"); - $this->assertEquals($parser->getCardAtIndex(0)->lastname, "Admiraal"); - $this->assertEquals($parser->getCardAtIndex(0)->fullname, "Wouter Admiraal"); + // Use this opportunity to test fetching all cards directly. + $cards = $parser->getCards(); + $this->assertEquals($cards[0]->firstname, "Wouter"); + $this->assertEquals($cards[0]->lastname, "Admiraal"); + $this->assertEquals($cards[0]->fullname, "Wouter Admiraal"); + } + + /** + * @expectedException \RuntimeException + */ + public function testFileNotFound() + { + $parser = VCardParser::parseFromFile(__DIR__ . '/does-not-exist.vcf'); } }