[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/horde/framework/Horde/Imap/Client/Data/ -> Thread.php (source)

   1  <?php
   2  /**
   3   * Copyright 2008-2014 Horde LLC (http://www.horde.org/)
   4   *
   5   * See the enclosed file COPYING for license information (LGPL). If you
   6   * did not receive this file, see http://www.horde.org/licenses/lgpl21.
   7   *
   8   * @category  Horde
   9   * @copyright 2008-2014 Horde LLC
  10   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11   * @package   Imap_Client
  12   */
  13  
  14  /**
  15   * Object representing the threaded sort results from
  16   * Horde_Imap_Client_Base#thread().
  17   *
  18   * @author    Michael Slusarz <slusarz@horde.org>
  19   * @category  Horde
  20   * @copyright 2008-2014 Horde LLC
  21   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  22   * @package   Imap_Client
  23   */
  24  class Horde_Imap_Client_Data_Thread implements Countable, Serializable
  25  {
  26      /**
  27       * Internal thread data structure. Keys are base values, values are arrays
  28       * with keys as the ID and values as the level.
  29       *
  30       * @var array
  31       */
  32      protected $_thread = array();
  33  
  34      /**
  35       * The index type.
  36       *
  37       * @var string
  38       */
  39      protected $_type;
  40  
  41      /**
  42       * Constructor.
  43       *
  44       * @param array $data   See $_thread.
  45       * @param string $type  Either 'sequence' or 'uid'.
  46       */
  47      public function __construct($data, $type)
  48      {
  49          $this->_thread = $data;
  50          $this->_type = $type;
  51      }
  52  
  53      /**
  54       * Return the ID type.
  55       *
  56       * @return string  Either 'sequence' or 'uid'.
  57       */
  58      public function getType()
  59      {
  60          return $this->_type;
  61      }
  62  
  63      /**
  64       * Return the sorted list of messages indices.
  65       *
  66       * @return Horde_Imap_Client_Ids  The sorted list of messages.
  67       */
  68      public function messageList()
  69      {
  70          return new Horde_Imap_Client_Ids($this->_getAllIndices(), $this->getType() == 'sequence');
  71      }
  72  
  73      /**
  74       * Returns the list of messages in a thread.
  75       *
  76       * @param integer $index  An index contained in the thread.
  77       *
  78       * @return array  Keys are indices, values are objects with the following
  79       *                properties:
  80       *   - base: (integer) Base ID of the thread. If null, thread is a single
  81       *           message.
  82       *   - last: (boolean) If true, this is the last index in the sublevel.
  83       *   - level: (integer) The sublevel of the index.
  84       */
  85      public function getThread($index)
  86      {
  87          reset($this->_thread);
  88          while (list(,$v) = each($this->_thread)) {
  89              if (isset($v[$index])) {
  90                  reset($v);
  91  
  92                  $ob = new stdClass;
  93                  $ob->base = (count($v) > 1) ? key($v) : null;
  94                  $ob->last = false;
  95  
  96                  $levels = $out = array();
  97                  $last = 0;
  98  
  99                  while (list($k2, $v2) = each($v)) {
 100                      $ob2 = clone $ob;
 101                      $ob2->level = $v2;
 102                      $out[$k2] = $ob2;
 103  
 104                      if (($last < $v2) && isset($levels[$v2])) {
 105                          $out[$levels[$v2]]->last = true;
 106                      }
 107                      $levels[$v2] = $k2;
 108                      $last = $v2;
 109                  }
 110  
 111                  foreach ($levels as $v) {
 112                      $out[$v]->last = true;
 113                  }
 114  
 115                  return $out;
 116              }
 117          }
 118  
 119          return array();
 120      }
 121  
 122      /* Countable methods. */
 123  
 124      /**
 125       */
 126      public function count()
 127      {
 128          return count($this->_getAllIndices());
 129      }
 130  
 131      /* Serializable methods. */
 132  
 133      /**
 134       */
 135      public function serialize()
 136      {
 137          return json_encode(array(
 138              $this->_thread,
 139              $this->_type
 140          ));
 141      }
 142  
 143      /**
 144       */
 145      public function unserialize($data)
 146      {
 147          list($this->_thread, $this->_type) = json_decode($data, true);
 148      }
 149  
 150      /* Protected methods. */
 151  
 152      /**
 153       * Return all indices.
 154       *
 155       * @return array  An array of indices.
 156       */
 157      protected function _getAllIndices()
 158      {
 159          $out = array();
 160  
 161          reset($this->_thread);
 162          while (list(,$v) = each($this->_thread)) {
 163              $out = array_merge($out, array_keys($v));
 164          }
 165  
 166          return $out;
 167      }
 168  
 169  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1