From 4402a0ed774f8eb36ea7ba169341d9d5b6049378 Mon Sep 17 00:00:00 2001 From: Jeffrey Way Date: Thu, 1 Oct 2015 01:16:28 -0400 Subject: [PATCH] Refactor a couple things --- .../PHPToJavaScriptTransformerSpec.php | 13 +++- src/PHPToJavaScriptTransformer.php | 60 ++++++++----------- 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/spec/Laracasts/Utilities/JavaScript/PHPToJavaScriptTransformerSpec.php b/spec/Laracasts/Utilities/JavaScript/PHPToJavaScriptTransformerSpec.php index 3a4ccb9..f989a6c 100644 --- a/spec/Laracasts/Utilities/JavaScript/PHPToJavaScriptTransformerSpec.php +++ b/spec/Laracasts/Utilities/JavaScript/PHPToJavaScriptTransformerSpec.php @@ -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() diff --git a/src/PHPToJavaScriptTransformer.php b/src/PHPToJavaScriptTransformer.php index abe1b39..57a6183 100644 --- a/src/PHPToJavaScriptTransformer.php +++ b/src/PHPToJavaScriptTransformer.php @@ -2,8 +2,9 @@ namespace Laracasts\Utilities\JavaScript; -use Exception; use stdClass; +use Exception; +use JsonSerializable; class PHPToJavaScriptTransformer { @@ -29,7 +30,6 @@ class PHPToJavaScriptTransformer protected $types = [ 'String', 'Array', - 'stdClass', 'Object', 'Numeric', 'Boolean', @@ -100,6 +100,10 @@ public function buildJavaScriptSyntax(array $vars) */ protected function buildNamespaceDeclaration() { + if ($this->namespace == 'window') { + return ''; + } + return "window.{$this->namespace} = window.{$this->namespace} || {};"; } @@ -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); @@ -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}'"; } /**