1<?php
2
3namespace Sabre\CardDAV\Xml\Property;
4
5use Sabre\CardDAV\Plugin;
6use Sabre\Xml\Writer;
7use Sabre\Xml\XmlSerializable;
8
9/**
10 * Supported-address-data property
11 *
12 * This property is a representation of the supported-address-data property
13 * in the CardDAV namespace.
14 *
15 * This property is defined in:
16 *
17 * http://tools.ietf.org/html/rfc6352#section-6.2.2
18 *
19 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
20 * @author Evert Pot (http://evertpot.com/)
21 * @license http://sabre.io/license/ Modified BSD License
22 */
23class SupportedAddressData implements XmlSerializable {
24
25    /**
26     * supported versions
27     *
28     * @var array
29     */
30    protected $supportedData = [];
31
32    /**
33     * Creates the property
34     *
35     * @param array|null $supportedData
36     */
37    function __construct(array $supportedData = null) {
38
39        if (is_null($supportedData)) {
40            $supportedData = [
41                ['contentType' => 'text/vcard', 'version' => '3.0'],
42                ['contentType' => 'text/vcard', 'version' => '4.0'],
43                ['contentType' => 'application/vcard+json', 'version' => '4.0'],
44            ];
45        }
46
47        $this->supportedData = $supportedData;
48
49    }
50
51    /**
52     * The xmlSerialize method is called during xml writing.
53     *
54     * Use the $writer argument to write its own xml serialization.
55     *
56     * An important note: do _not_ create a parent element. Any element
57     * implementing XmlSerializable should only ever write what's considered
58     * its 'inner xml'.
59     *
60     * The parent of the current element is responsible for writing a
61     * containing element.
62     *
63     * This allows serializers to be re-used for different element names.
64     *
65     * If you are opening new elements, you must also close them again.
66     *
67     * @param Writer $writer
68     * @return void
69     */
70    function xmlSerialize(Writer $writer) {
71
72        foreach ($this->supportedData as $supported) {
73            $writer->startElement('{' . Plugin::NS_CARDDAV . '}address-data-type');
74            $writer->writeAttributes([
75                'content-type' => $supported['contentType'],
76                'version'      => $supported['version']
77                ]);
78            $writer->endElement(); // address-data-type
79        }
80
81    }
82
83}
84