1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16: class IMP_Indices implements ArrayAccess, Countable, Iterator
17: {
18: 19: 20: 21: 22:
23: protected $_default = 'INBOX';
24:
25: 26: 27: 28: 29:
30: protected $_indices = array();
31:
32: 33: 34: 35: 36: 37: 38:
39: public function __construct()
40: {
41: if (func_num_args()) {
42: $args = func_get_args();
43: call_user_func_array(array($this, 'add'), $args);
44: }
45: }
46:
47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73:
74: public function add()
75: {
76: $data = func_get_arg(0);
77: $indices = array();
78:
79: switch (func_num_args()) {
80: case 1:
81: if (is_array($data)) {
82: foreach ($data as $key => $val) {
83: if (is_array($val)) {
84: $indices[$key] = array_keys(array_flip($val));
85: } elseif ($val instanceof Horde_Imap_Client_Ids) {
86: $this->add($key, $val);
87: } else {
88: $this->add($val);
89: }
90: }
91: } elseif (is_string($data)) {
92: $imp_imap = $GLOBALS['injector']->getInstance('IMP_Factory_Imap')->create();
93: $indices = $imp_imap->getUtils()->fromSequenceString($data);
94: if ($imp_imap->pop3) {
95: $indices = array($this->_default => $indices);
96: }
97: } elseif ($data instanceof IMP_Compose) {
98: $indices = array(
99: strval($data->getMetadata('mailbox')) => array($data->getMetadata('uid'))
100: );
101: } elseif ($data instanceof IMP_Contents) {
102: $indices = array(
103: strval($data->getMailbox()) => array($data->getUid())
104: );
105: } elseif ($data instanceof IMP_Indices) {
106: $indices = $data->indices();
107: } elseif ($data instanceof IMP_Mailbox_List_Track) {
108: $idx = $data->getIMAPIndex();
109: $indices = array(
110: strval($idx['mailbox']) => array($idx['uid'])
111: );
112: }
113: break;
114:
115: case 2:
116: $secondarg = func_get_arg(1);
117: if (is_array($secondarg)) {
118: $secondarg = array_keys(array_flip($secondarg));
119: } elseif ($secondarg instanceof Horde_Imap_Client_Ids) {
120: $secondarg = $secondarg->ids;
121: } else {
122: $secondarg = array($secondarg);
123: }
124:
125: if (!empty($secondarg)) {
126: $indices = array(
127: strval(func_get_arg(0)) => $secondarg
128: );
129: }
130: break;
131: }
132:
133: if (!empty($indices)) {
134: if (empty($this->_indices)) {
135: $this->_indices = $indices;
136: } else {
137: 138: 139:
140: foreach (array_keys($indices) as $key) {
141: $this->_indices[$key] = isset($this->_indices[$key])
142: ? array_merge($this->_indices[$key], $indices[$key])
143: : $indices[$key];
144: }
145: }
146: }
147: }
148:
149: 150: 151: 152: 153: 154: 155: 156: 157: 158:
159: public function getSingle($all = false)
160: {
161: $val = reset($this->_indices);
162: return array(
163: IMP_Mailbox::get(key($this->_indices)),
164: $all ? $val : (is_array($val) ? reset($val) : null)
165: );
166: }
167:
168: 169: 170: 171: 172: 173:
174: public function indices()
175: {
176: return $this->_indices;
177: }
178:
179: 180: 181: 182: 183: 184: 185:
186: public function formTo()
187: {
188: $converted = array();
189: foreach ($this->_indices as $key => $val) {
190: $converted[IMP_Mailbox::formTo($key)] = $val;
191: }
192:
193: return $GLOBALS['injector']->getInstance('IMP_Factory_Imap')->create()->getUtils()->toSequenceString($converted, array('mailbox' => true));
194: }
195:
196:
197:
198: 199:
200: public function offsetExists($offset)
201: {
202: return isset($this->_indices[$offset]);
203: }
204:
205: 206:
207: public function offsetGet($offset)
208: {
209: return isset($this->_indices[$offset])
210: ? $this->_indices[$offset]
211: : null;
212: }
213:
214: 215:
216: public function offsetSet($offset, $value)
217: {
218: unset($this->_indices[$offset]);
219: $this->add($offset, $value);
220: }
221:
222: 223:
224: public function offsetUnset($offset)
225: {
226: unset($this->_indices[$offset]);
227: }
228:
229:
230:
231: 232: 233: 234: 235:
236: public function count()
237: {
238: $count = 0;
239:
240: foreach (array_keys($this->_indices) as $key) {
241: $count += count($this->_indices[$key]);
242: }
243:
244: return $count;
245: }
246:
247:
248:
249: 250: 251: 252: 253:
254: public function __toString()
255: {
256: return $GLOBALS['injector']->getInstance('IMP_Factory_Imap')->create()->getUtils()->toSequenceString($this->_indices, array('mailbox' => true));
257: }
258:
259:
260:
261: public function current()
262: {
263: if (!$this->valid()) {
264: return null;
265: }
266:
267: $ret = new stdClass;
268: $ret->mbox = IMP_Mailbox::get($this->key());
269: $ret->uids = current($this->_indices);
270:
271: return $ret;
272: }
273:
274: public function key()
275: {
276: return key($this->_indices);
277: }
278:
279: public function next()
280: {
281: if ($this->valid()) {
282: next($this->_indices);
283: }
284: }
285:
286: public function rewind()
287: {
288: reset($this->_indices);
289: }
290:
291: public function valid()
292: {
293: return !is_null(key($this->_indices));
294: }
295:
296: }
297: