VCard.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Sabre\VObject\Splitter;
  3. use Sabre\VObject;
  4. use Sabre\VObject\Parser\MimeDir;
  5. /**
  6. * Splitter.
  7. *
  8. * This class is responsible for splitting up VCard objects.
  9. *
  10. * It is assumed that the input stream contains 1 or more VCARD objects. This
  11. * class checks for BEGIN:VCARD and END:VCARD and parses each encountered
  12. * component individually.
  13. *
  14. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  15. * @author Dominik Tobschall (http://tobschall.de/)
  16. * @author Armin Hackmann
  17. * @license http://sabre.io/license/ Modified BSD License
  18. */
  19. class VCard implements SplitterInterface
  20. {
  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 its 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. {
  43. $this->input = $input;
  44. $this->parser = new MimeDir($input, $options);
  45. }
  46. /**
  47. * Every time getNext() is called, a new object will be parsed, until we
  48. * hit the end of the stream.
  49. *
  50. * When the end is reached, null will be returned.
  51. *
  52. * @return \Sabre\VObject\Component|null
  53. */
  54. public function getNext()
  55. {
  56. try {
  57. $object = $this->parser->parse();
  58. if (!$object instanceof VObject\Component\VCard) {
  59. throw new VObject\ParseException('The supplied input contained non-VCARD data.');
  60. }
  61. } catch (VObject\EofException $e) {
  62. return;
  63. }
  64. return $object;
  65. }
  66. }