Skip to content

Commit 189d664

Browse files
committed
Added Json Formatter and a String toolkit
1 parent d3ca03f commit 189d664

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2011 Steve Sperandeo <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Altumo library.
5+
*
6+
* (c) Steve Sperandeo <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
13+
14+
15+
namespace Altumo\Javascript\Json;
16+
17+
18+
/**
19+
* This class formats JSON strings into pretty JSON strings.
20+
*
21+
* @author Steve Sperandeo <[email protected]>
22+
*/
23+
class JsonFormatter{
24+
25+
26+
/**
27+
* Echos a string to the screen in a nice display of hex values and exits.
28+
*
29+
*
30+
* @param string $string
31+
*/
32+
static public function hexDump( $string ){
33+
34+
echo '<pre>';
35+
for( $position = 0; $position < strlen($string); $position++ ){
36+
37+
$character = $string[$position];
38+
if( $position != 0 ){
39+
echo " ";
40+
if( $position % 4 == 0 ){
41+
echo " ";
42+
}
43+
if( $position % 16 == 0 ){
44+
echo "\n";
45+
}
46+
}
47+
echo dechex(ord($character));
48+
}
49+
exit();
50+
51+
}
52+
53+
54+
/**
55+
* Formats a JSON string into a pretty JSON string.
56+
*
57+
* @todo Fix so text-qualified characters eg. ",{[" don't break formatting.
58+
*
59+
* @param string $json
60+
* @return string
61+
*/
62+
static public function format( $json ){
63+
64+
$indent_character = ' ';
65+
$indent_count = 0;
66+
$json_length = strlen($json);
67+
68+
$position = 0;
69+
70+
$insert_newline_after = function( $offset ) use ( &$json, &$indent_character, &$indent_count, &$position ){
71+
$indent = str_repeat($indent_character, $indent_count);
72+
$json = \Altumo\String\String::insert( "\n" . $indent, $json, $offset + 1 );
73+
$position += strlen($indent) + 1;
74+
};
75+
76+
for( ; $position < $json_length; $position++ ){
77+
78+
$character = $json[$position];
79+
switch( $character ){
80+
81+
case ':':
82+
//insert a space after colons
83+
$json = \Altumo\String\String::insert( " ", $json, $position + 1 );
84+
$position++;
85+
break;
86+
87+
case ',':
88+
$insert_newline_after($position);
89+
break;
90+
91+
case '[':
92+
case '{':
93+
$indent_count++;
94+
$insert_newline_after($position);
95+
break;
96+
97+
case ']':
98+
case '}':
99+
$indent_count--;
100+
$insert_newline_after($position - 1);
101+
102+
break;
103+
104+
default:
105+
106+
};
107+
$json_length = strlen($json);
108+
109+
}
110+
111+
return $json;
112+
113+
}
114+
115+
116+
}
117+

source/php/String/String.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Altumo library.
5+
*
6+
* (c) Steve Sperandeo <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Altumo\String;
13+
14+
15+
16+
17+
/**
18+
* This class contains a number of string helper functions.
19+
*
20+
* @author Steve Sperandeo <[email protected]>
21+
*/
22+
class String{
23+
24+
/**
25+
* Inserts one string ($addition) into another ($destination) at a given
26+
* string $offset.
27+
*
28+
* @param string $addition //new string to add into $destination
29+
* @param string $destination //existing string that will contain $addition
30+
* @param integer $offset //offset in $destination that we will place $addition
31+
*
32+
* @see http://forums.digitalpoint.com/showthread.php?t=182666#post1785645
33+
*
34+
* @return string
35+
*/
36+
static public function insert( $addition, $destination, $offset ){
37+
$left = substr( $destination, 0, $offset );
38+
$right = substr( $destination, $offset );
39+
return $left . $addition . $right;
40+
}
41+
42+
}
43+
44+

0 commit comments

Comments
 (0)