← All changes
|
back/Libraries/Imap2/Roundcube/MessageHeaderSorter.php
+54
-0
10.10.1
→
11.1.0
View file →
| @@ -1,0 +1,54 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\Libraries\Imap2\Roundcube; | |
| 4 | + | |
| 5 | +/** | |
| 6 | + * Class for sorting an array of MessageHeader objects in a predetermined order. | |
| 7 | + * | |
| 8 | + * @package Framework | |
| 9 | + * @subpackage Storage | |
| 10 | + * @author Aleksander Machniak <[email protected]> | |
| 11 | + */ | |
| 12 | +class MessageHeaderSorter | |
| 13 | +{ | |
| 14 | + private $uids = []; | |
| 15 | + | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * Set the predetermined sort order. | |
| 19 | + * | |
| 20 | + * @param array $index Numerically indexed array of IMAP UIDs | |
| 21 | + */ | |
| 22 | + function set_index($index) | |
| 23 | + { | |
| 24 | + $index = array_flip($index); | |
| 25 | + | |
| 26 | + $this->uids = $index; | |
| 27 | + } | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * Sort the array of header objects | |
| 31 | + * | |
| 32 | + * @param array $headers Array of MessageHeader objects indexed by UID | |
| 33 | + */ | |
| 34 | + function sort_headers(&$headers) | |
| 35 | + { | |
| 36 | + uksort($headers, [$this, "compare_uids"]); | |
| 37 | + } | |
| 38 | + | |
| 39 | + /** | |
| 40 | + * Sort method called by uksort() | |
| 41 | + * | |
| 42 | + * @param int $a Array key (UID) | |
| 43 | + * @param int $b Array key (UID) | |
| 44 | + */ | |
| 45 | + function compare_uids($a, $b) | |
| 46 | + { | |
| 47 | + // then find each sequence number in my ordered list | |
| 48 | + $posa = isset($this->uids[$a]) ? intval($this->uids[$a]) : -1; | |
| 49 | + $posb = isset($this->uids[$b]) ? intval($this->uids[$b]) : -1; | |
| 50 | + | |
| 51 | + // return the relative position as the comparison value | |
| 52 | + return $posa - $posb; | |
| 53 | + } | |
| 54 | +} | |