Skip to content

Commit 2aa799c

Browse files
Merge pull request #2 from InitPHP/2.1
Helper eklendi, Message ve Stream interfacelerine isEmpty ve isNotEmp…
2 parents 4b930d9 + 85b617f commit 2aa799c

File tree

7 files changed

+95
-4
lines changed

7 files changed

+95
-4
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
"autoload": {
77
"psr-4": {
88
"InitPHP\\HTTP\\": "src/"
9-
}
9+
},
10+
"files": [
11+
"src/Helpers.php"
12+
]
1013
},
1114
"authors": [
1215
{

src/Client/Client.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515

1616
namespace InitPHP\HTTP\Client;
1717

18-
use InitPHP\HTTP\Message\Interfaces\StreamInterface;
1918
use \InitPHP\HTTP\Message\{Request, Stream, Response};
20-
use \Psr\Http\Message\{RequestInterface, ResponseInterface};
19+
use \Psr\Http\Message\{RequestInterface, ResponseInterface, StreamInterface};
2120
use \InitPHP\HTTP\Client\Exceptions\{ClientException, NetworkException, RequestException};
2221

2322
use const CASE_LOWER;
@@ -243,7 +242,7 @@ private function prepareRequest(string $method, string $url, $body = null, array
243242
$body = new Stream($body->saveHTML(), null);
244243
} else if ((class_exists('SimpleXMLElement')) && ($body instanceof \SimpleXMLElement)) {
245244
$body = new Stream($body->asXML(), null);
246-
} else if (is_object($body)) {
245+
} else if ((is_object($body)) && !($body instanceof StreamInterface)) {
247246
if (method_exists($body, '__toString')) {
248247
$body = $body->__toString();
249248
} else if (method_exists($body, 'toArray')) {

src/Helpers.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Helpers.php
4+
*
5+
* This file is part of InitPHP HTTP.
6+
*
7+
* @author Muhammet ŞAFAK <[email protected]>
8+
* @copyright Copyright © 2022 Muhammet ŞAFAK
9+
* @license ./LICENSE MIT
10+
* @version 2.0
11+
* @link https://www.muhammetsafak.com.tr
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
if (!function_exists('send_request')) {
17+
/**
18+
* @param string|\Psr\Http\Message\RequestInterface $method [GET|POST|PUT|PATCH|HEAD|DELETE]
19+
* @param string|null $url
20+
* @param array|null $headers
21+
* @param string|null|\DOMDocument|\SimpleXMLElement|array|object|\Psr\Http\Message\StreamInterface $body
22+
* @param string|null $version
23+
* @return \Psr\Http\Message\ResponseInterface
24+
*/
25+
function send_request($method, ?string $url = null, ?array $headers = [], $body = null, ?string $version = null): \Psr\Http\Message\ResponseInterface
26+
{
27+
if ($method instanceof \Psr\Http\Message\RequestInterface) {
28+
return \InitPHP\HTTP\Facade\Client::sendRequest($method);
29+
}
30+
31+
return \InitPHP\HTTP\Facade\Client::fetch($url, [
32+
'method' => $method,
33+
'body' => $body,
34+
'headers' => $headers,
35+
'version' => $version,
36+
]);
37+
}
38+
}

src/Message/Interfaces/MessageInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,14 @@ public function addedHeader($name, $value): self;
5151
*/
5252
public function outHeader($name): self;
5353

54+
/**
55+
* @return bool
56+
*/
57+
public function isEmpty(): bool;
58+
59+
/**
60+
* @return bool
61+
*/
62+
public function isNotEmpty(): bool;
63+
5464
}

src/Message/Interfaces/StreamInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,14 @@
1818

1919
interface StreamInterface extends \Psr\Http\Message\StreamInterface
2020
{
21+
/**
22+
* @return bool
23+
*/
24+
public function isEmpty(): bool;
25+
26+
/**
27+
* @return bool
28+
*/
29+
public function isNotEmpty(): bool;
2130

2231
}

src/Message/Stream.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,22 @@ public function getMetadata($key = null)
445445
return $data[$key] ?? null;
446446
}
447447

448+
/**
449+
* @inheritDoc
450+
*/
451+
public function isEmpty(): bool
452+
{
453+
return $this->getSize() < 1;
454+
}
455+
456+
/**
457+
* @inheritDoc
458+
*/
459+
public function isNotEmpty(): bool
460+
{
461+
return $this->getSize() > 0;
462+
}
463+
448464
protected function getUri()
449465
{
450466
if($this->uri !== FALSE){

src/Message/Traits/MessageTrait.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,22 @@ public function setHeaders(array $headers): self
221221
return $this;
222222
}
223223

224+
/**
225+
* @inheritDoc
226+
*/
227+
public function isEmpty(): bool
228+
{
229+
return $this->getBody()->getSize() < 1;
230+
}
231+
232+
/**
233+
* @inheritDoc
234+
*/
235+
public function isNotEmpty(): bool
236+
{
237+
return $this->getBody()->getSize() > 0;
238+
}
239+
224240
protected function validateAndTrimHeader($header, $values): array
225241
{
226242
if(!is_string($header) || (bool)preg_match("@^[!#$%&'*+.^_`|~0-9A-Za-z-]+$@", $header) === FALSE){

0 commit comments

Comments
 (0)