1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class Horde_Imap_Client_Data_AclRights extends Horde_Imap_Client_Data_AclCommon implements ArrayAccess, Iterator, Serializable
16: {
17: 18: 19: 20: 21:
22: protected $_optional = array();
23:
24: 25: 26: 27: 28:
29: protected $_required = array();
30:
31: 32: 33: 34: 35: 36:
37: public function __construct(array $required = array(),
38: array $optional = array())
39: {
40: $this->_required = $required;
41:
42: foreach ($optional as $val) {
43: foreach (str_split($val) as $right) {
44: $this->_optional[$right] = $val;
45: }
46: }
47:
48: $this->_normalize();
49: }
50:
51: 52: 53: 54: 55: 56:
57: public function __toString()
58: {
59: return implode('', array_keys(array_flip(array_merge(array_values($this->_required), array_keys($this->_optional)))));
60: }
61:
62: 63: 64:
65: protected function _normalize()
66: {
67: 68: 69: 70: 71:
72: foreach ($this->_virtual as $key => $val) {
73: if (isset($this->_optional[$key])) {
74: unset($this->_optional[$key]);
75: foreach ($val as $val2) {
76: $this->_optional[$val2] = implode('', $val);
77: }
78: } elseif (($pos = array_search($key, $this->_required)) !== false) {
79: unset($this->_required[$pos]);
80: $this->_required = array_unique(array_merge($this->_required, $val));
81: }
82: }
83: }
84:
85:
86:
87: 88:
89: public function offsetExists($offset)
90: {
91: return (bool)$this[$offset];
92: }
93:
94: 95:
96: public function offsetGet($offset)
97: {
98: if (isset($this->_optional[$offset])) {
99: return $this->_optional[$offset];
100: }
101:
102: $pos = array_search($offset, $this->_required);
103:
104: return ($pos === false)
105: ? null
106: : $this->_required[$pos];
107: }
108:
109: 110:
111: public function offsetSet($offset, $value)
112: {
113: $this->_optional[$offset] = $value;
114: $this->_normalize();
115: }
116:
117: 118:
119: public function offsetUnset($offset)
120: {
121: unset($this->_optional[$offset]);
122: $this->_required = array_values(array_diff($this->_required, array($offset)));
123:
124: if (isset($this->_virtual[$offset])) {
125: foreach ($this->_virtual[$offset] as $val) {
126: unset($this[$val]);
127: }
128: }
129: }
130:
131:
132:
133: 134:
135: public function current()
136: {
137: $val = current($this->_required);
138: return is_null($val)
139: ? current($this->_optional)
140: : $val;
141: }
142:
143: 144:
145: public function key()
146: {
147: $key = key($this->_required);
148: return is_null($key)
149: ? key($this->_optional)
150: : $key;
151: }
152:
153: 154:
155: public function next()
156: {
157: if (key($this->_required) === null) {
158: next($this->_optional);
159: } else {
160: next($this->_required);
161: }
162: }
163:
164: 165:
166: public function rewind()
167: {
168: reset($this->_required);
169: reset($this->_optional);
170: }
171:
172: 173:
174: public function valid()
175: {
176: return ((key($this->_required) !== null) ||
177: (key($this->_optional) !== null));
178:
179: }
180:
181:
182:
183: 184:
185: public function serialize()
186: {
187: return json_encode(array(
188: $this->_required,
189: $this->_optional
190: ));
191: }
192:
193: 194:
195: public function unserialize($data)
196: {
197: list($this->_required, $this->_optional) = json_decode($data);
198: }
199:
200: }
201: