1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11: * @link http://cakephp.org CakePHP(tm) Project
12: * @license http://www.opensource.org/licenses/mit-license.php MIT License
13: */
14:
15: App::uses('View', 'View');
16: App::uses('Xml', 'Utility');
17:
18: /**
19: * A view class that is used for creating XML responses.
20: *
21: * By setting the '_serialize' key in your controller, you can specify a view variable
22: * that should be serialized to XML and used as the response for the request.
23: * This allows you to omit views + layouts, if your just need to emit a single view
24: * variable as the XML response.
25: *
26: * In your controller, you could do the following:
27: *
28: * `$this->set(array('posts' => $posts, '_serialize' => 'posts'));`
29: *
30: * When the view is rendered, the `$posts` view variable will be serialized
31: * into XML.
32: *
33: * **Note** The view variable you specify must be compatible with Xml::fromArray().
34: *
35: * You can also define `'_serialize'` as an array. This will create an additional
36: * top level element named `<response>` containing all the named view variables:
37: *
38: * {{{
39: * $this->set(compact('posts', 'users', 'stuff'));
40: * $this->set('_serialize', array('posts', 'users'));
41: * }}}
42: *
43: * The above would generate a XML object that looks like:
44: *
45: * `<response><posts>...</posts><users>...</users></response>`
46: *
47: * If you don't use the `_serialize` key, you will need a view. You can use extended
48: * views to provide layout like functionality.
49: *
50: * @package Cake.View
51: * @since CakePHP(tm) v 2.1.0
52: */
53: class XmlView extends View {
54:
55: /**
56: * The subdirectory. XML views are always in xml.
57: *
58: * @var string
59: */
60: public $subDir = 'xml';
61:
62: /**
63: * Constructor
64: *
65: * @param Controller $controller
66: */
67: public function __construct(Controller $controller = null) {
68: parent::__construct($controller);
69:
70: if (isset($controller->response) && $controller->response instanceof CakeResponse) {
71: $controller->response->type('xml');
72: }
73: }
74:
75: /**
76: * Render a XML view.
77: *
78: * Uses the special '_serialize' parameter to convert a set of
79: * view variables into a XML response. Makes generating simple
80: * XML responses very easy. You can omit the '_serialize' parameter,
81: * and use a normal view + layout as well.
82: *
83: * @param string $view The view being rendered.
84: * @param string $layout The layout being rendered.
85: * @return string The rendered view.
86: */
87: public function render($view = null, $layout = null) {
88: if (isset($this->viewVars['_serialize'])) {
89: return $this->_serialize($this->viewVars['_serialize']);
90: }
91: if ($view !== false && $this->_getViewFileName($view)) {
92: return parent::render($view, false);
93: }
94: }
95:
96: /**
97: * Serialize view vars.
98: *
99: * @param array $serialize The viewVars that need to be serialized.
100: * @return string The serialized data
101: */
102: protected function _serialize($serialize) {
103: $rootNode = isset($this->viewVars['_rootNode']) ? $this->viewVars['_rootNode'] : 'response';
104:
105: if (is_array($serialize)) {
106: $data = array($rootNode => array());
107: foreach ($serialize as $key) {
108: $data[$rootNode][$key] = $this->viewVars[$key];
109: }
110: } else {
111: $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
112: if (is_array($data) && Set::numeric(array_keys($data))) {
113: $data = array($rootNode => array($serialize => $data));
114: }
115: }
116: return Xml::fromArray($data)->asXML();
117: }
118:
119: }
120: