[TASK] Re-work/simplify copyright header in PHP files - Part 1
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / View / JsonView.php
1 <?php
2 namespace TYPO3\CMS\Install\View;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Extbase\Mvc\View\AbstractView;
18 use TYPO3\CMS\Install\Status\StatusInterface;
19 use TYPO3\CMS\Install\Status\Exception as StatusException;
20
21 /**
22 * Simple JsonView (currently returns an associative array)
23 */
24 class JsonView extends AbstractView {
25
26 /**
27 * @return string
28 */
29 public function render() {
30 $renderedData = $this->variables;
31 if (isset($renderedData['status']) && is_array($renderedData['status'])) {
32 try {
33 $renderedData['status'] = $this->transformStatusMessagesToArray($renderedData['status']);
34 } catch (StatusException $e) {
35 $renderedData['status'] = array(array(
36 'severity' => 'error',
37 'title' => htmlspecialchars($e->getMessage())
38 ));
39 }
40 }
41
42 return $renderedData;
43 }
44
45 /**
46 * Transform an array of messages to an associative array.
47 *
48 * @param array<StatusInterface>
49 * @return array
50 * @throws StatusException
51 */
52 protected function transformStatusMessagesToArray(array $statusArray = array()) {
53 $result = array();
54 foreach ($statusArray as $status) {
55 if (!$status instanceof StatusInterface) {
56 throw new StatusException (
57 'Object must implement StatusInterface',
58 1381059600
59 );
60 }
61 $result[] = $this->transformStatusToArray($status);
62 }
63 return $result;
64 }
65
66 /**
67 * Creates an array from a status object.
68 * Used for example to transfer the message as json.
69 *
70 * @param StatusInterface $status
71 * @return array
72 */
73 public function transformStatusToArray(StatusInterface $status) {
74 $arrayStatus = array();
75 $arrayStatus['severity'] = htmlspecialchars($status->getSeverity());
76 $arrayStatus['title'] = htmlspecialchars($status->getTitle());
77 $arrayStatus['message'] = htmlspecialchars($status->getMessage());
78 return $arrayStatus;
79 }
80
81 }