1<?php
2/**
3 * Copyright 2013-2017 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file LICENSE for license information (LGPL). If you
6 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
7 *
8 * @category  Horde
9 * @copyright 2013-2017 Horde LLC
10 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
11 * @package   Imap_Client
12 */
13
14/**
15 * Namespace data.
16 *
17 * @author    Michael Slusarz <slusarz@horde.org>
18 * @category  Horde
19 * @copyright 2013-2017 Horde LLC
20 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
21 * @package   Imap_Client
22 * @since     2.21.0
23 *
24 * @property-read string $base  The namespace base ($name without trailing
25 *                              delimiter) (UTF-8).
26 * @property string $delimiter  The namespace delimiter.
27 * @property boolean $hidden  Is this a hidden namespace?
28 * @property string $name  The namespace name (UTF-8).
29 * @property string $translation  Returns the translated name of the namespace
30 *                                (UTF-8).
31 * @property integer $type  The namespace type. Either self::NS_PERSONAL,
32 *                          self::NS_OTHER, or self::NS_SHARED.
33 */
34class Horde_Imap_Client_Data_Namespace implements Serializable
35{
36    /* Namespace type constants. */
37    const NS_PERSONAL = 1;
38    const NS_OTHER = 2;
39    const NS_SHARED = 3;
40
41    /**
42     * Data object.
43     *
44     * @var array
45     */
46    protected $_data = array();
47
48    /**
49     * Strips namespace information from the given mailbox name.
50     *
51     * @param string $mbox  Mailbox name.
52     *
53     * @return string  Mailbox name with namespace prefix stripped.
54     */
55    public function stripNamespace($mbox)
56    {
57        $mbox = strval($mbox);
58        $name = $this->name;
59
60        return (strlen($name) && (strpos($mbox, $name) === 0))
61            ? substr($mbox, strlen($name))
62            : $mbox;
63    }
64
65    /**
66     */
67    public function __get($name)
68    {
69        if (isset($this->_data[$name])) {
70            return $this->_data[$name];
71        }
72
73        switch ($name) {
74        case 'base':
75            return rtrim($this->name, $this->delimiter);
76
77        case 'delimiter':
78        case 'name':
79        case 'translation':
80            return '';
81
82        case 'hidden':
83            return false;
84
85        case 'type':
86            return self::NS_PERSONAL;
87        }
88
89        return null;
90    }
91
92    /**
93     */
94    public function __set($name, $value)
95    {
96        switch ($name) {
97        case 'delimiter':
98        case 'name':
99        case 'translation':
100            $this->_data[$name] = strval($value);
101            break;
102
103        case 'hidden':
104            $this->_data[$name] = (bool)$value;
105            break;
106
107        case 'type':
108            $this->_data[$name] = intval($value);
109            break;
110        }
111    }
112
113    /**
114     */
115    public function __isset($name)
116    {
117        return isset($this->_data[$name]);
118    }
119
120    /**
121     */
122    public function __toString()
123    {
124        return $this->name;
125    }
126
127    /* Serializable methods. */
128
129    /**
130     */
131    public function serialize()
132    {
133        return json_encode($this->_data);
134    }
135
136    /**
137     */
138    public function unserialize($data)
139    {
140        $this->_data = json_decode($data, true);
141    }
142
143}
144