OBJECT ALL THE THINGS

This commit is contained in:
Adam Backstrom 2012-05-23 11:18:49 -04:00
parent 15eaef804f
commit b67f1bafb7
3 changed files with 201 additions and 6 deletions

153
classes.php Normal file
View File

@ -0,0 +1,153 @@
<?php
abstract class AbstractAction
{
protected $value;
protected $container;
protected $formatter;
public function __construct( $value )
{
$this->value = $value;
}
public function setContainer( ContainerInterface $container )
{
$this->container = $container;
}
public function setFormatter( FormatterInterface $formatter )
{
$this->formatter = $formatter;
}
public function esc_raw()
{
return nl2br( htmlentities( $this->value ) );
}
abstract public function decode();
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 decode()
{
return quoted_printable_decode( $this->value );
}
}
class ActionUrldecode extends AbstractAction
{
public function __construct( $value )
{
parent::__construct( $value );
$this->setContainer( new TextareaContainer );
$this->setFormatter( new EchoFormatter );
}
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 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 decode()
{
return unserialize( $this->value );
}
}
interface ContainerInterface
{
public function wrap( $contents );
}
class DivContainer implements ContainerInterface
{
public function wrap( $contents )
{
return '<div class="col">' . $contents . '</div>';
}
}
class TextareaContainer extends DivContainer
{
public function wrap( $contents )
{
return parent::wrap( '<textarea>' . $contents . '</textarea>' );
}
}
interface FormatterInterface
{
public function format( $value );
}
class DbugFormatter implements FormatterInterface
{
public function format( $value )
{
ob_start();
new dBug( $value );
return ob_get_clean();
}
}
class VardumpFormatter implements FormatterInterface
{
public function format( $value )
{
ob_start();
var_dump( $value );
return ob_get_clean();
}
}
class EchoFormatter implements FormatterInterface
{
public function format( $value )
{
return $value;
}
}

19
functions.php Normal file
View File

@ -0,0 +1,19 @@
<?php
// http://core.trac.wordpress.org/browser/tags/3.3.2/wp-includes/general-template.php
function selected( $selected, $current = true, $echo = true ) {
return __checked_selected_helper( $selected, $current, $echo, 'selected' );
}
// http://core.trac.wordpress.org/browser/tags/3.3.2/wp-includes/general-template.php
function __checked_selected_helper( $helper, $current, $echo, $type ) {
if ( (string) $helper === (string) $current )
$result = " $type='$type'";
else
$result = '';
if ( $echo )
echo $result;
return $result;
}

View File

@ -4,11 +4,16 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<title></title> <title></title>
<style> <style>
.serialized { body {
font-family: Verdana, Arial, sans-serif;
font-size: 67.5%;
}
textarea {
height: 200px; height: 200px;
width: 100%; width: 100%;
} }
.cols { h2 {
clear: both;
} }
.col { .col {
margin-right: 1.8%; margin-right: 1.8%;
@ -23,6 +28,8 @@
<?php <?php
require_once 'dBug.php'; require_once 'dBug.php';
require_once 'classes.php';
require_once 'functions.php';
?> ?>
@ -30,16 +37,32 @@ require_once 'dBug.php';
<h2>Expand Serialized</h2> <h2>Expand Serialized</h2>
<?php
$action = null;
if( 'POST' === $_SERVER['REQUEST_METHOD'] ) {
$class = "Action" . $_POST['action'];
$action = new $class( $_POST['data'] );
}
?>
<div class="cols"> <div class="cols">
<div class="col"> <div class="col">
<form method="post"> <form method="post">
<textarea class="serialized" name="serialized"><?php echo nl2br(htmlentities($_POST['serialized'])); ?></textarea><br> <textarea class="data" name="data"><?php if( $action ) echo $action->esc_raw(); ?></textarea><br>
<select name="action">
<option value="QuotedPrintableDecode" <?php echo selected($_POST['action'], 'QuotedPrintableDecode'); ?>>quoted_printable_decode()</option>
<option value="Urlencode" <?php echo selected($_POST['action'], 'Urlencode'); ?>>urlencode()</option>
<option value="Urldecode"<?php echo selected($_POST['action'], 'Urldecode'); ?>>urldecode()</option>
<option value="Unserialize"<?php echo selected($_POST['action'], 'Unserialize'); ?>>unserialize()</option>
<select>
<input type="submit"> <input type="submit">
</form> </form>
</div> </div>
<div class="col"><?php if( isset($_POST['serialized']) ) new dBug(unserialize($_POST['serialized'])); ?></div> <?php if( $action ) echo $action; ?>
</div> </div>
</body> </body>
</html> </html>