Skip to content

Commit

Permalink
t push origin masterMerge branch 'richardkmiller-handle_stdclass'
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Sep 30, 2015
2 parents 6f4763f + 57a32de commit 07795d3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,16 @@ function it_transforms_null_values()
->shouldMatch('/window.age = null;window.sum = null;/');
}

function it_throws_an_exception_if_an_object_cant_be_transformed(\StdClass $obj)
function it_throws_an_exception_if_an_object_cant_be_transformed(\Laracasts\Utilities\JavaScript\PHPToJavaScriptTransformer $obj)
{
$this->shouldThrow('Exception')
->duringBuildJavaScriptSyntax(['foo' => $obj]);
}

function it_does_not_throw_an_exception_for_stdClass(\StdClass $obj)
{
$this->buildJavaScriptSyntax(['foo' => $obj])
->shouldMatch('/window.window = window.window || {};/');
}

}
16 changes: 15 additions & 1 deletion src/PHPToJavaScriptTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laracasts\Utilities\JavaScript;

use Exception;
use stdClass;

class PHPToJavaScriptTransformer
{
Expand All @@ -28,6 +29,7 @@ class PHPToJavaScriptTransformer
protected $types = [
'String',
'Array',
'stdClass',
'Object',
'Numeric',
'Boolean',
Expand Down Expand Up @@ -190,7 +192,19 @@ protected function transformBoolean($value)
*
* @param object $value
* @return string
* @throws Exception
*/
protected function transformstdClass($value)
{
if ($value instanceof stdClass)
{
return json_encode($value);
}
}

/**
* @param $value
* @return string
* @throws \Exception
*/
protected function transformObject($value)
{
Expand Down

0 comments on commit 07795d3

Please sign in to comment.