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 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      public function attach(SplObserver $observer)
 173      {
 174          $this->detach($observer);
 175          $this->_observers[] = $observer;
 176      }
 177  
 178      /**
 179       */
 180      public function detach(SplObserver $observer)
 181      {
 182          if (($key = array_search($observer, $this->_observers, true)) !== false) {
 183              unset($this->_observers[$key]);
 184          }
 185      }
 186  
 187      /**
 188       * Notification is triggered internally whenever the object's internal
 189       * data storage is altered.
 190       */
 191      public function notify()
 192      {
 193          foreach ($this->_observers as $val) {
 194              $val->update($this);
 195          }
 196      }
 197  
 198      /* Serializable methods. */
 199  
 200      /**
 201       */
 202      public function serialize()
 203      {
 204          return json_encode($this->_data);
 205      }
 206  
 207      /**
 208       */
 209      public function unserialize($data)
 210      {
 211          $this->_data = json_decode($data, true);
 212      }
 213  
 214  }