Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
   3  
   4  /**
   5   * utility functions
   6   *
   7   * PHP versions 4 and 5
   8   *
   9   * LICENSE: This source file is subject to version 3.01 of the PHP license
  10   * that is available through the world-wide-web at the following URI:
  11   * http://www.php.net/license/3_01.txt If you did not receive a copy of
  12   * the PHP License and are unable to obtain it through the web, please
  13   * send a note to license@php.net so we can mail you a copy immediately.
  14   *
  15   * @category    HTML
  16   * @package     HTML_QuickForm
  17   * @author      Chuck Burgess <ashnazg@php.net>
  18   * @copyright   2001-2018 The PHP Group
  19   * @license     http://www.php.net/license/3_01.txt PHP License 3.01
  20   * @version     CVS: $Id$
  21   * @link        http://pear.php.net/package/HTML_QuickForm
  22   */
  23  
  24  /**
  25   * Provides a collection of static methods for array manipulation.
  26   *
  27   * (courtesy of CiviCRM project (https://civicrm.org/)
  28   *
  29   * @category    HTML
  30   * @package     HTML_QuickForm
  31   * @author      Chuck Burgess <ashnazg@php.net>
  32   * @version     Release: @package_version@
  33   * @since       3.2
  34   */
  35  class HTML_QuickForm_utils
  36  {
  37      /**
  38       * Get a single value from an array-tree.
  39       *
  40       * @param   array     $values   Ex: ['foo' => ['bar' => 123]].
  41       * @param   array     $path     Ex: ['foo', 'bar'].
  42       * @param   mixed     $default
  43       * @return  mixed               Ex 123.
  44       *
  45       * @access  public
  46       * @static
  47       */
  48      static function pathGet($values, $path, $default = NULL) {
  49          foreach ($path as $key) {
  50              if (!is_array($values) || !isset($values[$key])) {
  51                  return $default;
  52              }
  53              $values = $values[$key];
  54          }
  55          return $values;
  56      }
  57  
  58      /**
  59       * Check if a key isset which may be several layers deep.
  60       *
  61       * This is a helper for when the calling function does not know how many layers deep
  62       * the path array is so cannot easily check.
  63       *
  64       * @param   array $values
  65       * @param   array $path
  66       * @return  bool
  67       *
  68       * @access  public
  69       * @static
  70       */
  71      static function pathIsset($values, $path) {
  72          foreach ($path as $key) {
  73              if (!is_array($values) || !isset($values[$key])) {
  74                  return FALSE;
  75              }
  76              $values = $values[$key];
  77          }
  78          return TRUE;
  79      }
  80  
  81      /**
  82       * Set a single value in an array tree.
  83       *
  84       * @param   array   $values     Ex: ['foo' => ['bar' => 123]].
  85       * @param   array   $pathParts  Ex: ['foo', 'bar'].
  86       * @param   mixed   $value      Ex: 456.
  87       * @return  void
  88       *
  89       * @access  public
  90       * @static
  91       */
  92      static function pathSet(&$values, $pathParts, $value) {
  93          $r = &$values;
  94          $last = array_pop($pathParts);
  95          foreach ($pathParts as $part) {
  96              if (!isset($r[$part])) {
  97                  $r[$part] = array();
  98              }
  99              $r = &$r[$part];
 100          }
 101          $r[$last] = $value;
 102      }
 103  
 104      /**
 105       * Check if a key isset which may be several layers deep.
 106       *
 107       * This is a helper for when the calling function does not know how many layers deep the
 108       * path array is so cannot easily check.
 109       *
 110       * @param   array $array
 111       * @param   array $path
 112       * @return  bool
 113       *
 114       * @access  public
 115       * @static
 116       */
 117      static function recursiveIsset($array, $path) {
 118          return self::pathIsset($array, $path);
 119      }
 120  
 121      /**
 122       * Check if a key isset which may be several layers deep.
 123       *
 124       * This is a helper for when the calling function does not know how many layers deep the
 125       * path array is so cannot easily check.
 126       *
 127       * @param   array   $array
 128       * @param   array   $path       An array of keys,
 129       *                              e.g [0, 'bob', 8] where we want to check if $array[0]['bob'][8]
 130       * @param   mixed   $default    Value to return if not found.
 131       * @return  bool
 132       *
 133       * @access  public
 134       * @static
 135       */
 136      static function recursiveValue($array, $path, $default = NULL) {
 137          return self::pathGet($array, $path, $default);
 138      }
 139  
 140      /**
 141       * Append the value to the array using the key provided.
 142       *
 143       * e.g if value is 'llama' & path is [0, 'email', 'location'] result will be
 144       * [0 => ['email' => ['location' => 'llama']]
 145       *
 146       * @param           $path
 147       * @param           $value
 148       * @param   array   $source
 149       * @return  array
 150       *
 151       * @access  public
 152       * @static
 153       */
 154      static function recursiveBuild($path, $value, $source = array()) {
 155          self::pathSet($source, $path, $value);
 156          return $source;
 157      }
 158  }
 159  ?>