Skip to content

Commit

Permalink
1.0.3 Added RedirectResponse::class.
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammetsafak committed Aug 16, 2022
1 parent 8ebb91d commit 8ad1763
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Emitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 InitPHP
* @license http://initphp.github.com/license.txt MIT
* @version 1.0
* @version 1.0.3
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 InitPHP
* @license http://initphp.github.com/license.txt MIT
* @version 1.0
* @version 1.0.3
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
31 changes: 28 additions & 3 deletions src/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 InitPHP
* @license http://initphp.github.com/license.txt MIT
* @version 1.0.1
* @version 1.0.3
* @link https://www.muhammetsafak.com.tr
*/

Expand All @@ -16,15 +16,40 @@
namespace InitPHP\HTTP;

use function json_encode;
use function json_decode;
use function is_string;
use function is_array;

/**
* @since 1.0.1
*/
class JsonResponse extends Response
{
public function __construct(array $data, int $status = 200, string $version = '1.1')

private array $data = [];

/**
* @param string|array $data
* @param int $status
* @param string $version
*/
public function __construct($data, int $status = 200, string $version = '1.1')
{
$body = new Stream(json_encode($data), null);
if(is_array($data)){
$this->data = $data;
$data = json_encode($data);
}elseif(is_string($data)){
$this->data = json_decode($data, true);
}else{
throw new \InvalidArgumentException('The $data parameter must be an array or json string.');
}
$body = new Stream($data, null);
parent::__construct($status, ['Content-Type' => 'application/json'], $body, $version, null);
}

public function toArray(): array
{
return $this->data;
}

}
2 changes: 1 addition & 1 deletion src/MessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 InitPHP
* @license http://initphp.github.com/license.txt MIT
* @version 1.0
* @version 1.0.3
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
53 changes: 53 additions & 0 deletions src/RedirectResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* RedirectResponse.php
*
* This file is part of HTTP.
*
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 1.0.3
* @link https://www.muhammetsafak.com.tr
*/

declare(strict_types=1);

namespace InitPHP\HTTP;

use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;

use function is_string;

/**
* @since 1.0.2
*/
class RedirectResponse extends Response
{
/**
* @param string|UriInterface $url
* @param int $status
* @param int $second
*/
public function __construct($url, int $status = 200, int $second = 0)
{
if($url instanceof UriInterface){
$url = $url->__toString();
}
if(!is_string($url)){
throw new \InvalidArgumentException('The $url parameter must be an UriInterface or string.');
}
if($second < 1){
$headers = [
'Location' => $url,
];
}else{
$headers = [
'Refresh' => $second . '; url=' . $url,
];
}
parent::__construct($status, $headers, new Stream('', null), '1.1');
}

}
2 changes: 1 addition & 1 deletion src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 InitPHP
* @license http://initphp.github.com/license.txt MIT
* @version 1.0
* @version 1.0.3
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/RequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 InitPHP
* @license http://initphp.github.com/license.txt MIT
* @version 1.0
* @version 1.0.3
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 InitPHP
* @license http://initphp.github.com/license.txt MIT
* @version 1.0
* @version 1.0.3
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 InitPHP
* @license http://initphp.github.com/license.txt MIT
* @version 1.0
* @version 1.0.3
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 InitPHP
* @license http://initphp.github.com/license.txt MIT
* @version 1.0
* @version 1.0.3
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 InitPHP
* @license http://initphp.github.com/license.txt MIT
* @version 1.0
* @version 1.0.3
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 InitPHP
* @license http://initphp.github.com/license.txt MIT
* @version 1.0
* @version 1.0.3
* @link https://www.muhammetsafak.com.tr
*/

Expand Down

0 comments on commit 8ad1763

Please sign in to comment.