1<?php
2/**
3 * Copyright 2014-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 2014-2017 Horde LLC
10 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
11 * @package   Imap_Client
12 */
13
14/**
15 * List of namespaces.
16 *
17 * @author    Michael Slusarz <slusarz@horde.org>
18 * @category  Horde
19 * @copyright 2014-2017 Horde LLC
20 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
21 * @package   Imap_Client
22 * @since     2.21.0
23 */
24class Horde_Imap_Client_Namespace_List
25implements ArrayAccess, Countable, IteratorAggregate
26{
27    /**
28     * The list of namespace objects.
29     *
30     * @var array
31     */
32    protected $_ns = array();
33
34    /**
35     * Constructor.
36     *
37     * @param array $ns  The list of namespace objects.
38     */
39    public function __construct($ns = array())
40    {
41        foreach ($ns as $val) {
42            $this->_ns[strval($val)] = $val;
43        }
44    }
45
46    /**
47     * Get namespace info for a full mailbox path.
48     *
49     * @param string $mbox       The mailbox path.
50     * @param boolean $personal  If true, will return the empty namespace only
51     *                           if it is a personal namespace.
52     *
53     * @return mixed  The Horde_Imap_Client_Data_Namespace object for the
54     *                mailbox path, or null if the path doesn't exist.
55     */
56    public function getNamespace($mbox, $personal = false)
57    {
58        $mbox = strval($mbox);
59
60        if ($ns = $this[$mbox]) {
61            return $ns;
62        }
63
64        foreach ($this->_ns as $val) {
65            $mbox = $mbox . $val->delimiter;
66            if (strlen($val->name) && (strpos($mbox, $val->name) === 0)) {
67                return $val;
68            }
69        }
70
71        return (($ns = $this['']) && (!$personal || ($ns->type === $ns::NS_PERSONAL)))
72            ? $ns
73            : null;
74    }
75
76    /* ArrayAccess methods. */
77
78    /**
79     */
80    public function offsetExists($offset)
81    {
82        return isset($this->_ns[strval($offset)]);
83    }
84
85    /**
86     */
87    public function offsetGet($offset)
88    {
89        $offset = strval($offset);
90
91        return isset($this->_ns[$offset])
92            ? $this->_ns[$offset]
93            : null;
94    }
95
96    /**
97     */
98    public function offsetSet($offset, $value)
99    {
100        if ($value instanceof Horde_Imap_Client_Data_Namespace) {
101            $this->_ns[strval($value)] = $value;
102        }
103    }
104
105    /**
106     */
107    public function offsetUnset($offset)
108    {
109        unset($this->_ns[strval($offset)]);
110    }
111
112    /* Countable methods. */
113
114    /**
115     */
116    public function count()
117    {
118        return count($this->_ns);
119    }
120
121    /* IteratorAggregate methods. */
122
123    /**
124     */
125    public function getIterator()
126    {
127        return new ArrayIterator($this->_ns);
128    }
129
130}
131