Horde_Imap_Client_Mailbox_List::_mboxCompare PHP Method

_mboxCompare() final protected method

Hierarchical folder sorting function (used with usort()).
final protected _mboxCompare ( string $a, string $b ) : integer
$a string Comparison item 1.
$b string Comparison item 2.
return integer See usort().
    protected final function _mboxCompare($a, $b)
    {
        /* Always return INBOX as "smaller". */
        if ($this->_sortinbox) {
            if (strcasecmp($a, 'INBOX') === 0) {
                return -1;
            } elseif (strcasecmp($b, 'INBOX') === 0) {
                return 1;
            }
        }
        $a_parts = explode($this->_delimiter, $a);
        $b_parts = explode($this->_delimiter, $b);
        $a_count = count($a_parts);
        $b_count = count($b_parts);
        for ($i = 0, $iMax = min($a_count, $b_count); $i < $iMax; ++$i) {
            if ($a_parts[$i] != $b_parts[$i]) {
                /* If only one of the folders is under INBOX, return it as
                 * "smaller". */
                if ($this->_sortinbox && $i === 0) {
                    $a_base = strcasecmp($a_parts[0], 'INBOX') === 0;
                    $b_base = strcasecmp($b_parts[0], 'INBOX') === 0;
                    if ($a_base && !$b_base) {
                        return -1;
                    } elseif (!$a_base && $b_base) {
                        return 1;
                    }
                }
                $cmp = strnatcasecmp($a_parts[$i], $b_parts[$i]);
                return $cmp === 0 ? strcmp($a_parts[$i], $b_parts[$i]) : $cmp;
            } elseif ($a_parts[$i] !== $b_parts[$i]) {
                return strlen($a_parts[$i]) - strlen($b_parts[$i]);
            }
        }
        return $a_count - $b_count;
    }