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

@ -2,197 +2,253 @@
abstract class AbstractAction abstract class AbstractAction
{ {
protected $value; protected $value;
protected $container; protected $container;
protected $formatter; protected $formatter;
public function __construct( $value ) public function __construct( $value )
{ {
$this->value = $value; $this->value = $value;
} }
public function setContainer( ContainerInterface $container ) public function setContainer( ContainerInterface $container )
{ {
$this->container = $container; $this->container = $container;
} }
public function setFormatter( FormatterInterface $formatter ) public function setFormatter( FormatterInterface $formatter )
{ {
$this->formatter = $formatter; $this->formatter = $formatter;
} }
public function esc_raw() public function esc_raw()
{ {
return htmlentities( $this->value ); return htmlentities( $this->value );
} }
abstract public function decode(); abstract public function decode();
public function __toString() public function __toString()
{ {
$value = $this->decode(); $value = $this->decode();
$formatted = $this->formatter->format( $value ); $formatted = $this->formatter->format( $value );
return $this->container->wrap( $formatted ); return $this->container->wrap( $formatted );
} }
} }
class ActionQuotedPrintableDecode extends AbstractAction class ActionQuotedPrintableDecode extends AbstractAction
{ {
public function __construct( $value ) public function __construct( $value )
{ {
parent::__construct( $value ); parent::__construct( $value );
$this->setContainer( new TextareaContainer ); $this->setContainer( new TextareaContainer );
$this->setFormatter( new EchoFormatter ); $this->setFormatter( new EchoFormatter );
} }
public function decode() public function decode()
{ {
return quoted_printable_decode( $this->value ); return quoted_printable_decode( $this->value );
} }
} }
class ActionQuotedPrintableEncode extends AbstractAction class ActionQuotedPrintableEncode extends AbstractAction
{ {
public function __construct( $value ) public function __construct( $value )
{ {
parent::__construct( $value ); parent::__construct( $value );
$this->setContainer( new TextareaContainer ); $this->setContainer( new TextareaContainer );
$this->setFormatter( new EchoFormatter ); $this->setFormatter( new EchoFormatter );
} }
public function decode() public function decode()
{ {
return quoted_printable_encode( $this->value ); return quoted_printable_encode( $this->value );
} }
} }
class ActionBase64Encode extends AbstractAction class ActionBase64Encode extends AbstractAction
{ {
public function __construct( $value ) public function __construct( $value )
{ {
parent::__construct( $value ); parent::__construct( $value );
$this->setContainer( new TextareaContainer ); $this->setContainer( new TextareaContainer );
$this->setFormatter( new EchoFormatter ); $this->setFormatter( new EchoFormatter );
} }
public function decode() public function decode()
{ {
return base64_encode( $this->value ); return base64_encode( $this->value );
} }
} }
class ActionBase64Decode extends AbstractAction class ActionBase64Decode extends AbstractAction
{ {
public function __construct( $value ) public function __construct( $value )
{ {
parent::__construct( $value ); parent::__construct( $value );
$this->setContainer( new TextareaContainer ); $this->setContainer( new TextareaContainer );
$this->setFormatter( new EchoFormatter ); $this->setFormatter( new EchoFormatter );
} }
public function decode() public function decode()
{ {
return base64_decode( $this->value ); return base64_decode( $this->value );
} }
} }
class ActionUrldecode extends AbstractAction class ActionUrldecode extends AbstractAction
{ {
public function __construct( $value ) public function __construct( $value )
{ {
parent::__construct( $value ); parent::__construct( $value );
$this->setContainer( new TextareaContainer ); $this->setContainer( new TextareaContainer );
$this->setFormatter( new EchoFormatter ); $this->setFormatter( new EchoFormatter );
} }
public function decode() public function decode()
{ {
return urldecode( $this->value ); return urldecode( $this->value );
} }
} }
class ActionUrlencode extends AbstractAction class ActionUrlencode extends AbstractAction
{ {
public function __construct( $value ) public function __construct( $value )
{ {
parent::__construct( $value ); parent::__construct( $value );
$this->setContainer( new TextareaContainer ); $this->setContainer( new TextareaContainer );
$this->setFormatter( new EchoFormatter ); $this->setFormatter( new EchoFormatter );
} }
public function decode() public function decode()
{ {
return urlencode( $this->value ); return urlencode( $this->value );
} }
} }
class ActionUnserialize extends AbstractAction class ActionUnserialize extends AbstractAction
{ {
public function __construct( $value ) public function __construct( $value )
{ {
parent::__construct( $value ); parent::__construct( $value );
$this->setContainer( new DivContainer ); $this->setContainer( new DivContainer );
$this->setFormatter( new DbugFormatter ); $this->setFormatter( new DbugFormatter );
} }
public function decode() public function decode()
{ {
return unserialize( $this->value ); return unserialize( $this->value );
} }
}
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 interface ContainerInterface
{ {
public function wrap( $contents ); public function wrap( $contents );
} }
class DivContainer implements ContainerInterface class DivContainer implements ContainerInterface
{ {
public function wrap( $contents ) public function wrap( $contents )
{ {
return '<div class="col">' . $contents . '</div>'; return '<div class="col">' . $contents . '</div>';
} }
} }
class TextareaContainer extends DivContainer class TextareaContainer extends DivContainer
{ {
public function wrap( $contents ) public function wrap( $contents )
{ {
return parent::wrap( '<textarea>' . htmlentities($contents) . '</textarea>' ); return parent::wrap( '<textarea>' . htmlentities($contents) . '</textarea>' );
} }
} }
interface FormatterInterface interface FormatterInterface
{ {
public function format( $value ); public function format( $value );
} }
class DbugFormatter implements FormatterInterface abstract class BufferedFormatter implements FormatterInterface {
{ const ESCAPE = false;
public function format( $value )
{ public function format( $value ) {
ob_start(); ob_start();
new dBug( $value ); $this->bufferedFormat( $value );
return ob_get_clean(); $output = ob_get_clean();
}
if (static::ESCAPE) {
$output = '<pre>' . htmlentities($output) . '</pre>';
}
return $output;
}
abstract public function bufferedFormat( $value );
} }
class VardumpFormatter implements FormatterInterface class DbugFormatter extends BufferedFormatter
{ {
public function format( $value ) public function bufferedFormat( $value )
{ {
ob_start(); new dBug( $value );
var_dump( $value ); }
return ob_get_clean(); }
}
class VardumpFormatter extends BufferedFormatter
{
const ESCAPE = true;
public function bufferedFormat( $value )
{
var_dump( $value );
}
} }
class EchoFormatter implements FormatterInterface class EchoFormatter implements FormatterInterface
{ {
public function format( $value ) public function format( $value )
{ {
return $value; return $value;
} }
}
class VarexportFormatter extends BufferedFormatter
{
const ESCAPE = true;
public function bufferedFormat( $value )
{
var_export( $value );
}
} }

View File

@ -37,10 +37,12 @@ if( 'POST' === $_SERVER['REQUEST_METHOD'] ) {
<option value="QuotedPrintableDecode" <?php echo selected($action_str, 'QuotedPrintableDecode'); ?>>quoted_printable_decode()</option> <option value="QuotedPrintableDecode" <?php echo selected($action_str, 'QuotedPrintableDecode'); ?>>quoted_printable_decode()</option>
<option value="QuotedPrintableEncode" <?php echo selected($action_str, 'QuotedPrintableEncode'); ?>>quoted_printable_encode()</option> <option value="QuotedPrintableEncode" <?php echo selected($action_str, 'QuotedPrintableEncode'); ?>>quoted_printable_encode()</option>
<option value="Urlencode" <?php echo selected($action_str, 'Urlencode'); ?>>urlencode()</option> <option value="Urlencode" <?php echo selected($action_str, 'Urlencode'); ?>>urlencode()</option>
<option value="Urldecode"<?php echo selected($action_str, 'Urldecode'); ?>>urldecode()</option> <option value="Urldecode" <?php echo selected($action_str, 'Urldecode'); ?>>urldecode()</option>
<option value="Unserialize"<?php echo selected($action_str, 'Unserialize'); ?>>unserialize()</option> <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="Base64Decode" <?php echo selected($action_str, 'Base64Decode'); ?>>base64_decode()</option>
<option value="Base64Encode"<?php echo selected($action_str, 'Base64Encode'); ?>>base64_encode()</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> <select>
<input type="submit"> <input type="submit">
</form> </form>

View File

@ -1,28 +1,28 @@
body { body {
font-family: Verdana, Arial, sans-serif; font-family: Verdana, Arial, sans-serif;
font-size: 67.5%; font-size: 12px;
} }
textarea { textarea {
font-family: Inconsolata, Consolas, Monaco, monospace; font-family: Inconsolata, Consolas, Monaco, monospace;
font-size: 1.2em; font-size: 1.2em;
white-space: pre; white-space: pre;
word-wrap: normal; word-wrap: normal;
height: 200px; height: 200px;
width: 100%; width: 100%;
} }
h2 { h2 {
clear: both; clear: both;
} }
.col { .col {
margin-right: 1.8%; margin-right: 1.8%;
width: 45%; width: 45%;
float: left; float: left;
padding: 10px; padding: 10px;
} }
.randomness-box { .randomness-box {
width: 30em; width: 30em;
} }