Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ansformer into spamoom-master
  • Loading branch information
JeffreyWay committed Mar 5, 2014
2 parents d10740e + e29325a commit f492120
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ function it_transforms_numerics()
$this->buildJavaScriptSyntax(['age' => 10, 'sum' => 10.12, 'dec' => 0.5])
->shouldMatch('/window.age = 10;window.sum = 10.12;window.dec = 0.5;/');
}

function it_transforms_zero_values()
{
$this->buildJavaScriptSyntax(['age' => 0, 'sum' => 10 - 10, 'dec' => 0.00])
->shouldMatch('/window.age = 0;window.sum = 0;window.dec = 0;/');
}

function it_transforms_null_values()
{
$this->buildJavaScriptSyntax(['age' => null, 'sum' => null])
->shouldMatch('/window.age = null;window.sum = null;/');
}

function it_throws_an_exception_if_an_object_cant_be_transformed(\StdClass $obj)
{
Expand Down
26 changes: 19 additions & 7 deletions src/Laracasts/Utilities/JavaScript/PHPToJavaScriptTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PHPToJavaScriptTransformer {
* @var array
*/
protected $types = [
'String', 'Array', 'Object', 'Numeric', 'Boolean'
'String', 'Array', 'Object', 'Numeric', 'Boolean', 'Null'
];

/**
Expand Down Expand Up @@ -111,7 +111,7 @@ protected function optimizeValueForJavaScript($value)
{
$js = $this->{"transform{$transformer}"}($value);

if ($js) return $js;
if (is_array($js)) return $js[0];
}
}

Expand All @@ -123,7 +123,7 @@ protected function transformString($value)
{
if (is_string($value))
{
return "'{$this->escape($value)}'";
return ["'{$this->escape($value)}'"];
}
}

Expand All @@ -135,7 +135,7 @@ protected function transformArray($value)
{
if (is_array($value))
{
return json_encode($value);
return [json_encode($value)];
}
}

Expand All @@ -147,7 +147,7 @@ protected function transformNumeric($value)
{
if (is_numeric($value))
{
return $value;
return [$value];
}
}

Expand All @@ -159,7 +159,7 @@ protected function transformBoolean($value)
{
if (is_bool($value))
{
return $value ? 'true' : 'false';
return [$value ? 'true' : 'false'];
}
}

Expand All @@ -183,7 +183,19 @@ protected function transformObject($value)
throw new Exception('The provided object needs a __toString() method.');
}

return "'{$value}'";
return ["'{$value}'"];
}
}

/**
* @param $value
* @return string
*/
protected function transformNull($value)
{
if (is_null($value))
{
return ['null'];
}
}

Expand Down

0 comments on commit f492120

Please sign in to comment.