Skip to content
This repository
Browse code

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

…asses
  • Loading branch information...
commit e2e42ee185f48d88350d7c060636ef009b8c0b3c 1 parent e5ad204
ceeram authored
31  lib/Cake/View/JsonView.php
@@ -82,18 +82,7 @@ public function __construct(Controller $controller = null) {
82 82
  */
83 83
 	public function render($view = null, $layout = null) {
84 84
 		if (isset($this->viewVars['_serialize'])) {
85  
-			$serialize = $this->viewVars['_serialize'];
86  
-			if (is_array($serialize)) {
87  
-				$data = array();
88  
-				foreach ($serialize as $key) {
89  
-					$data[$key] = $this->viewVars[$key];
90  
-				}
91  
-			} else {
92  
-				$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
93  
-			}
94  
-			$content = json_encode($data);
95  
-			$this->Blocks->set('content', $content);
96  
-			return $content;
  85
+			return $this->_serialize($this->viewVars['_serialize']);
97 86
 		}
98 87
 		if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
99 88
 			if (!$this->_helpersLoaded) {
@@ -105,4 +94,22 @@ public function render($view = null, $layout = null) {
105 94
 		}
106 95
 	}
107 96
 
  97
+/**
  98
+ * Serialize view vars
  99
+ *
  100
+ * @param array $serialize The viewVars that need to be serialized
  101
+ * @return string The serialized data
  102
+ */
  103
+	protected function _serialize($serialize) {
  104
+		if (is_array($serialize)) {
  105
+			$data = array();
  106
+			foreach ($serialize as $key) {
  107
+				$data[$key] = $this->viewVars[$key];
  108
+			}
  109
+		} else {
  110
+			$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
  111
+		}
  112
+		return json_encode($data);
  113
+	}
  114
+
108 115
 }
36  lib/Cake/View/XmlView.php
@@ -85,20 +85,7 @@ public function __construct(Controller $controller = null) {
85 85
  */
86 86
 	public function render($view = null, $layout = null) {
87 87
 		if (isset($this->viewVars['_serialize'])) {
88  
-			$serialize = $this->viewVars['_serialize'];
89  
-			if (is_array($serialize)) {
90  
-				$data = array('response' => array());
91  
-				foreach ($serialize as $key) {
92  
-					$data['response'][$key] = $this->viewVars[$key];
93  
-				}
94  
-			} else {
95  
-				$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
96  
-				if (is_array($data) && Set::numeric(array_keys($data))) {
97  
-					$data = array('response' => array($serialize => $data));
98  
-				}
99  
-			}
100  
-			$content = Xml::fromArray($data)->asXML();
101  
-			return $content;
  88
+			return $this->_serialize($this->viewVars['_serialize']);
102 89
 		}
103 90
 		if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
104 91
 			if (!$this->_helpersLoaded) {
@@ -110,4 +97,25 @@ public function render($view = null, $layout = null) {
110 97
 		}
111 98
 	}
112 99
 
  100
+/**
  101
+ * Serialize view vars
  102
+ *
  103
+ * @param array $serialize The viewVars that need to be serialized
  104
+ * @return string The serialized data
  105
+ */
  106
+	protected function _serialize($serialize) {
  107
+		if (is_array($serialize)) {
  108
+			$data = array('response' => array());
  109
+			foreach ($serialize as $key) {
  110
+				$data['response'][$key] = $this->viewVars[$key];
  111
+			}
  112
+		} else {
  113
+			$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
  114
+			if (is_array($data) && Set::numeric(array_keys($data))) {
  115
+				$data = array('response' => array($serialize => $data));
  116
+			}
  117
+		}
  118
+		 return Xml::fromArray($data)->asXML();
  119
+	}
  120
+
113 121
 }

0 notes on commit e2e42ee

Please sign in to comment.
Something went wrong with that request. Please try again.