forked from jeffreyhill/LRPHP
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutility.php
More file actions
124 lines (107 loc) · 3.04 KB
/
Copy pathutility.php
File metadata and controls
124 lines (107 loc) · 3.04 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
// @package LR-PHP
// @copyright 2011 Jeffrey Hill
// @license Apache 2.0 License http://www.apache.org/licenses/LICENSE-2.0.html
defined('LREXEC') or die();
class LRUtility {
public static function _($t) // Identity
{
return $t;
}
public static function _boolean($t)
{
return $t ? 'true' : 'false';
}
public static function _date($t)
{
return date('Y-m-d',strtotime($t));
}
public static function _datetime($t)
{
return date('c',strtotime($t));
}
public static function _int($t)
{
return intval($t);
}
public static function _string($t)
{
return (string) $t;
}
}
/**
* TidyJSON
*
* A simple class for cleaning up poorly formatted JSON strings. No validation
* is performed; if you pass in bogus data, you will get bogus output.
*
* Credits:
* Mike Matz https://github.com/pix0r
* Kevin Burns https://github.com/KevBurnsJr
*/
class TidyJSON {
protected static $default_config = array(
'indent' => ' ',
'space' => ' ',
);
protected static $string_chars = array('"', "'");
protected static $esc_string_chars = array("\\\"", "\\'");
protected static $white_chars = array(" ", "\t", "\n", "\r");
/**
* tidy
* @param string $json JSON-formatted string you'd like to tidy
* @param array $config Optional configuration values
*/
public static function tidy($json, $config = null) {
$config = self::get_config($config);
$out = '';
$level = 0;
$strchr = null;
$c = null;
for ($x = 0; $x < strlen($json); $x++) {
$lc = $c;
$c = substr($json, $x, 1);
if ($strchr === null) {
if (in_array($c, self::$white_chars))
continue;
if (in_array($c, self::$string_chars)) {
$strchr = $c;
} else {
if ($c == '{' || $c == '[') {
$eol = true;
$level++;
} elseif ($c == '}' || $c == ']') {
$level--;
$out .= "\n" . self::indent($level, $config);
} elseif ($c == ',') {
$eol = true;
} elseif ($c == ':') {
$c .= $config['space'];
}
}
} else {
if ($c === $strchr && !in_array($lc.$c, self::$esc_string_chars)) {
$strchr = null;
}
}
$out .= $c;
if ($eol) {
$eol = false;
$out .= "\n" . self::indent($level, $config);
}
}
// Remove trailing whitespace
while (in_array(substr($out, -1), self::$white_chars)) {
$out = substr($out, 0, -1);
}
return $out;
}
protected static function indent($level, $config) {
$out = '';
for ($x = 0; $x < $level; $x++) $out .= $config['indent'];
return $out;
}
protected static function get_config($config = null) {
return is_array($config) ? array_merge(self::$default_config, $config) : self::$default_config;
}
}