Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   1  <?php
   2  /**
   3   * Copyright 2014-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 2014-2017 Horde LLC
  10   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11   * @package   Imap_Client
  12   */
  13  
  14  /**
  15   * Query the capabilities of a server.
  16   *
  17   * @author    Michael Slusarz <slusarz@horde.org>
  18   * @category  Horde
  19   * @copyright 2014-2017 Horde LLC
  20   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  21   * @package   Imap_Client
  22   * @since     2.24.0
  23   */
  24  class Horde_Imap_Client_Data_Capability
  25  implements Serializable, SplSubject
  26  {
  27      /**
  28       * Capability data.
  29       *
  30       * @var array
  31       */
  32      protected $_data = array();
  33  
  34      /**
  35       * Observers.
  36       *
  37       * @var array
  38       */
  39      protected $_observers = array();
  40  
  41      /**
  42       * Add a capability (and optional parameters).
  43       *
  44       * @param string $capability  The capability to add.
  45       * @param mixed $params       A parameter (or array of parameters) to add.
  46       */
  47      public function add($capability, $params = null)
  48      {
  49          $capability = Horde_String::upper($capability);
  50  
  51          if (is_null($params)) {
  52              if (isset($this->_data[$capability])) {
  53                  return;
  54              }
  55              $params = true;
  56          } else {
  57              if (!is_array($params)) {
  58                  $params = array($params);
  59              }
  60              $params = array_map('Horde_String::upper', $params);
  61  
  62              if (isset($this->_data[$capability]) &&
  63                  is_array($this->_data[$capability])) {
  64                  $params = array_merge($this->_data[$capability], $params);
  65              }
  66          }
  67  
  68          $this->_data[$capability] = $params;
  69          $this->notify();
  70      }
  71  
  72      /**
  73       * Remove a capability.
  74       *
  75       * @param string $capability  The capability to remove.
  76       * @param string $params      A parameter (or array of parameters) to
  77       *                            remove from the capability.
  78       */
  79      public function remove($capability, $params = null)
  80      {
  81          $capability = Horde_String::upper($capability);
  82  
  83          if (is_null($params)) {
  84              unset($this->_data[$capability]);
  85          } elseif (isset($this->_data[$capability])) {
  86              if (!is_array($params)) {
  87                  $params = array($params);
  88              }
  89              $params = array_map('Horde_String::upper', $params);
  90  
  91              $this->_data[$capability] = is_array($this->_data[$capability])
  92                  ? array_diff($this->_data[$capability], $params)
  93                  : array();
  94  
  95              if (empty($this->_data[$capability])) {
  96                  unset($this->_data[$capability]);
  97              }
  98          }
  99  
 100          $this->notify();
 101      }
 102  
 103      /**
 104       * Returns whether the server supports the given capability.
 105       *
 106       * @param string $capability  The capability string to query.
 107       * @param string $parameter   If set, require the parameter to exist.
 108       *
 109       * @return boolean  True if the capability (and parameter) exist.
 110       */
 111      public function query($capability, $parameter = null)
 112      {
 113          $capability = Horde_String::upper($capability);
 114  
 115          if (!isset($this->_data[$capability])) {
 116              return false;
 117          }
 118  
 119          return is_null($parameter) ?:
 120                 (is_array($this->_data[$capability]) &&
 121                  in_array(Horde_String::upper($parameter), $this->_data[$capability]));
 122      }
 123  
 124      /**
 125       * Return the list of parameters for an extension.
 126       *
 127       * @param string $capability  The capability string to query.
 128       *
 129       * @return array  An array of parameters if the extension exists and
 130       *                supports parameters.  Otherwise, an empty array.
 131       */
 132      public function getParams($capability)
 133      {
 134          return ($this->query($capability) && is_array($out = $this->_data[Horde_String::upper($capability)]))
 135              ? $out
 136              : array();
 137      }
 138  
 139      /**
 140       * Is the extension enabled?
 141       *
 142       * @param string $capability  The extension (+ parameter) to query. If
 143       *                            null, returns all enabled extensions.
 144       *
 145       * @return mixed  If $capability is null, return all enabled extensions.
 146       *                Otherwise, true if the extension (+ parameter) is
 147       *                enabled.
 148       */
 149      public function isEnabled($capability = null)
 150      {
 151          return is_null($capability)
 152              ? array()
 153              : false;
 154      }
 155  
 156      /**
 157       * Returns the raw data.
 158       *
 159       * @deprecated
 160       *
 161       * @return array  Capability data.
 162       */
 163      public function toArray()
 164      {
 165          return $this->_data;
 166      }
 167  
 168      /* SplSubject methods. */
 169  
 170      /**
 171       */
 172      #[ReturnTypeWillChange]
 173      public function attach(SplObserver $observer)
 174      {
 175          $this->detach($observer);
 176          $this->_observers[] = $observer;
 177      }
 178  
 179      /**
 180       */
 181      #[ReturnTypeWillChange]
 182      public function detach(SplObserver $observer)
 183      {
 184          if (($key = array_search($observer, $this->_observers, true)) !== false) {
 185              unset($this->_observers[$key]);
 186          }
 187      }
 188  
 189      /**
 190       * Notification is triggered internally whenever the object's internal
 191       * data storage is altered.
 192       */
 193      #[ReturnTypeWillChange]
 194      public function notify()
 195      {
 196          foreach ($this->_observers as $val) {
 197              $val->update($this);
 198          }
 199      }
 200  
 201      /* Serializable methods. */
 202  
 203      /**
 204       */
 205      public function serialize()
 206      {
 207          return serialize($this->__serialize());
 208      }
 209  
 210      /**
 211       */
 212      public function unserialize($data)
 213      {
 214          $data = @unserialize($data);
 215          if (!is_array($data)) {
 216              throw new Exception('Cache version change.');
 217          }
 218          $this->__unserialize();
 219      }
 220  
 221      /**
 222       * @return array
 223       */
 224      public function __serialize()
 225      {
 226          return $this->_data;
 227      }
 228  
 229      public function __unserialize(array $data)
 230      {
 231          $this->_data = $data;
 232      }
 233  
 234  }