|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00011 if (! defined('TYPE_MATTERS')) { 00012 define('TYPE_MATTERS', true); 00013 } 00014 00020 class SimpleDumper { 00021 00028 function describeValue($value) { 00029 $type = $this->getType($value); 00030 switch($type) { 00031 case "Null": 00032 return "NULL"; 00033 case "Boolean": 00034 return "Boolean: " . ($value ? "true" : "false"); 00035 case "Array": 00036 return "Array: " . count($value) . " items"; 00037 case "Object": 00038 return "Object: of " . get_class($value); 00039 case "String": 00040 return "String: " . $this->clipString($value, 200); 00041 default: 00042 return "$type: $value"; 00043 } 00044 return "Unknown"; 00045 } 00046 00053 function getType($value) { 00054 if (! isset($value)) { 00055 return "Null"; 00056 } elseif (is_bool($value)) { 00057 return "Boolean"; 00058 } elseif (is_string($value)) { 00059 return "String"; 00060 } elseif (is_integer($value)) { 00061 return "Integer"; 00062 } elseif (is_float($value)) { 00063 return "Float"; 00064 } elseif (is_array($value)) { 00065 return "Array"; 00066 } elseif (is_resource($value)) { 00067 return "Resource"; 00068 } elseif (is_object($value)) { 00069 return "Object"; 00070 } 00071 return "Unknown"; 00072 } 00073 00084 function describeDifference($first, $second, $identical = false) { 00085 if ($identical) { 00086 if (! $this->_isTypeMatch($first, $second)) { 00087 return "with type mismatch as [" . $this->describeValue($first) . 00088 "] does not match [" . $this->describeValue($second) . "]"; 00089 } 00090 } 00091 $type = $this->getType($first); 00092 if ($type == "Unknown") { 00093 return "with unknown type"; 00094 } 00095 $method = '_describe' . $type . 'Difference'; 00096 return $this->$method($first, $second, $identical); 00097 } 00098 00106 function _isTypeMatch($first, $second) { 00107 return ($this->getType($first) == $this->getType($second)); 00108 } 00109 00118 function clipString($value, $size, $position = 0) { 00119 $length = strlen($value); 00120 if ($length <= $size) { 00121 return $value; 00122 } 00123 $position = min($position, $length); 00124 $start = ($size/2 > $position ? 0 : $position - $size/2); 00125 if ($start + $size > $length) { 00126 $start = $length - $size; 00127 } 00128 $value = substr($value, $start, $size); 00129 return ($start > 0 ? "..." : "") . $value . ($start + $size < $length ? "..." : ""); 00130 } 00131 00141 function _describeGenericDifference($first, $second) { 00142 return "as [" . $this->describeValue($first) . 00143 "] does not match [" . 00144 $this->describeValue($second) . "]"; 00145 } 00146 00156 function _describeNullDifference($first, $second, $identical) { 00157 return $this->_describeGenericDifference($first, $second); 00158 } 00159 00169 function _describeBooleanDifference($first, $second, $identical) { 00170 return $this->_describeGenericDifference($first, $second); 00171 } 00172 00182 function _describeStringDifference($first, $second, $identical) { 00183 if (is_object($second) || is_array($second)) { 00184 return $this->_describeGenericDifference($first, $second); 00185 } 00186 $position = $this->_stringDiffersAt($first, $second); 00187 $message = "at character $position"; 00188 $message .= " with [" . 00189 $this->clipString($first, 200, $position) . "] and [" . 00190 $this->clipString($second, 200, $position) . "]"; 00191 return $message; 00192 } 00193 00203 function _describeIntegerDifference($first, $second, $identical) { 00204 if (is_object($second) || is_array($second)) { 00205 return $this->_describeGenericDifference($first, $second); 00206 } 00207 return "because [" . $this->describeValue($first) . 00208 "] differs from [" . 00209 $this->describeValue($second) . "] by " . 00210 abs($first - $second); 00211 } 00212 00222 function _describeFloatDifference($first, $second, $identical) { 00223 if (is_object($second) || is_array($second)) { 00224 return $this->_describeGenericDifference($first, $second); 00225 } 00226 return "because [" . $this->describeValue($first) . 00227 "] differs from [" . 00228 $this->describeValue($second) . "] by " . 00229 abs($first - $second); 00230 } 00231 00241 function _describeArrayDifference($first, $second, $identical) { 00242 if (! is_array($second)) { 00243 return $this->_describeGenericDifference($first, $second); 00244 } 00245 if (! $this->_isMatchingKeys($first, $second, $identical)) { 00246 return "as key list [" . 00247 implode(", ", array_keys($first)) . "] does not match key list [" . 00248 implode(", ", array_keys($second)) . "]"; 00249 } 00250 foreach (array_keys($first) as $key) { 00251 if ($identical && ($first[$key] === $second[$key])) { 00252 continue; 00253 } 00254 if (! $identical && ($first[$key] == $second[$key])) { 00255 continue; 00256 } 00257 return "with member [$key] " . $this->describeDifference( 00258 $first[$key], 00259 $second[$key], 00260 $identical); 00261 } 00262 return ""; 00263 } 00264 00275 function _isMatchingKeys($first, $second, $identical) { 00276 $first_keys = array_keys($first); 00277 $second_keys = array_keys($second); 00278 if ($identical) { 00279 return ($first_keys === $second_keys); 00280 } 00281 sort($first_keys); 00282 sort($second_keys); 00283 return ($first_keys == $second_keys); 00284 } 00285 00295 function _describeResourceDifference($first, $second, $identical) { 00296 return $this->_describeGenericDifference($first, $second); 00297 } 00298 00308 function _describeObjectDifference($first, $second, $identical) { 00309 if (! is_object($second)) { 00310 return $this->_describeGenericDifference($first, $second); 00311 } 00312 return $this->_describeArrayDifference( 00313 get_object_vars($first), 00314 get_object_vars($second), 00315 $identical); 00316 } 00317 00327 function _stringDiffersAt($first, $second) { 00328 if (! $first || ! $second) { 00329 return 0; 00330 } 00331 if (strlen($first) < strlen($second)) { 00332 list($first, $second) = array($second, $first); 00333 } 00334 $position = 0; 00335 $step = strlen($first); 00336 while ($step > 1) { 00337 $step = (integer)(($step + 1) / 2); 00338 if (strncmp($first, $second, $position + $step) == 0) { 00339 $position += $step; 00340 } 00341 } 00342 return $position; 00343 } 00344 00352 function dump($variable) { 00353 ob_start(); 00354 print_r($variable); 00355 $formatted = ob_get_contents(); 00356 ob_end_clean(); 00357 return $formatted; 00358 } 00359 } 00360 ?>