2
2
3
3
namespace Francerz \Http \Utils ;
4
4
5
+ use Francerz \Http \Utils \Constants \StatusCodes ;
5
6
use Francerz \Http \Utils \Headers \AbstractAuthorizationHeader ;
6
7
use Francerz \Http \Utils \Headers \BasicAuthorizationHeader ;
7
8
use Francerz \Http \Utils \Headers \BearerAuthorizationHeader ;
8
9
use InvalidArgumentException ;
10
+ use LogicException ;
9
11
use Psr \Http \Message \MessageInterface ;
10
12
use Psr \Http \Message \RequestInterface ;
11
13
use Psr \Http \Message \ResponseInterface ;
12
14
use Psr \Http \Message \ServerRequestInterface ;
15
+ use Psr \Http \Message \UriInterface ;
13
16
14
17
class MessageHelper
15
18
{
@@ -20,14 +23,19 @@ public static function setHttpFactoryManager(HttpFactoryManager $factories)
20
23
static ::$ httpFactoryManager = $ factories ;
21
24
}
22
25
23
- public static function getCurrentRequest () : ServerRequestInterface
26
+ private static function checkFactoryManager ( $ method )
24
27
{
25
28
if (!isset (static ::$ httpFactoryManager )) {
26
- throw new \ Exception (sprintf (
27
- 'Method %s::%s requires you setHttpFactoryManager on %s. ' ,
28
- __CLASS__ , __METHOD__ , __CLASS__
29
+ throw new LogicException (sprintf (
30
+ 'Method %s requires assign setHttpFactoryManager ' ,
31
+ $ method
29
32
));
30
33
}
34
+ }
35
+
36
+ public static function getCurrentRequest () : ServerRequestInterface
37
+ {
38
+ static ::checkFactoryManager (__METHOD__ );
31
39
32
40
$ requestFactory = static ::$ httpFactoryManager ->getServerRequestFactory ();
33
41
$ uriFactory = static ::$ httpFactoryManager ->getUriFactory ();
@@ -124,12 +132,7 @@ public static function getContent(MessageInterface $message)
124
132
125
133
public static function withContent (MessageInterface $ message , string $ mediaType , $ content ) : MessageInterface
126
134
{
127
- if (!isset (static ::$ httpFactoryManager )) {
128
- throw new \Exception (sprintf (
129
- 'Method %s::%s requires you setHttpFactoryManager on %s. ' ,
130
- __CLASS__ , __METHOD__ , __CLASS__
131
- ));
132
- }
135
+ static ::checkFactoryManager (__METHOD__ );
133
136
134
137
$ parser = BodyParserHandler::find ($ mediaType );
135
138
$ streamFactory = static ::$ httpFactoryManager ->getStreamFactory ();
@@ -145,6 +148,20 @@ public static function withContent(MessageInterface $message, string $mediaType,
145
148
->withHeader ('Content-Type ' , $ mediaType );
146
149
}
147
150
151
+ public static function makeRedirect ($ location , int $ code = StatusCodes::REDIRECT_TEMPORARY_REDIRECT )
152
+ {
153
+ static ::checkFactoryManager (__METHOD__ );
154
+ $ responseFactory = static ::$ httpFactoryManager ->getResponseFactory ();
155
+
156
+ if ($ location instanceof UriInterface) {
157
+ $ location = (string )$ location ;
158
+ }
159
+
160
+ return $ responseFactory
161
+ ->createResponse ($ code )
162
+ ->withHeader ('Location ' , $ location );
163
+ }
164
+
148
165
public static function isInfo (ResponseInterface $ response ) : bool
149
166
{
150
167
return $ response ->getStatusCode () >= 100 && $ response ->getStatusCode () < 200 ;
0 commit comments