Skip to content

Commit

Permalink
Refactor a couple things
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Oct 1, 2015
1 parent 3575625 commit 4402a0e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ function it_is_initializable()
$this->shouldHaveType('Laracasts\Utilities\JavaScript\PHPToJavaScriptTransformer');
}

function it_nests_all_vars_under_namespace()
function it_uses_the_window_as_the_root_namespace_by_default()
{
// defaulting to window
$this->buildJavaScriptSyntax([])
->shouldMatch('/window.window = window.window || {};/');
->shouldEqual('');
}

function if_another_namespace_is_provided_it_will_use_that_instead(ViewBinder $viewBinder)
{
$this->beConstructedWith($viewBinder, 'Namespace');

$this->buildJavaScriptSyntax([])
->shouldEqual('window.Namespace = window.Namespace || {};');
}

function it_translates_an_array_of_key_value_pairs_to_javascript()
Expand Down
60 changes: 26 additions & 34 deletions src/PHPToJavaScriptTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Laracasts\Utilities\JavaScript;

use Exception;
use stdClass;
use Exception;
use JsonSerializable;

class PHPToJavaScriptTransformer
{
Expand All @@ -29,7 +30,6 @@ class PHPToJavaScriptTransformer
protected $types = [
'String',
'Array',
'stdClass',
'Object',
'Numeric',
'Boolean',
Expand Down Expand Up @@ -100,6 +100,10 @@ public function buildJavaScriptSyntax(array $vars)
*/
protected function buildNamespaceDeclaration()
{
if ($this->namespace == 'window') {
return '';
}

return "window.{$this->namespace} = window.{$this->namespace} || {};";
}

Expand All @@ -125,7 +129,8 @@ protected function buildVariableInitialization($key, $value)
protected function optimizeValueForJavaScript($value)
{
// For every transformable type, let's see if
// it needs to be converted for JS-used.
// it needs to be transformed for JS-use.

foreach ($this->types as $transformer) {
$js = $this->{"transform{$transformer}"}($value);

Expand Down Expand Up @@ -188,46 +193,33 @@ protected function transformBoolean($value)
}

/**
* Transform an object.
*
* @param object $value
* @return string
*/
protected function transformstdClass($value)
{
if ($value instanceof stdClass)
{
return json_encode($value);
}
}

/**
* @param $value
* @return string
* @throws \Exception
* @throws Exception
*/
protected function transformObject($value)
{
if (is_object($value)) {
if ($value instanceof \JsonSerializable) {
return json_encode($value);
}

// If a toJson() method exists, we'll assume that
// the object can cast itself automatically.
if (method_exists($value, 'toJson')) {
return $value;
}
if ( ! is_object($value)) {
return;
}

if ($value instanceof JsonSerializable || $value instanceof StdClass) {
return json_encode($value);
}

// Otherwise, if the object doesn't even have
// a toString method, we can't proceed.
if ( ! method_exists($value, '__toString')) {
throw new Exception('The provided object needs a __toString() method.');
}
// If a toJson() method exists, we'll assume that
// the object can cast itself automatically.
if (method_exists($value, 'toJson')) {
return $value;
}

return "'{$value}'";
// Otherwise, if the object doesn't even have a
// __toString() method, we can't proceed.
if ( ! method_exists($value, '__toString')) {
throw new Exception('Cannot transform this object to JavaScript.');
}

return "'{$value}'";
}

/**
Expand Down

0 comments on commit 4402a0e

Please sign in to comment.