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 it's input.
42     *
43     * @param resource $input
44     * @param int $options Parser options, see the OPTIONS constants.
45     */
46    function __construct($input, $options = 0) {
47
48        $this->input = $input;
49        $this->parser = new MimeDir($input, $options);
50
51    }
52
53    /**
54     * Every time getNext() is called, a new object will be parsed, until we
55     * hit the end of the stream.
56     *
57     * When the end is reached, null will be returned.
58     *
59     * @return Sabre\VObject\Component|null
60     */
61    function getNext() {
62
63        try {
64            $object = $this->parser->parse();
65
66            if (!$object instanceof VObject\Component\VCard) {
67                throw new VObject\ParseException('The supplied input contained non-VCARD data.');
68            }
69
70        } catch (VObject\EofException $e) {
71            return;
72        }
73
74        return $object;
75
76    }
77
78}
79