1<?php
2
3namespace Sabre\VObject;
4
5use Sabre\Xml;
6
7/**
8 * iCalendar/vCard/jCal/jCard/xCal/xCard writer object.
9 *
10 * This object provides a few (static) convenience methods to quickly access
11 * the serializers.
12 *
13 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
14 * @author Ivan Enderlin
15 * @license http://sabre.io/license/ Modified BSD License
16 */
17class Writer {
18
19    /**
20     * Serializes a vCard or iCalendar object.
21     *
22     * @param Component $component
23     *
24     * @return string
25     */
26    static function write(Component $component) {
27
28        return $component->serialize();
29
30    }
31
32    /**
33     * Serializes a jCal or jCard object.
34     *
35     * @param Component $component
36     * @param int $options
37     *
38     * @return string
39     */
40    static function writeJson(Component $component, $options = 0) {
41
42        return json_encode($component, $options);
43
44    }
45
46    /**
47     * Serializes a xCal or xCard object.
48     *
49     * @param Component $component
50     *
51     * @return string
52     */
53    static function writeXml(Component $component) {
54
55        $writer = new Xml\Writer();
56        $writer->openMemory();
57        $writer->setIndent(true);
58
59        $writer->startDocument('1.0', 'utf-8');
60
61        if ($component instanceof Component\VCalendar) {
62
63            $writer->startElement('icalendar');
64            $writer->writeAttribute('xmlns', Parser\Xml::XCAL_NAMESPACE);
65
66        } else {
67
68            $writer->startElement('vcards');
69            $writer->writeAttribute('xmlns', Parser\Xml::XCARD_NAMESPACE);
70
71        }
72
73        $component->xmlSerialize($writer);
74
75        $writer->endElement();
76
77        return $writer->outputMemory();
78
79    }
80
81}
82