-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
SerializedView.php
116 lines (104 loc) · 3.38 KB
/
SerializedView.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.1.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\View;
use Cake\View\Exception\SerializationFailureException;
use Exception;
use TypeError;
/**
* Parent class for view classes generating serialized outputs like JsonView and XmlView.
*/
abstract class SerializedView extends View
{
/**
* Default config options.
*
* Use ViewBuilder::setOption()/setOptions() in your controlle to set these options.
*
* - `serialize`: Option to convert a set of view variables into a serialized response.
* Its value can be a string for single variable name or array for multiple
* names. If true all view variables will be serialized. If null or false
* normal view template will be rendered.
*
* @var array<string, mixed>
*/
protected array $_defaultConfig = [
'serialize' => null,
];
/**
* Load helpers only if serialization is disabled.
*
* @return $this
*/
public function loadHelpers()
{
if (!$this->getConfig('serialize')) {
parent::loadHelpers();
}
return $this;
}
/**
* Serialize view vars.
*
* @param array|string $serialize The name(s) of the view variable(s) that
* need(s) to be serialized
* @return string The serialized data.
*/
abstract protected function _serialize(array|string $serialize): string;
/**
* Render view template or return serialized data.
*
* @param string|null $template The template being rendered.
* @param string|false|null $layout The layout being rendered.
* @return string The rendered view.
* @throws \Cake\View\Exception\SerializationFailureException When serialization fails.
*/
public function render(?string $template = null, string|false|null $layout = null): string
{
$serialize = $this->serializeKeys();
if ($serialize !== false) {
try {
return $this->_serialize($serialize);
} catch (Exception | TypeError $e) {
throw new SerializationFailureException(
'Serialization of View data failed.',
null,
$e
);
}
}
return parent::render($template, false);
}
/**
* @return array|string|false
*/
protected function serializeKeys(): array|string|false
{
$serialize = $this->getConfig('serialize', false);
if ($serialize === true) {
$options = array_map(
function ($v) {
return '_' . $v;
},
array_keys($this->_defaultConfig)
);
$serialize = array_diff(
array_keys($this->viewVars),
$options
);
}
return $serialize;
}
}