Skip to content

Commit 15d11cd

Browse files
Merge pull request #67 from wadmiraal/master
Implement the Iterator interface for the parser
2 parents 4c43f29 + 4a03d2b commit 15d11cd

File tree

2 files changed

+80
-6
lines changed

2 files changed

+80
-6
lines changed

src/VCardParser.php

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* Original code is available at: http://code.google.com/p/zendvcard/
2222
*/
2323

24+
use Iterator;
25+
2426
/**
2527
* VCard PHP Class to parse .vcard files.
2628
*
@@ -32,9 +34,8 @@
3234
* @author ruzicka.jan
3335
* @author Wouter Admiraal <[email protected]>
3436
*/
35-
class VCardParser
37+
class VCardParser implements Iterator
3638
{
37-
3839
/**
3940
* The raw VCard content.
4041
*
@@ -49,6 +50,13 @@ class VCardParser
4950
*/
5051
protected $vcardObjects;
5152

53+
/**
54+
* The iterator position.
55+
*
56+
* @var int
57+
*/
58+
protected $position;
59+
5260
/**
5361
* Helper function to parse a file directly.
5462
*
@@ -69,9 +77,48 @@ public function __construct($content)
6977
{
7078
$this->content = $content;
7179
$this->vcardObjects = array();
80+
$this->rewind();
7281
$this->parse();
7382
}
7483

84+
public function rewind()
85+
{
86+
$this->position = 0;
87+
}
88+
89+
public function current()
90+
{
91+
if ($this->valid()) {
92+
return $this->getCardAtIndex($this->position);
93+
}
94+
}
95+
96+
public function key()
97+
{
98+
return $this->position;
99+
}
100+
101+
public function next()
102+
{
103+
$this->position++;
104+
}
105+
106+
public function valid()
107+
{
108+
return !empty($this->vcardObjects[$this->position]);
109+
}
110+
111+
/**
112+
* Fetch all the imported VCards.
113+
*
114+
* @return array
115+
* A list of VCard card data objects.
116+
*/
117+
public function getCards()
118+
{
119+
return $this->vcardObjects;
120+
}
121+
75122
/**
76123
* Fetch the imported VCard at the specified index.
77124
*

tests/VCardParserTest.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
*/
1313
class VCardParserTest extends \PHPUnit_Framework_TestCase
1414
{
15-
1615
/**
1716
* @expectedException OutOfBoundsException
1817
*/
@@ -200,11 +199,39 @@ public function testVcardDB()
200199
$this->assertEquals($parser->getCardAtIndex(1)->fullname, "Ipsum Lorem");
201200
}
202201

202+
public function testIteration()
203+
{
204+
// Prepare a VCard DB.
205+
$db = '';
206+
$vcard = new VCard();
207+
$vcard->addName("Admiraal", "Wouter");
208+
$db .= $vcard->buildVCard();
209+
210+
$vcard = new VCard();
211+
$vcard->addName("Lorem", "Ipsum");
212+
$db .= $vcard->buildVCard();
213+
214+
$parser = new VCardParser($db);
215+
foreach ($parser as $i => $card) {
216+
$this->assertEquals($card->fullname, $i == 0 ? "Wouter Admiraal" : "Ipsum Lorem");
217+
}
218+
}
219+
203220
public function testFromFile()
204221
{
205222
$parser = VCardParser::parseFromFile(__DIR__ . '/example.vcf');
206-
$this->assertEquals($parser->getCardAtIndex(0)->firstname, "Wouter");
207-
$this->assertEquals($parser->getCardAtIndex(0)->lastname, "Admiraal");
208-
$this->assertEquals($parser->getCardAtIndex(0)->fullname, "Wouter Admiraal");
223+
// Use this opportunity to test fetching all cards directly.
224+
$cards = $parser->getCards();
225+
$this->assertEquals($cards[0]->firstname, "Wouter");
226+
$this->assertEquals($cards[0]->lastname, "Admiraal");
227+
$this->assertEquals($cards[0]->fullname, "Wouter Admiraal");
228+
}
229+
230+
/**
231+
* @expectedException \RuntimeException
232+
*/
233+
public function testFileNotFound()
234+
{
235+
$parser = VCardParser::parseFromFile(__DIR__ . '/does-not-exist.vcf');
209236
}
210237
}

0 commit comments

Comments
 (0)