Skip to content

Commit

Permalink
Make the PHP formatter output type aware data (Fix #39)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Oct 11, 2016
1 parent 50bc4c4 commit d09c140
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 43 deletions.
51 changes: 27 additions & 24 deletions Tests/format/PhpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class PhpTest extends \PHPUnit_Framework_TestCase
/**
* @testdox A data object is converted to a string
*
* @covers Joomla\Registry\Format\Php::formatValue
* @covers Joomla\Registry\Format\Php::getArrayString
* @covers Joomla\Registry\Format\Php::objectToString
*/
Expand All @@ -39,12 +40,12 @@ public function testADataObjectIsConvertedToAString()
"class myClass {\n" .
"\tpublic \$foo = 'bar';\n" .
"\tpublic \$quoted = '\"stringwithquotes\"';\n" .
"\tpublic \$booleantrue = '1';\n" .
"\tpublic \$booleanfalse = '';\n" .
"\tpublic \$numericint = '42';\n" .
"\tpublic \$numericfloat = '3.1415';\n" .
"\tpublic \$section = array(\"key\" => \"value\");\n" .
"\tpublic \$array = array(\"nestedarray\" => array(\"test1\" => \"value1\"));\n" .
"\tpublic \$booleantrue = true;\n" .
"\tpublic \$booleanfalse = false;\n" .
"\tpublic \$numericint = 42;\n" .
"\tpublic \$numericfloat = 3.1415;\n" .
"\tpublic \$section = array('key' => 'value');\n" .
"\tpublic \$array = array('nestedarray' => array('test1' => 'value1'));\n" .
"}\n?>";

$this->assertSame($string, $class->objectToString($object, $options));
Expand All @@ -53,6 +54,7 @@ public function testADataObjectIsConvertedToAString()
/**
* @testdox A data object is converted to a string with no specified class
*
* @covers Joomla\Registry\Format\Php::formatValue
* @covers Joomla\Registry\Format\Php::getArrayString
* @covers Joomla\Registry\Format\Php::objectToString
*/
Expand All @@ -76,12 +78,12 @@ public function testADataObjectIsConvertedToAStringWithNoSpecifiedClass()
"class Registry {\n" .
"\tpublic \$foo = 'bar';\n" .
"\tpublic \$quoted = '\"stringwithquotes\"';\n" .
"\tpublic \$booleantrue = '1';\n" .
"\tpublic \$booleanfalse = '';\n" .
"\tpublic \$numericint = '42';\n" .
"\tpublic \$numericfloat = '3.1415';\n" .
"\tpublic \$section = array(\"key\" => \"value\");\n" .
"\tpublic \$array = array(\"nestedarray\" => array(\"test1\" => \"value1\"));\n" .
"\tpublic \$booleantrue = true;\n" .
"\tpublic \$booleanfalse = false;\n" .
"\tpublic \$numericint = 42;\n" .
"\tpublic \$numericfloat = 3.1415;\n" .
"\tpublic \$section = array('key' => 'value');\n" .
"\tpublic \$array = array('nestedarray' => array('test1' => 'value1'));\n" .
"}\n?>";

$this->assertSame($string, $class->objectToString($object));
Expand All @@ -90,6 +92,7 @@ public function testADataObjectIsConvertedToAStringWithNoSpecifiedClass()
/**
* @testdox A data object is converted to a string with a namespace
*
* @covers Joomla\Registry\Format\Php::formatValue
* @covers Joomla\Registry\Format\Php::getArrayString
* @covers Joomla\Registry\Format\Php::objectToString
*/
Expand All @@ -116,12 +119,12 @@ public function testADataObjectIsConvertedToAStringWithANamespace()
"class myClass {\n" .
"\tpublic \$foo = 'bar';\n" .
"\tpublic \$quoted = '\"stringwithquotes\"';\n" .
"\tpublic \$booleantrue = '1';\n" .
"\tpublic \$booleanfalse = '';\n" .
"\tpublic \$numericint = '42';\n" .
"\tpublic \$numericfloat = '3.1415';\n" .
"\tpublic \$section = array(\"key\" => \"value\");\n" .
"\tpublic \$array = array(\"nestedarray\" => array(\"test1\" => \"value1\"));\n" .
"\tpublic \$booleantrue = true;\n" .
"\tpublic \$booleanfalse = false;\n" .
"\tpublic \$numericint = 42;\n" .
"\tpublic \$numericfloat = 3.1415;\n" .
"\tpublic \$section = array('key' => 'value');\n" .
"\tpublic \$array = array('nestedarray' => array('test1' => 'value1'));\n" .
"}\n?>";

$this->assertSame($string, $class->objectToString($object, $options));
Expand Down Expand Up @@ -156,12 +159,12 @@ public function testDataEqualityInConvertedObjects()
"class myClass {\n" .
"\tpublic \$foo = 'bar';\n" .
"\tpublic \$quoted = '\"stringwithquotes\"';\n" .
"\tpublic \$booleantrue = '1';\n" .
"\tpublic \$booleanfalse = '';\n" .
"\tpublic \$numericint = '42';\n" .
"\tpublic \$numericfloat = '3.1415';\n" .
"\tpublic \$section = array(\"key\" => \"value\");\n" .
"\tpublic \$array = array(\"nestedarray\" => array(\"test1\" => \"value1\"));\n" .
"\tpublic \$booleantrue = true;\n" .
"\tpublic \$booleanfalse = false;\n" .
"\tpublic \$numericint = 42;\n" .
"\tpublic \$numericfloat = 3.1415;\n" .
"\tpublic \$section = array('key' => 'value');\n" .
"\tpublic \$array = array('nestedarray' => array('test1' => 'value1'));\n" .
"}\n?>";

$object = $class->stringToObject($input);
Expand Down
52 changes: 33 additions & 19 deletions src/Format/Php.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,7 @@ public function objectToString($object, array $params = [])

foreach (get_object_vars($object) as $k => $v)
{
if (is_scalar($v))
{
$vars .= "\tpublic $" . $k . " = '" . addcslashes($v, '\\\'') . "';\n";
}
elseif (is_array($v) || is_object($v))
{
$vars .= "\tpublic $" . $k . " = " . $this->getArrayString((array) $v) . ";\n";
}
$vars .= "\tpublic \$$k = " . $this->formatValue($v) . ";\n";
}

$str = "<?php\n";
Expand All @@ -56,7 +49,7 @@ public function objectToString($object, array $params = [])
$str .= "namespace " . $params['namespace'] . ";\n\n";
}

