summaryrefslogtreecommitdiff
path: root/modules/simpletest/dumper.php
blob: ae7c88fa8680038cc190141de7ed8e31cb478bab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
// $Id$

/**
 * Displays variables as text and does diffs.
 */
class SimpleDumper {
  /**
   * Renders a variable in a shorter form than print_r().
   *
   * @param mixed $value      Variable to render as a string.
   *
   * @return string           Human readable string form.
   * @access public
   */
  function describeValue($value) {
    $type = $this->getType($value);
    switch ($type) {
      case "Null":
        return "NULL";

      case "Bool":
        return "Boolean: ". ($value ? "true" : "false");

      case "Array":
        return "Array: ". count($value) ." items";

      case "Object":
        return "Object: of ". get_class($value);

      case "String":
        return "String: ". $this->clipString($value, 200);

      default:
        return "$type: $value";
    }
  }

  /**
   *    Gets the string representation of a type.
   *    @param mixed $value    Variable to check against.
   *    @return string         Type.
   *    @access public
   */
  function getType($value) {
    if (!isset($value)) {
      return "Null";
    }
    $functions = array('bool', 'string', 'integer', 'float', 'array', 'resource', 'object');
    foreach ($functions as $function) {
      $function_name = 'is_' . $function;
      if ($function_name($value)) {
        return ucfirst($function);
      }
    }
    return "Unknown";
  }

 /**
  *    Clips a string to a maximum length.
  *    @param string $value         String to truncate.
  *    @param integer $size         Minimum string size to show.
  *    @param integer $position     Centre of string section.
  *    @return string               Shortened version.
  *    @access public
  */
  function clipString($value, $size, $position = 0) {
    $length = strlen($value);
    if ($length <= $size) {
      return $value;
    }
    $position = min($position, $length);
    $start = ($size / 2 > $position ? 0 : $position - $size / 2);
    if ($start + $size > $length) {
      $start = $length - $size;
    }
    $value = substr($value, $start, $size);
    return ($start > 0 ? "..." : "") . $value . ($start + $size < $length ? "..." : "");
  }
}