-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from wadmiraal/master
Implement the Iterator interface for the parser
- Loading branch information
Showing
2 changed files
with
80 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> | ||
*/ | ||
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. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters