From 2802ad66dfc9ea41e783ec5ad8489a257a10959c Mon Sep 17 00:00:00 2001 From: Adam Backstrom Date: Thu, 25 Jul 2013 11:54:27 -0400 Subject: [PATCH] JSON encode/decode, plus abstract BufferedFormatter Retab all the things. --- classes.php | 306 +++++++++++++++++++++++++++++++--------------------- index.php | 10 +- style.css | 28 ++--- 3 files changed, 201 insertions(+), 143 deletions(-) diff --git a/classes.php b/classes.php index d863df9..af21eba 100644 --- a/classes.php +++ b/classes.php @@ -2,197 +2,253 @@ abstract class AbstractAction { - protected $value; + protected $value; - protected $container; + protected $container; - protected $formatter; + protected $formatter; - public function __construct( $value ) - { - $this->value = $value; - } + public function __construct( $value ) + { + $this->value = $value; + } - public function setContainer( ContainerInterface $container ) - { - $this->container = $container; - } + public function setContainer( ContainerInterface $container ) + { + $this->container = $container; + } - public function setFormatter( FormatterInterface $formatter ) - { - $this->formatter = $formatter; - } + public function setFormatter( FormatterInterface $formatter ) + { + $this->formatter = $formatter; + } - public function esc_raw() - { - return htmlentities( $this->value ); - } + public function esc_raw() + { + return htmlentities( $this->value ); + } - abstract public function decode(); + abstract public function decode(); - public function __toString() - { - $value = $this->decode(); - $formatted = $this->formatter->format( $value ); - return $this->container->wrap( $formatted ); - } + public function __toString() + { + $value = $this->decode(); + $formatted = $this->formatter->format( $value ); + return $this->container->wrap( $formatted ); + } } class ActionQuotedPrintableDecode extends AbstractAction { - public function __construct( $value ) - { - parent::__construct( $value ); - $this->setContainer( new TextareaContainer ); - $this->setFormatter( new EchoFormatter ); - } + public function __construct( $value ) + { + parent::__construct( $value ); + $this->setContainer( new TextareaContainer ); + $this->setFormatter( new EchoFormatter ); + } - public function decode() - { - return quoted_printable_decode( $this->value ); - } + public function decode() + { + return quoted_printable_decode( $this->value ); + } } class ActionQuotedPrintableEncode extends AbstractAction { - public function __construct( $value ) - { - parent::__construct( $value ); - $this->setContainer( new TextareaContainer ); - $this->setFormatter( new EchoFormatter ); - } + public function __construct( $value ) + { + parent::__construct( $value ); + $this->setContainer( new TextareaContainer ); + $this->setFormatter( new EchoFormatter ); + } - public function decode() - { - return quoted_printable_encode( $this->value ); - } + public function decode() + { + return quoted_printable_encode( $this->value ); + } } class ActionBase64Encode extends AbstractAction { - public function __construct( $value ) - { - parent::__construct( $value ); - $this->setContainer( new TextareaContainer ); - $this->setFormatter( new EchoFormatter ); - } + public function __construct( $value ) + { + parent::__construct( $value ); + $this->setContainer( new TextareaContainer ); + $this->setFormatter( new EchoFormatter ); + } - public function decode() - { - return base64_encode( $this->value ); - } + public function decode() + { + return base64_encode( $this->value ); + } } class ActionBase64Decode extends AbstractAction { - public function __construct( $value ) - { - parent::__construct( $value ); - $this->setContainer( new TextareaContainer ); - $this->setFormatter( new EchoFormatter ); - } + public function __construct( $value ) + { + parent::__construct( $value ); + $this->setContainer( new TextareaContainer ); + $this->setFormatter( new EchoFormatter ); + } - public function decode() - { - return base64_decode( $this->value ); - } + public function decode() + { + return base64_decode( $this->value ); + } } class ActionUrldecode extends AbstractAction { - public function __construct( $value ) - { - parent::__construct( $value ); - $this->setContainer( new TextareaContainer ); - $this->setFormatter( new EchoFormatter ); - } + public function __construct( $value ) + { + parent::__construct( $value ); + $this->setContainer( new TextareaContainer ); + $this->setFormatter( new EchoFormatter ); + } - public function decode() - { - return urldecode( $this->value ); - } + public function decode() + { + return urldecode( $this->value ); + } } class ActionUrlencode extends AbstractAction { - public function __construct( $value ) - { - parent::__construct( $value ); - $this->setContainer( new TextareaContainer ); - $this->setFormatter( new EchoFormatter ); - } + public function __construct( $value ) + { + parent::__construct( $value ); + $this->setContainer( new TextareaContainer ); + $this->setFormatter( new EchoFormatter ); + } - public function decode() - { - return urlencode( $this->value ); - } + public function decode() + { + return urlencode( $this->value ); + } } class ActionUnserialize extends AbstractAction { - public function __construct( $value ) - { - parent::__construct( $value ); - $this->setContainer( new DivContainer ); - $this->setFormatter( new DbugFormatter ); - } + public function __construct( $value ) + { + parent::__construct( $value ); + $this->setContainer( new DivContainer ); + $this->setFormatter( new DbugFormatter ); + } - public function decode() - { - return unserialize( $this->value ); - } + public function decode() + { + 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 { - public function wrap( $contents ); + public function wrap( $contents ); } class DivContainer implements ContainerInterface { - public function wrap( $contents ) - { - return '
' . $contents . '
'; - } + public function wrap( $contents ) + { + return '
' . $contents . '
'; + } } class TextareaContainer extends DivContainer { - public function wrap( $contents ) - { - return parent::wrap( '' ); - } + public function wrap( $contents ) + { + return parent::wrap( '' ); + } } interface FormatterInterface { - public function format( $value ); + public function format( $value ); } -class DbugFormatter implements FormatterInterface -{ - public function format( $value ) - { - ob_start(); - new dBug( $value ); - return ob_get_clean(); - } +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 = '
' . htmlentities($output) . '
'; + } + + return $output; + } + + abstract public function bufferedFormat( $value ); } -class VardumpFormatter implements FormatterInterface +class DbugFormatter extends BufferedFormatter { - public function format( $value ) - { - ob_start(); - var_dump( $value ); - return ob_get_clean(); - } + public function bufferedFormat( $value ) + { + new dBug( $value ); + } +} + +class VardumpFormatter extends BufferedFormatter +{ + const ESCAPE = true; + + public function bufferedFormat( $value ) + { + var_dump( $value ); + } } class EchoFormatter implements FormatterInterface { - public function format( $value ) - { - return $value; - } + public function format( $value ) + { + return $value; + } +} + +class VarexportFormatter extends BufferedFormatter +{ + const ESCAPE = true; + + public function bufferedFormat( $value ) + { + var_export( $value ); + } } diff --git a/index.php b/index.php index da663fe..d742098 100644 --- a/index.php +++ b/index.php @@ -37,10 +37,12 @@ if( 'POST' === $_SERVER['REQUEST_METHOD'] ) { - - - - + + + + + + diff --git a/style.css b/style.css index 434ab2e..2d08a29 100644 --- a/style.css +++ b/style.css @@ -1,28 +1,28 @@ body { - font-family: Verdana, Arial, sans-serif; - font-size: 67.5%; + font-family: Verdana, Arial, sans-serif; + font-size: 12px; } textarea { - font-family: Inconsolata, Consolas, Monaco, monospace; - font-size: 1.2em; - white-space: pre; - word-wrap: normal; - height: 200px; - width: 100%; + font-family: Inconsolata, Consolas, Monaco, monospace; + font-size: 1.2em; + white-space: pre; + word-wrap: normal; + height: 200px; + width: 100%; } h2 { - clear: both; + clear: both; } .col { - margin-right: 1.8%; - width: 45%; - float: left; - padding: 10px; + margin-right: 1.8%; + width: 45%; + float: left; + padding: 10px; } .randomness-box { - width: 30em; + width: 30em; }