VCard.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace Sabre\VObject\Splitter;
  3. use
  4. Sabre\VObject,
  5. Sabre\VObject\Parser\MimeDir;
  6. /**
  7. * Splitter
  8. *
  9. * This class is responsible for splitting up VCard objects.
  10. *
  11. * It is assumed that the input stream contains 1 or more VCARD objects. This
  12. * class checks for BEGIN:VCARD and END:VCARD and parses each encountered
  13. * component individually.
  14. *
  15. * @copyright Copyright (C) 2007-2014 fruux GmbH (https://fruux.com/).
  16. * @author Dominik Tobschall
  17. * @author Armin Hackmann
  18. * @license http://sabre.io/license/ Modified BSD License
  19. */
  20. class VCard implements SplitterInterface {
  21. /**
  22. * File handle
  23. *
  24. * @var resource
  25. */
  26. protected $input;
  27. /**
  28. * Persistent parser
  29. *
  30. * @var MimeDir
  31. */
  32. protected $parser;
  33. /**
  34. * Constructor
  35. *
  36. * The splitter should receive an readable file stream as it's input.
  37. *
  38. * @param resource $input
  39. * @param int $options Parser options, see the OPTIONS constants.
  40. */
  41. public function __construct($input, $options = 0) {
  42. $this->input = $input;
  43. $this->parser = new MimeDir($input, $options);
  44. }
  45. /**
  46. * Every time getNext() is called, a new object will be parsed, until we
  47. * hit the end of the stream.
  48. *
  49. * When the end is reached, null will be returned.
  50. *
  51. * @return Sabre\VObject\Component|null
  52. */
  53. public function getNext() {
  54. try {
  55. $object = $this->parser->parse();
  56. if (!$object instanceof VObject\Component\VCard) {
  57. throw new VObject\ParseException('The supplied input contained non-VCARD data.');
  58. }
  59. } catch (VObject\EofException $e) {
  60. return null;
  61. }
  62. return $object;
  63. }
  64. }