Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   1  <?php
   2  /**
   3   * Copyright 2012-2017 Horde LLC (http://www.horde.org/)
   4   *
   5   * See the enclosed file LICENSE 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 2012-2017 Horde LLC
  10   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11   * @package   Imap_Client
  12   */
  13  
  14  /**
  15   * Fetch results object for use with Horde_Imap_Client_Base#fetch().
  16   *
  17   * @author    Michael Slusarz <slusarz@horde.org>
  18   * @category  Horde
  19   * @copyright 2012-2017 Horde LLC
  20   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  21   * @package   Imap_Client
  22   *
  23   * @property-read integer $key_type  The key type (sequence or UID).
  24   */
  25  class Horde_Imap_Client_Fetch_Results
  26  implements ArrayAccess, Countable, IteratorAggregate
  27  {
  28      /**
  29       * Key type constants.
  30       */
  31      const SEQUENCE = 1;
  32      const UID = 2;
  33  
  34      /**
  35       * Internal data array.
  36       *
  37       * @var array
  38       */
  39      protected $_data = array();
  40  
  41      /**
  42       * Key type.
  43       *
  44       * @var integer
  45       */
  46      protected $_keyType;
  47  
  48      /**
  49       * Class to use when creating a new fetch object.
  50       *
  51       * @var string
  52       */
  53      protected $_obClass;
  54  
  55      /**
  56       * Constructor.
  57       *
  58       * @param string $ob_class   Class to use when creating a new fetch
  59       *                           object.
  60       * @param integer $key_type  Key type.
  61       */
  62      public function __construct($ob_class = 'Horde_Imap_Client_Data_Fetch',
  63                                  $key_type = self::UID)
  64      {
  65          $this->_obClass = $ob_class;
  66          $this->_keyType = $key_type;
  67      }
  68  
  69      /**
  70       */
  71      public function __get($name)
  72      {
  73          switch ($name) {
  74          case 'key_type':
  75              return $this->_keyType;
  76          }
  77      }
  78  
  79      /**
  80       * Return a fetch object, creating and storing an empty object in the
  81       * results set if it doesn't currently exist.
  82       *
  83       * @param string $key  The key to retrieve.
  84       *
  85       * @return Horde_Imap_Client_Data_Fetch  The fetch object.
  86       */
  87      public function get($key)
  88      {
  89          if (!isset($this->_data[$key])) {
  90              $this->_data[$key] = new $this->_obClass();
  91          }
  92  
  93          return $this->_data[$key];
  94      }
  95  
  96      /**
  97       * Return the list of IDs.
  98       *
  99       * @return array  ID list.
 100       */
 101      public function ids()
 102      {
 103          ksort($this->_data);
 104          return array_keys($this->_data);
 105      }
 106  
 107      /**
 108       * Return the first fetch object in the results, if there is only one
 109       * object.
 110       *
 111       * @return null|Horde_Imap_Client_Data_Fetch  The fetch object if there is
 112       *                                            only one object, or null.
 113       */
 114      public function first()
 115      {
 116          return (count($this->_data) === 1)
 117              ? reset($this->_data)
 118              : null;
 119      }
 120  
 121      /**
 122       * Clears all fetch results.
 123       *
 124       * @since 2.6.0
 125       */
 126      public function clear()
 127      {
 128          $this->_data = array();
 129      }
 130  
 131      /* ArrayAccess methods. */
 132  
 133      /**
 134       */
 135      public function offsetExists($offset)
 136      {
 137          return isset($this->_data[$offset]);
 138      }
 139  
 140      /**
 141       */
 142      public function offsetGet($offset)
 143      {
 144          return isset($this->_data[$offset])
 145              ? $this->_data[$offset]
 146              : null;
 147      }
 148  
 149      /**
 150       */
 151      public function offsetSet($offset, $value)
 152      {
 153          $this->_data[$offset] = $value;
 154      }
 155  
 156      /**
 157       */
 158      public function offsetUnset($offset)
 159      {
 160          unset($this->_data[$offset]);
 161      }
 162  
 163      /* Countable methods. */
 164  
 165      /**
 166       */
 167      public function count()
 168      {
 169          return count($this->_data);
 170      }
 171  
 172      /* IteratorAggregate methods. */
 173  
 174      /**
 175       */
 176      public function getIterator()
 177      {
 178          ksort($this->_data);
 179          return new ArrayIterator($this->_data);
 180      }
 181  
 182  }