Browse Source

Allow setting `_serialize` to true.

Doing so will serialize all available view variables.
ADmad 11 years ago
parent
commit
e3d8e82ae3
2 changed files with 50 additions and 17 deletions
  1. 48 15
      src/View/JsonView.php
  2. 2 2
      tests/TestCase/View/JsonViewTest.php

+ 48 - 15
src/View/JsonView.php

@@ -48,6 +48,8 @@ use Cake\Network\Response;
  *
  * `{"posts": [...], "users": [...]}`
  *
+ * You can also set `_serialize` to `true` to serialize all view variables.
+ *
  * If you don't use the `_serialize` key, you will need a view. You can use extended
  * views to provide layout-like functionality.
  *
@@ -111,6 +113,7 @@ class JsonView extends View
      * ### Special parameters
      * `_serialize` To convert a set of view variables into a JSON response.
      *   Its value can be a string for single variable name or array for multiple names.
+     *   It can also be set to true to serialize all view variables.
      *   You can omit the`_serialize` parameter, and use a normal view + layout as well.
      * `_jsonp` Enables JSONP support and wraps response in callback function provided in query string.
      *   - Setting it to true enables the default query string parameter "callback".
@@ -151,25 +154,13 @@ class JsonView extends View
      * `_jsonOptions` You can set custom options for json_encode() this way,
      *   e.g. `JSON_HEX_TAG | JSON_HEX_APOS`.
      *
-     * @param array|string $serialize The name(s) of the view variable(s) that need(s) to be serialized
+     * @param array|string|bool $serialize The name(s) of the view variable(s)
+     *   that need(s) to be serialized. If true all available view variables.
      * @return string The serialized data
      */
     protected function _serialize($serialize)
     {
-        if (is_array($serialize)) {
-            $data = [];
-            foreach ($serialize as $alias => $key) {
-                if (is_numeric($alias)) {
-                    $alias = $key;
-                }
-                if (array_key_exists($key, $this->viewVars)) {
-                    $data[$alias] = $this->viewVars[$key];
-                }
-            }
-            $data = !empty($data) ? $data : null;
-        } else {
-            $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
-        }
+        $data = $this->_dataToSerialize($serialize);
 
         $jsonOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
         if (isset($this->viewVars['_jsonOptions'])) {
@@ -185,4 +176,46 @@ class JsonView extends View
         }
         return json_encode($data, $jsonOptions);
     }
+
+    /**
+     * Returns data to be serialized
+     *
+     * @param array|string|bool $serialize The name(s) of the view variable(s)
+     *   that need(s) to be serialized. If true all available view variables.
+     * @return mixe The data to serialize
+     */
+    protected function _dataToSerialize($serialize)
+    {
+        if ($serialize === true) {
+            $data = array_diff_key(
+                $this->viewVars,
+                ['_serialize' => null, '_jsonOptions' => null, '_jsonp' => null]
+            );
+
+            if (empty($data)) {
+                return null;
+            }
+
+            if (count($data) === 1) {
+                return current($data);
+            }
+
+            return $data;
+        }
+
+        if (is_array($serialize)) {
+            $data = [];
+            foreach ($serialize as $alias => $key) {
+                if (is_numeric($alias)) {
+                    $alias = $key;
+                }
+                if (array_key_exists($key, $this->viewVars)) {
+                    $data[$alias] = $this->viewVars[$key];
+                }
+            }
+            return !empty($data) ? $data : null;
+        }
+
+        return isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
+    }
 }

+ 2 - 2
tests/TestCase/View/JsonViewTest.php

@@ -115,8 +115,8 @@ class JsonViewTest extends TestCase
             [
                 ['no' => 'nope', 'user' => 'fake', 'list' => ['item1', 'item2']],
                 true,
-                null,
-                json_encode(null)
+                JSON_HEX_QUOT,
+                json_encode(['no' => 'nope', 'user' => 'fake', 'list' => ['item1', 'item2']])
             ],
 
             // Test render with empty string in _serialize.