TYPO3 CMS  TYPO3_8-7
JsonView.php
Go to the documentation of this file.
1 <?php
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 
20 
24 class JsonView extends AbstractView
25 {
29  public function render()
30  {
31  $renderedData = $this->variables;
32  if (isset($renderedData['status']) && is_array($renderedData['status'])) {
33  try {
34  $renderedData['status'] = $this->transformStatusMessagesToArray($renderedData['status']);
35  } catch (StatusException $e) {
36  $renderedData['status'] = [[
37  'severity' => 'error',
38  'title' => htmlspecialchars($e->getMessage())
39  ]];
40  }
41  }
42 
43  return $renderedData;
44  }
45 
53  protected function transformStatusMessagesToArray(array $statusArray = [])
54  {
55  $result = [];
56  foreach ($statusArray as $status) {
57  if (!$status instanceof StatusInterface) {
58  throw new StatusException(
59  'Object must implement StatusInterface',
60  1381059600
61  );
62  }
63  $result[] = $this->transformStatusToArray($status);
64  }
65  return $result;
66  }
67 
75  public function transformStatusToArray(StatusInterface $status)
76  {
77  $arrayStatus = [];
78  $arrayStatus['severity'] = $this->getSeverityAsNumber($status->getSeverity());
79  $arrayStatus['title'] = htmlspecialchars($status->getTitle());
80  $arrayStatus['message'] = htmlspecialchars($status->getMessage());
81  return $arrayStatus;
82  }
83 
91  protected function getSeverityAsNumber($severity)
92  {
93  $number = -2;
94  switch (strtolower($severity)) {
95  case 'loading':
96  $number = -3;
97  break;
98  case 'notice':
99  $number = -2;
100  break;
101  case 'info':
102  $number = -1;
103  break;
104  case 'ok':
105  case 'success':
106  $number = 0;
107  break;
108  case 'warning':
109  $number = 1;
110  break;
111  case 'error':
112  case 'danger':
113  case 'fatal':
114  $number = 2;
115  break;
116  }
117  return $number;
118  }
119 }
transformStatusMessagesToArray(array $statusArray=[])
Definition: JsonView.php:53
transformStatusToArray(StatusInterface $status)
Definition: JsonView.php:75