1<?php
2
3namespace Sabre\VObject\Property;
4
5use Sabre\VObject\Property;
6
7/**
8 * BINARY property.
9 *
10 * This object represents BINARY values.
11 *
12 * Binary values are most commonly used by the iCalendar ATTACH property, and
13 * the vCard PHOTO property.
14 *
15 * This property will transparently encode and decode to base64.
16 *
17 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
18 * @author Evert Pot (http://evertpot.com/)
19 * @license http://sabre.io/license/ Modified BSD License
20 */
21class Binary extends Property {
22
23    /**
24     * In case this is a multi-value property. This string will be used as a
25     * delimiter.
26     *
27     * @var string|null
28     */
29    public $delimiter = null;
30
31    /**
32     * Updates the current value.
33     *
34     * This may be either a single, or multiple strings in an array.
35     *
36     * @param string|array $value
37     *
38     * @return void
39     */
40    function setValue($value) {
41
42        if (is_array($value)) {
43
44            if (count($value) === 1) {
45                $this->value = $value[0];
46            } else {
47                throw new \InvalidArgumentException('The argument must either be a string or an array with only one child');
48            }
49
50        } else {
51
52            $this->value = $value;
53
54        }
55
56    }
57
58    /**
59     * Sets a raw value coming from a mimedir (iCalendar/vCard) file.
60     *
61     * This has been 'unfolded', so only 1 line will be passed. Unescaping is
62     * not yet done, but parameters are not included.
63     *
64     * @param string $val
65     *
66     * @return void
67     */
68    function setRawMimeDirValue($val) {
69
70        $this->value = base64_decode($val);
71
72    }
73
74    /**
75     * Returns a raw mime-dir representation of the value.
76     *
77     * @return string
78     */
79    function getRawMimeDirValue() {
80
81        return base64_encode($this->value);
82
83    }
84
85    /**
86     * Returns the type of value.
87     *
88     * This corresponds to the VALUE= parameter. Every property also has a
89     * 'default' valueType.
90     *
91     * @return string
92     */
93    function getValueType() {
94
95        return 'BINARY';
96
97    }
98
99    /**
100     * Returns the value, in the format it should be encoded for json.
101     *
102     * This method must always return an array.
103     *
104     * @return array
105     */
106    function getJsonValue() {
107
108        return [base64_encode($this->getValue())];
109
110    }
111
112    /**
113     * Sets the json value, as it would appear in a jCard or jCal object.
114     *
115     * The value must always be an array.
116     *
117     * @param array $value
118     *
119     * @return void
120     */
121    function setJsonValue(array $value) {
122
123        $value = array_map('base64_decode', $value);
124        parent::setJsonValue($value);
125
126    }
127
128}
129