Skip to content

Commit

Permalink
move serialization to separate method, for easier overriding in subcl…
Browse files Browse the repository at this point in the history
…asses
  • Loading branch information
ceeram committed Aug 10, 2012
1 parent e5ad204 commit e2e42ee
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
31 changes: 19 additions & 12 deletions lib/Cake/View/JsonView.php
Expand Up @@ -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) {
Expand All @@ -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);
}

}
36 changes: 22 additions & 14 deletions lib/Cake/View/XmlView.php
Expand Up @@ -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) {
Expand All @@ -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.