2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
 
   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.
 
  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
 
  21  * @link        http://pear.php.net/package/HTML_QuickForm
 
  25  * Provides a collection of static methods for array manipulation.
 
  27  * (courtesy of CiviCRM project (https://civicrm.org/)
 
  30  * @package     HTML_QuickForm
 
  31  * @author      Chuck Burgess <ashnazg@php.net>
 
  32  * @version     Release: 3.2.16
 
  35 class HTML_QuickForm_utils
 
  38      * Get a single value from an array-tree.
 
  40      * @param   array     $values   Ex: ['foo' => ['bar' => 123]].
 
  41      * @param   array     $path     Ex: ['foo', 'bar'].
 
  42      * @param   mixed     $default
 
  43      * @return  mixed               Ex 123.
 
  48     function pathGet($values, $path, $default = NULL) {
 
  49         foreach ($path as $key) {
 
  50             if (!is_array($values) || !isset($values[$key])) {
 
  53             $values = $values[$key];
 
  59      * Check if a key isset which may be several layers deep.
 
  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.
 
  64      * @param   array $values
 
  71     function pathIsset($values, $path) {
 
  72         foreach ($path as $key) {
 
  73             if (!is_array($values) || !isset($values[$key])) {
 
  76             $values = $values[$key];
 
  82      * Set a single value in an array tree.
 
  84      * @param   array   $values     Ex: ['foo' => ['bar' => 123]].
 
  85      * @param   array   $pathParts  Ex: ['foo', 'bar'].
 
  86      * @param   mixed   $value      Ex: 456.
 
  92     function pathSet(&$values, $pathParts, $value) {
 
  94         $last = array_pop($pathParts);
 
  95         foreach ($pathParts as $part) {
 
  96             if (!isset($r[$part])) {
 
 105      * Check if a key isset which may be several layers deep.
 
 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.
 
 110      * @param   array $array
 
 117     function recursiveIsset($array, $path) {
 
 118         return self::pathIsset($array, $path);
 
 122      * Check if a key isset which may be several layers deep.
 
 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.
 
 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.
 
 136     function recursiveValue($array, $path, $default = NULL) {
 
 137         return self::pathGet($array, $path, $default);
 
 141      * Append the value to the array using the key provided.
 
 143      * e.g if value is 'llama' & path is [0, 'email', 'location'] result will be
 
 144      * [0 => ['email' => ['location' => 'llama']]
 
 148      * @param   array   $source
 
 154     function recursiveBuild($path, $value, $source = array()) {
 
 155         self::pathSet($source, $path, $value);