Permalink
Browse files

move serialization to separate method, for easier overriding in subcl…

…asses
  • Loading branch information...
1 parent e5ad204 commit e2e42ee185f48d88350d7c060636ef009b8c0b3c @ceeram ceeram committed Aug 10, 2012
Showing with 41 additions and 26 deletions.
  1. +19 −12 lib/Cake/View/JsonView.php
  2. +22 −14 lib/Cake/View/XmlView.php
View
31 lib/Cake/View/JsonView.php
@@ -82,18 +82,7 @@ public function __construct(Controller $controller = null) {
*/
public function render($view = null, $layout = null) {
if (isset($this->viewVars['_serialize'])) {
- $serialize = $this->viewVars['_serialize'];
- if (is_array($serialize)) {
- $data = array();
- foreach ($serialize as $key) {
- $data[$key] = $this->viewVars[$key];
- }
- } else {
- $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
- }
- $content = json_encode($data);
- $this->Blocks->set('content', $content);
- return $content;
+ return $this->_serialize($this->viewVars['_serialize']);
}
if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
if (!$this->_helpersLoaded) {
@@ -105,4 +94,22 @@ public function render($view = null, $layout = null) {
}
}
+/**
+ * Serialize view vars
+ *
+ * @param array $serialize The viewVars that need to be serialized
+ * @return string The serialized data
+ */
+ protected function _serialize($serialize) {
+ if (is_array($serialize)) {
+ $data = array();
+ foreach ($serialize as $key) {
+ $data[$key] = $this->viewVars[$key];
+ }
+ } else {
+ $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
+ }
+ return json_encode($data);
+ }
+
}
View
36 lib/Cake/View/XmlView.php
@@ -85,20 +85,7 @@ public function __construct(Controller $controller = null) {
*/
public function render($view = null, $layout = null) {
if (isset($this->viewVars['_serialize'])) {
- $serialize = $this->viewVars['_serialize'];
- if (is_array($serialize)) {
- $data = array('response' => array());
- foreach ($serialize as $key) {
- $data['response'][$key] = $this->viewVars[$key];
- }
- } else {
- $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
- if (is_array($data) && Set::numeric(array_keys($data))) {
- $data = array('response' => array($serialize => $data));
- }
- }
- $content = Xml::fromArray($data)->asXML();
- return $content;
+ return $this->_serialize($this->viewVars['_serialize']);
}
if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
if (!$this->_helpersLoaded) {
@@ -110,4 +97,25 @@ public function render($view = null, $layout = null) {
}
}
+/**
+ * Serialize view vars
+ *
+ * @param array $serialize The viewVars that need to be serialized
+ * @return string The serialized data
+ */
+ protected function _serialize($serialize) {
+ if (is_array($serialize)) {
+ $data = array('response' => array());
+ foreach ($serialize as $key) {
+ $data['response'][$key] = $this->viewVars[$key];
+ }
+ } else {
+ $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
+ if (is_array($data) && Set::numeric(array_keys($data))) {
+ $data = array('response' => array($serialize => $data));
+ }
+ }
+ return Xml::fromArray($data)->asXML();
+ }
+
}

0 comments on commit e2e42ee

Please sign in to comment.