Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

   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      #[ReturnTypeWillChange]
 136      public function offsetExists($offset)
 137      {
 138          return isset($this->_data[$offset]);
 139      }
 140  
 141      /**
 142       */
 143      #[ReturnTypeWillChange]
 144      public function offsetGet($offset)
 145      {
 146          return isset($this->_data[$offset])
 147              ? $this->_data[$offset]
 148              : null;
 149      }
 150  
 151      /**
 152       */
 153      #[ReturnTypeWillChange]
 154      public function offsetSet($offset, $value)
 155      {
 156          $this->_data[$offset] = $value;
 157      }
 158  
 159      /**
 160       */
 161      #[ReturnTypeWillChange]
 162      public function offsetUnset($offset)
 163      {
 164          unset($this->_data[$offset]);
 165      }
 166  
 167      /* Countable methods. */
 168  
 169      /**
 170       */
 171      #[ReturnTypeWillChange]
 172      public function count()
 173      {
 174          return count($this->_data);
 175      }
 176  
 177      /* IteratorAggregate methods. */
 178  
 179      /**
 180       */
 181      #[ReturnTypeWillChange]
 182      public function getIterator()
 183      {
 184          ksort($this->_data);
 185          return new ArrayIterator($this->_data);
 186      }
 187  
 188  }