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    public function setValue($value)
39    {
40        if (is_array($value)) {
41            if (1 === count($value)) {
42                $this->value = $value[0];
43            } else {
44                throw new \InvalidArgumentException('The argument must either be a string or an array with only one child');
45            }
46        } else {
47            $this->value = $value;
48        }
49    }
50
51    /**
52     * Sets a raw value coming from a mimedir (iCalendar/vCard) file.
53     *
54     * This has been 'unfolded', so only 1 line will be passed. Unescaping is
55     * not yet done, but parameters are not included.
56     *
57     * @param string $val
58     */
59    public function setRawMimeDirValue($val)
60    {
61        $this->value = base64_decode($val);
62    }
63
64    /**
65     * Returns a raw mime-dir representation of the value.
66     *
67     * @return string
68     */
69    public function getRawMimeDirValue()
70    {
71        return base64_encode($this->value);
72    }
73
74    /**
75     * Returns the type of value.
76     *
77     * This corresponds to the VALUE= parameter. Every property also has a
78     * 'default' valueType.
79     *
80     * @return string
81     */
82    public function getValueType()
83    {
84        return 'BINARY';
85    }
86
87    /**
88     * Returns the value, in the format it should be encoded for json.
89     *
90     * This method must always return an array.
91     *
92     * @return array
93     */
94    public function getJsonValue()
95    {
96        return [base64_encode($this->getValue())];
97    }
98
99    /**
100     * Sets the json value, as it would appear in a jCard or jCal object.
101     *
102     * The value must always be an array.
103     *
104     * @param array $value
105     */
106    public function setJsonValue(array $value)
107    {
108        $value = array_map('base64_decode', $value);
109        parent::setJsonValue($value);
110    }
111}
112