[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 3 require_once 'Horde/Util.php'; 4 require_once 'Horde/Array.php'; 5 6 /** 7 * Variables:: class. 8 * 9 * $Horde: framework/Util/Variables.php,v 1.8.10.6 2006/03/28 12:31:12 jan Exp $ 10 * 11 * @author Robert E. Coyle <robertecoyle@hotmail.com> 12 * @author Chuck Hagenbuch <chuck@horde.org> 13 * @package Horde_Util 14 */ 15 class Variables { 16 17 var $_vars; 18 var $_expectedVariables = array(); 19 20 function Variables($vars = array()) 21 { 22 if (is_null($vars)) { 23 $vars = Util::dispelMagicQuotes($_REQUEST); 24 } 25 if (isset($vars['_formvars'])) { 26 $this->_expectedVariables = @unserialize($vars['_formvars']); 27 unset($vars['_formvars']); 28 } 29 $this->_vars = $vars; 30 } 31 32 function &getDefaultVariables() 33 { 34 $vars = &new Variables(Util::dispelMagicQuotes($_REQUEST)); 35 return $vars; 36 } 37 38 function exists($varname) 39 { 40 if (count($this->_expectedVariables) && 41 $this->_exists($this->_expectedVariables, $varname, false)) { 42 return true; 43 } 44 return $this->_exists($this->_vars, $varname, false); 45 } 46 47 function get($varname) 48 { 49 $this->_getExists($this->_vars, $varname, $value); 50 return $value; 51 } 52 53 function getExists($varname, &$exists) 54 { 55 $exists = $this->_getExists($this->_vars, $varname, $value); 56 return $value; 57 } 58 59 function set($varname, $value) 60 { 61 $keys = array(); 62 if (!Horde_Array::getArrayParts($varname, $base, $keys)) { 63 $this->_vars[$varname] = $value; 64 } else { 65 array_unshift($keys, $base); 66 $place = &$this->_vars; 67 68 while (count($keys)) { 69 $key = array_shift($keys); 70 if (!isset($place[$key])) { 71 $place[$key] = array(); 72 } 73 $place = &$place[$key]; 74 } 75 76 $place = $value; 77 } 78 } 79 80 function merge($vars) 81 { 82 foreach ($vars as $varname => $value) { 83 $this->set($varname, $value); 84 } 85 } 86 87 function add($varname, $value) 88 { 89 if ($this->exists($varname)) { 90 return false; 91 } 92 $this->_vars[$varname] = $value; 93 } 94 95 function remove($varname) 96 { 97 Horde_Array::getArrayParts($varname, $base, $keys); 98 if (!is_null($base)) { 99 $ptr = &$this->_vars[$base]; 100 $end = count($keys) - 1; 101 foreach ($keys as $key => $val) { 102 if (!isset($ptr[$val])) { 103 break; 104 } 105 if ($end == $key) { 106 array_splice($ptr, array_search($val, array_keys($ptr)), 1); 107 } else { 108 $ptr = &$ptr[$val]; 109 } 110 } 111 } else { 112 unset($this->_vars[$varname]); 113 } 114 } 115 116 /** 117 * Find out whether or not $varname was set in $array. 118 * 119 * @access private 120 * 121 * @param array $array The array to search in (usually 122 * either $this->_vars or 123 * $this->_expectedVariables). 124 * @param string $varname The name of the variable to look 125 * for. 126 * @param boolean $checkExpectedVariables If we don't find $varname, 127 * should we check 128 * $this->_expectedVariables to see 129 * if should have existed (like a 130 * checkbox or select multiple). 131 * 132 * @return Whether or not the variable was set (or, if we've checked 133 * $this->_expectedVariables, should have been set). 134 */ 135 function _exists($array, $varname, $checkExpectedVariables = true) 136 { 137 return $this->_getExists($array, $varname, $value, $checkExpectedVariables); 138 } 139 140 /** 141 * Fetch the requested variable ($varname) into $value, and return 142 * whether or not the variable was set in $array. 143 * 144 * @access private 145 * 146 * @param array $array The array to search in (usually 147 * either $this->_vars or 148 * $this->_expectedVariables). 149 * @param string $varname The name of the variable to look 150 * for. 151 * @param mixed &$value $varname's value gets assigned 152 * to. 153 * this variable. 154 * @param boolean $checkExpectedVariables If we don't find $varname, 155 * should we check 156 * $this->_expectedVariables to see 157 * if it should have existed (like 158 * a checkbox or select multiple). 159 * 160 * @return Whether or not the variable was set (or, if we've checked 161 * $this->_expectedVariables, should have been set). 162 */ 163 function _getExists($array, $varname, &$value, $checkExpectedVariables = true) 164 { 165 if (Horde_Array::getArrayParts($varname, $base, $keys)) { 166 if (!isset($array[$base])) { 167 $value = null; 168 // If we're supposed to check $this->_expectedVariables, do so, 169 // but make sure not to check it again. 170 return $checkExpectedVariables ? $this->_exists($this->_expectedVariables, $varname, false) : false; 171 } else { 172 $searchspace = &$array[$base]; 173 while (count($keys)) { 174 $key = array_shift($keys); 175 if (!isset($searchspace[$key])) { 176 $value = null; 177 // If we're supposed to check 178 // $this->_expectedVariables, do so, but make 179 // sure not to check it again. 180 return $checkExpectedVariables ? $this->_exists($this->_expectedVariables, $varname, false) : false; 181 } 182 $searchspace = &$searchspace[$key]; 183 } 184 $value = $searchspace; 185 return true; 186 } 187 } else { 188 $value = isset($array[$varname]) ? $array[$varname] : null; 189 if (!is_null($value)) { 190 return true; 191 } elseif ($checkExpectedVariables) { 192 // If we're supposed to check 193 // $this->_expectedVariables, do so, but make sure not 194 // to check it again. 195 return $this->_exists($this->_expectedVariables, $varname, false); 196 } else { 197 return false; 198 } 199 } 200 } 201 202 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |