1: <?php
2: /**
3: * THis class provides an interface to query a mailbox's settable permanent
4: * flags.
5: *
6: * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
7: *
8: * See the enclosed file COPYING for license information (GPL). If you
9: * did not receive this file, see http://www.horde.org/licenses/gpl.
10: *
11: * @author Michael Slusarz <slusarz@horde.org>
12: * @category Horde
13: * @license http://www.horde.org/licenses/gpl GPL
14: * @package IMP
15: */
16: class IMP_Imap_PermanentFlags implements Iterator
17: {
18: /* IMAP flag indicating flags can be created in mailbox. */
19: const CREATE = "\\*";
20:
21: /**
22: * Can new flags NOT be created?
23: *
24: * @var boolean
25: */
26: protected $_nocreate = false;
27:
28: /**
29: * List of unsettable flags.
30: *
31: * @var array
32: */
33: protected $_noset = array();
34:
35: /**
36: * List of settable flags.
37: *
38: * @var array
39: */
40: protected $_set = array();
41:
42: /**
43: * Constructor.
44: *
45: * @param array $permflags List of permanent flags in mailbox.
46: * @param array $flags List of flags in mailbox.
47: */
48: public function __construct(array $permflags = array(),
49: array $flags = array())
50: {
51: $this->_nocreate = !in_array(self::CREATE, $permflags);
52: $this->_noset = array_diff($permflags, $flags, array(self::CREATE));
53: $this->_set = array_intersect($permflags, $flags);
54: }
55:
56: /**
57: * Determines if the given flag is allowed to be changed permanently.
58: *
59: * @param string $flag The flag to query.
60: *
61: * @return boolean True if flag can be set permanently.
62: */
63: public function allowed($flag)
64: {
65: $flag = strtolower($flag);
66: return (!in_array($flag, $this->_noset) &&
67: (!$this->_nocreate || in_array($flag, $this->_set)));
68: }
69:
70: /* Iterator methods. */
71:
72: public function current()
73: {
74: return current($this->_set);
75: }
76:
77: public function key()
78: {
79: return key($this->_set);
80: }
81:
82: public function next()
83: {
84: next($this->_set);
85: }
86:
87: public function rewind()
88: {
89: reset($this->_set);
90: }
91:
92: public function valid()
93: {
94: return !is_null(key($this->_set));
95: }
96:
97: }
98: