9b9f1446513aebecbe8dbaa3842bacf410321d81
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / View / JsonView.php
1 <?php
2 namespace TYPO3\CMS\Install\View;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 Helmut Hummel <helmut.hummel@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 use TYPO3\CMS\Extbase\Mvc\View\AbstractView;
28 use TYPO3\CMS\Install\Status\StatusInterface;
29 use TYPO3\CMS\Install\Status\Exception as StatusException;
30
31 /**
32 * Simple JsonView (currently returns an associative array)
33 */
34 class JsonView extends AbstractView {
35
36 /**
37 * @return string
38 */
39 public function render() {
40 $renderedData = $this->variables;
41 if (isset($renderedData['status']) && is_array($renderedData['status'])) {
42 try {
43 $renderedData['status'] = $this->transformStatusMessagesToArray($renderedData['status']);
44 } catch (StatusException $e) {
45 $renderedData['status'] = array(array(
46 'severity' => 'error',
47 'title' => htmlspecialchars($e->getMessage())
48 ));
49 }
50 }
51
52 return $renderedData;
53 }
54
55 /**
56 * Transform an array of messages to an associative array.
57 *
58 * @param array<StatusInterface>
59 * @return array
60 * @throws StatusException
61 */
62 protected function transformStatusMessagesToArray(array $statusArray = array()) {
63 $result = array();
64 foreach ($statusArray as $status) {
65 if (!$status instanceof StatusInterface) {
66 throw new StatusException (
67 'Object must implement StatusInterface',
68 1381059600
69 );
70 }
71 $result[] = $this->transformStatusToArray($status);
72 }
73 return $result;
74 }
75
76 /**
77 * Creates an array from a status object.
78 * Used for example to transfer the message as json.
79 *
80 * @param StatusInterface $status
81 * @return array
82 */
83 public function transformStatusToArray(StatusInterface $status) {
84 $arrayStatus = array();
85 $arrayStatus['severity'] = htmlspecialchars($status->getSeverity());
86 $arrayStatus['title'] = htmlspecialchars($status->getTitle());
87 $arrayStatus['message'] = htmlspecialchars($status->getMessage());
88 return $arrayStatus;
89 }
90
91 }