JSON encode/decode, plus abstract BufferedFormatter

Retab all the things.
This commit is contained in:
Adam Backstrom 2013-07-25 11:54:27 -04:00
parent 70770d7cac
commit 2802ad66df
3 changed files with 201 additions and 143 deletions

View File

@ -143,6 +143,36 @@ class ActionUnserialize extends AbstractAction
}
}
class ActionJsonDecode extends AbstractAction
{
public function __construct( $value )
{
parent::__construct( $value );
$this->setContainer( new DivContainer );
$this->setFormatter( new VarexportFormatter );
}
public function decode()
{
return json_decode( $this->value, true );
}
}
class ActionJsonEncode extends AbstractAction
{
public function __construct( $value )
{
parent::__construct( $value );
$this->setContainer( new TextareaContainer );
$this->setFormatter( new EchoFormatter );
}
public function decode()
{
return json_encode( $this->value );
}
}
interface ContainerInterface
{
public function wrap( $contents );
@ -169,23 +199,39 @@ interface FormatterInterface
public function format( $value );
}
class DbugFormatter implements FormatterInterface
{
public function format( $value )
{
abstract class BufferedFormatter implements FormatterInterface {
const ESCAPE = false;
public function format( $value ) {
ob_start();
$this->bufferedFormat( $value );
$output = ob_get_clean();
if (static::ESCAPE) {
$output = '<pre>' . htmlentities($output) . '</pre>';
}
return $output;
}
abstract public function bufferedFormat( $value );
}
class DbugFormatter extends BufferedFormatter
{
public function bufferedFormat( $value )
{
new dBug( $value );
return ob_get_clean();
}
}
class VardumpFormatter implements FormatterInterface
class VardumpFormatter extends BufferedFormatter
{
public function format( $value )
const ESCAPE = true;
public function bufferedFormat( $value )
{
ob_start();
var_dump( $value );
return ob_get_clean();
}
}
@ -196,3 +242,13 @@ class EchoFormatter implements FormatterInterface
return $value;
}
}
class VarexportFormatter extends BufferedFormatter
{
const ESCAPE = true;
public function bufferedFormat( $value )
{
var_export( $value );
}
}

View File

@ -41,6 +41,8 @@ if( 'POST' === $_SERVER['REQUEST_METHOD'] ) {
<option value="Unserialize" <?php echo selected($action_str, 'Unserialize'); ?>>unserialize()</option>
<option value="Base64Decode" <?php echo selected($action_str, 'Base64Decode'); ?>>base64_decode()</option>
<option value="Base64Encode" <?php echo selected($action_str, 'Base64Encode'); ?>>base64_encode()</option>
<option value="JsonDecode" <?php echo selected($action_str, 'JsonDecode'); ?>>json_decode()</option>
<option value="JsonEncode" <?php echo selected($action_str, 'JsonEncode'); ?>>json_encode()</option>
<select>
<input type="submit">
</form>

View File

@ -1,6 +1,6 @@
body {
font-family: Verdana, Arial, sans-serif;
font-size: 67.5%;
font-size: 12px;
}
textarea {