Skip to content

Commit

Permalink
Merge pull request #67 from wadmiraal/master
Browse files Browse the repository at this point in the history
Implement the Iterator interface for the parser
  • Loading branch information
jeroendesloovere committed Jun 8, 2016
2 parents 4c43f29 + 4a03d2b commit 15d11cd
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 6 deletions.
51 changes: 49 additions & 2 deletions src/VCardParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* Original code is available at: http://code.google.com/p/zendvcard/
*/

use Iterator;

/**
* VCard PHP Class to parse .vcard files.
*
Expand All @@ -32,9 +34,8 @@
* @author ruzicka.jan
* @author Wouter Admiraal <[email protected]>
*/
class VCardParser
class VCardParser implements Iterator
{

/**
* The raw VCard content.
*
Expand All @@ -49,6 +50,13 @@ class VCardParser
*/
protected $vcardObjects;

/**
* The iterator position.
*
* @var int
*/
protected $position;

/**
* Helper function to parse a file directly.
*
Expand All @@ -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.
*
Expand Down
35 changes: 31 additions & 4 deletions tests/VCardParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
class VCardParserTest extends \PHPUnit_Framework_TestCase
{

/**
* @expectedException OutOfBoundsException
*/
Expand Down Expand Up @@ -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');
}
}

0 comments on commit 15d11cd

Please sign in to comment.