Skip to content

Commit 8ad1763

Browse files
committed
1.0.3 Added RedirectResponse::class.
1 parent 8ebb91d commit 8ad1763

12 files changed

+91
-13
lines changed

src/Emitter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <[email protected]>
88
* @copyright Copyright © 2022 InitPHP
99
* @license http://initphp.github.com/license.txt MIT
10-
* @version 1.0
10+
* @version 1.0.3
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

src/Factory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <[email protected]>
88
* @copyright Copyright © 2022 InitPHP
99
* @license http://initphp.github.com/license.txt MIT
10-
* @version 1.0
10+
* @version 1.0.3
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

src/JsonResponse.php

+28-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <[email protected]>
88
* @copyright Copyright © 2022 InitPHP
99
* @license http://initphp.github.com/license.txt MIT
10-
* @version 1.0.1
10+
* @version 1.0.3
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

@@ -16,15 +16,40 @@
1616
namespace InitPHP\HTTP;
1717

1818
use function json_encode;
19+
use function json_decode;
20+
use function is_string;
21+
use function is_array;
1922

2023
/**
2124
* @since 1.0.1
2225
*/
2326
class JsonResponse extends Response
2427
{
25-
public function __construct(array $data, int $status = 200, string $version = '1.1')
28+
29+
private array $data = [];
30+
31+
/**
32+
* @param string|array $data
33+
* @param int $status
34+
* @param string $version
35+
*/
36+
public function __construct($data, int $status = 200, string $version = '1.1')
2637
{
27-
$body = new Stream(json_encode($data), null);
38+
if(is_array($data)){
39+
$this->data = $data;
40+
$data = json_encode($data);
41+
}elseif(is_string($data)){
42+
$this->data = json_decode($data, true);
43+
}else{
44+
throw new \InvalidArgumentException('The $data parameter must be an array or json string.');
45+
}
46+
$body = new Stream($data, null);
2847
parent::__construct($status, ['Content-Type' => 'application/json'], $body, $version, null);
2948
}
49+
50+
public function toArray(): array
51+
{
52+
return $this->data;
53+
}
54+
3055
}

src/MessageTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <[email protected]>
88
* @copyright Copyright © 2022 InitPHP
99
* @license http://initphp.github.com/license.txt MIT
10-
* @version 1.0
10+
* @version 1.0.3
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

src/RedirectResponse.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* RedirectResponse.php
4+
*
5+
* This file is part of HTTP.
6+
*
7+
* @author Muhammet ŞAFAK <[email protected]>
8+
* @copyright Copyright © 2022 Muhammet ŞAFAK
9+
* @license ./LICENSE MIT
10+
* @version 1.0.3
11+
* @link https://www.muhammetsafak.com.tr
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace InitPHP\HTTP;
17+
18+
use Psr\Http\Message\StreamInterface;
19+
use Psr\Http\Message\UriInterface;
20+
21+
use function is_string;
22+
23+
/**
24+
* @since 1.0.2
25+
*/
26+
class RedirectResponse extends Response
27+
{
28+
/**
29+
* @param string|UriInterface $url
30+
* @param int $status
31+
* @param int $second
32+
*/
33+
public function __construct($url, int $status = 200, int $second = 0)
34+
{
35+
if($url instanceof UriInterface){
36+
$url = $url->__toString();
37+
}
38+
if(!is_string($url)){
39+
throw new \InvalidArgumentException('The $url parameter must be an UriInterface or string.');
40+
}
41+
if($second < 1){
42+
$headers = [
43+
'Location' => $url,
44+
];
45+
}else{
46+
$headers = [
47+
'Refresh' => $second . '; url=' . $url,
48+
];
49+
}
50+
parent::__construct($status, $headers, new Stream('', null), '1.1');
51+
}
52+
53+
}

src/Request.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <[email protected]>
88
* @copyright Copyright © 2022 InitPHP
99
* @license http://initphp.github.com/license.txt MIT
10-
* @version 1.0
10+
* @version 1.0.3
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

src/RequestTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <[email protected]>
88
* @copyright Copyright © 2022 InitPHP
99
* @license http://initphp.github.com/license.txt MIT
10-
* @version 1.0
10+
* @version 1.0.3
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

src/Response.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <[email protected]>
88
* @copyright Copyright © 2022 InitPHP
99
* @license http://initphp.github.com/license.txt MIT
10-
* @version 1.0
10+
* @version 1.0.3
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

src/ServerRequest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <[email protected]>
88
* @copyright Copyright © 2022 InitPHP
99
* @license http://initphp.github.com/license.txt MIT
10-
* @version 1.0
10+
* @version 1.0.3
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

src/Stream.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <[email protected]>
88
* @copyright Copyright © 2022 InitPHP
99
* @license http://initphp.github.com/license.txt MIT
10-
* @version 1.0
10+
* @version 1.0.3
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

src/UploadedFile.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <[email protected]>
88
* @copyright Copyright © 2022 InitPHP
99
* @license http://initphp.github.com/license.txt MIT
10-
* @version 1.0
10+
* @version 1.0.3
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

src/Uri.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <[email protected]>
88
* @copyright Copyright © 2022 InitPHP
99
* @license http://initphp.github.com/license.txt MIT
10-
* @version 1.0
10+
* @version 1.0.3
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

0 commit comments

Comments
 (0)