/
ResponseHeadImplementation.php
91 lines (83 loc) · 2.37 KB
/
ResponseHeadImplementation.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
<?php
namespace Neos\Fusion\FusionObjects\Http;
/*
* This file is part of the Neos.Fusion package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Http\Helper\ResponseInformationHelper;
use Neos\Fusion\FusionObjects\AbstractFusionObject;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Response Head generate a standard HTTP response head
* @api
*/
class ResponseHeadImplementation extends AbstractFusionObject
{
/**
* @Flow\Inject
* @var ResponseFactoryInterface
*/
protected $responseFactory;
/**
* Get HTTP protocol version
*
* @return string
*/
public function getHttpVersion()
{
$httpVersion = $this->fusionValue('httpVersion');
if ($httpVersion === null) {
$httpVersion = 'HTTP/1.1';
}
return trim($httpVersion);
}
/**
* @return integer
*/
public function getStatusCode()
{
$statusCode = $this->fusionValue('statusCode');
if ($statusCode === null) {
$statusCode = 200;
}
if (ResponseInformationHelper::getStatusMessageByCode($statusCode) === 'Unknown Status') {
throw new \InvalidArgumentException('Unknown HTTP status code', 1412085703);
}
return (integer)$statusCode;
}
/**
* @return array
*/
public function getHeaders(): array
{
$headers = $this->fusionValue('headers');
if (!is_array($headers)) {
$headers = [];
}
return $headers;
}
/**
* Just return the processed value
*
* @return ResponseInterface
*/
public function evaluate(): ResponseInterface
{
$httpVersion = $this->getHttpVersion();
if (strpos($httpVersion, 'HTTP/') === 0) {
$httpVersion = substr($httpVersion, 5);
}
$response = $this->responseFactory->createResponse($this->getStatusCode())->withProtocolVersion($httpVersion);
foreach ($this->getHeaders() as $headerName => $headerValue) {
$response = $response->withHeader($headerName, $headerValue);
}
return $response;
}
}