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