$str .= "class " . $class . " {\n";
$str .= "class $class {\n";
$str .= $vars;
$str .= "}";

Expand Down Expand Up @@ -84,6 +77,35 @@ public function stringToObject($data, array $options = [])
return true;
}

/**
* Format a value for the string conversion
*
* @param mixed $value The value to format
*
* @return mixed The formatted value
*
* @since __DEPLOY_VERSION__
*/
protected function formatValue($value)
{
switch (gettype($value))
{
case 'string':
return "'" . addcslashes($value, '\\\'') . "'";

case 'array':
case 'object':
return $this->getArrayString((array) $value);

case 'double':
case 'integer':
return $value;

case 'boolean':
return $value ? 'true' : 'false';
}
}

/**
* Method to get an array as an exported string.
*
Expand All @@ -101,16 +123,8 @@ protected function getArrayString($a)
foreach ($a as $k => $v)
{
$s .= ($i) ? ', ' : '';
$s .= '"' . $k . '" => ';

if (is_array($v) || is_object($v))
{
$s .= $this->getArrayString((array) $v);
}
else
{
$s .= '"' . addslashes($v) . '"';
}
$s .= "'" . addcslashes($k, '\\\'') . "' => ";
$s .= $this->formatValue($v);

$i++;
}
Expand Down

0 comments on commit d09c140

Please sign in to comment.