|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Jaeger |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2015-2016, mithra62 |
|
6
|
|
|
* @link http://jaeger-app.com |
|
7
|
|
|
* @version 1.0 |
|
8
|
|
|
* @filesource ./Platforms/View/Rest.php |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace JaegerApp\Platforms\View; |
|
11
|
|
|
|
|
12
|
|
|
use Nocarrier\Hal; |
|
13
|
|
|
use Crell\ApiProblem\ApiProblem; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Jaeger - Rest View Object |
|
17
|
|
|
* |
|
18
|
|
|
* Contains the view helpers for Rest Requests |
|
19
|
|
|
* |
|
20
|
|
|
* @package Rest\View |
|
21
|
|
|
* @author Eric Lamb <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class Rest extends AbstractView |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* The Platform object |
|
27
|
|
|
* @var \mithra62\Platforms\AbstractPlatform |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $platform = null; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The System errors |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $system_errors = array(); |
|
36
|
|
|
|
|
37
|
|
|
public function setSystemErrors(array $errors = array()) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->system_errors = $errors; |
|
40
|
|
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Returns any set system errors |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getSystemErrors() |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->system_errors; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Returns an instance of the Hal object for use |
|
54
|
|
|
* @param string $route |
|
55
|
|
|
* @return Hal |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getHal($route, array $item = array()) |
|
58
|
|
|
{ |
|
59
|
|
|
return new Hal($route, $item); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns an instance of ApiProblem object |
|
64
|
|
|
* @param string $title |
|
65
|
|
|
* @param string $type |
|
66
|
|
|
* @return \Crell\ApiProblem\ApiProblem |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getApiProblem($title, $type) |
|
69
|
|
|
{ |
|
70
|
|
|
return new ApiProblem($this->m62Lang($title), $type); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Returns the data for output and sets the appropriate headers |
|
76
|
|
|
* @param \Nocarrier\Hal $hal |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public function renderOutput(\Nocarrier\Hal $hal) |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
|
|
$this->sendHeaders(); |
|
|
|
|
|
|
82
|
|
|
if($this->getSystemErrors()) |
|
83
|
|
|
{ |
|
84
|
|
|
$system_errors = array(); |
|
85
|
|
|
foreach($this->getSystemErrors() As $key => $value) { |
|
86
|
|
|
$system_errors[$key] = $this->m62Lang($key); |
|
87
|
|
|
} |
|
88
|
|
|
$hal->setData($hal->getData() + array('_system_errors' => $system_errors)); |
|
89
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
View Code Duplication |
if(isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos(strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'xml') !== false) |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
header('Content-Type: application/hal+xml'); |
|
94
|
|
|
return $hal->asXml(true); |
|
95
|
|
|
} |
|
96
|
|
|
else |
|
97
|
|
|
{ |
|
98
|
|
|
header('Content-Type: application/hal+json'); |
|
99
|
|
|
return $hal->asJson(true); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Wrapper to handle error output |
|
105
|
|
|
* |
|
106
|
|
|
* Note that $detail should be a key for language translation |
|
107
|
|
|
* |
|
108
|
|
|
* @param int $code The HTTP response code to send |
|
109
|
|
|
* @param string $title The title to display |
|
110
|
|
|
* @param array $errors Any errors to explain what went wrong |
|
111
|
|
|
* @param string $detail A human readable explanation of what happened |
|
112
|
|
|
* @param string $type A URI resource to deaper explanations on what happened |
|
113
|
|
|
*/ |
|
114
|
|
|
public function renderError($code, $title, array $errors = array(), $detail = null, $type = null) |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
|
|
http_response_code($code); |
|
117
|
|
|
|
|
118
|
|
|
$problem = $this->getApiProblem($title, $type); |
|
119
|
|
|
$problem->setStatus($code); |
|
120
|
|
|
$problem->setDetail($detail); |
|
121
|
|
|
if($errors) |
|
|
|
|
|
|
122
|
|
|
{ |
|
123
|
|
|
$problem['errors'] = $errors; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$this->sendHeaders(); |
|
|
|
|
|
|
127
|
|
View Code Duplication |
if(isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos(strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'xml') !== false) |
|
|
|
|
|
|
128
|
|
|
{ |
|
129
|
|
|
header('Content-Type: application/problem+xml'); |
|
130
|
|
|
return $problem->asXml(true); |
|
131
|
|
|
} |
|
132
|
|
|
else |
|
133
|
|
|
{ |
|
134
|
|
|
header('Content-Type: application/problem+json'); |
|
135
|
|
|
return $problem->asJson(true); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: