Skip to content

Commit e2e42ee

Browse files
committed
move serialization to separate method, for easier overriding in subclasses
1 parent e5ad204 commit e2e42ee

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

lib/Cake/View/JsonView.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,7 @@ public function __construct(Controller $controller = null) {
8282
*/
8383
public function render($view = null, $layout = null) {
8484
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']);
9786
}
9887
if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
9988
if (!$this->_helpersLoaded) {
@@ -105,4 +94,22 @@ public function render($view = null, $layout = null) {
10594
}
10695
}
10796

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+
108115
}

lib/Cake/View/XmlView.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,7 @@ public function __construct(Controller $controller = null) {
8585
*/
8686
public function render($view = null, $layout = null) {
8787
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']);
10289
}
10390
if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
10491
if (!$this->_helpersLoaded) {
@@ -110,4 +97,25 @@ public function render($view = null, $layout = null) {
11097
}
11198
}
11299

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+
113121
}

0 commit comments

Comments
 (0)