-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f600cc5
commit a0cfaf2
Showing
3 changed files
with
96 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Chargily\ChargilyPay\Core\Helpers; | ||
|
||
class HttpRequest | ||
{ | ||
/** | ||
* Cache headers | ||
* | ||
* @var array|null | ||
*/ | ||
protected static ?array $headers = null; | ||
|
||
/** | ||
* Get current request headers | ||
* | ||
* @return array | ||
*/ | ||
public static function headers(): array | ||
{ | ||
if (static::$headers) { | ||
return static::$headers; | ||
} | ||
$server_headers = []; | ||
foreach ($_SERVER as $key => $value) { | ||
if (Str::startsWith($key, "HTTP_")) { | ||
$header_name = Str::lower(Str::replace(['HTTP_', "_"], ['', '-'], $key)); | ||
$header_name = ucwords($header_name, '-'); | ||
|
||
$server_headers[$header_name] = $value; | ||
} | ||
} | ||
|
||
return static::$headers = $server_headers; | ||
} | ||
/** | ||
* get header | ||
* | ||
* @param string $name | ||
* @return string|null | ||
*/ | ||
public static function header(string $name): ?string | ||
{ | ||
$headers = self::headers(); | ||
|
||
return $headers[$name] ?? null; | ||
} | ||
/** | ||
* Get request data | ||
* | ||
* @return array|null | ||
*/ | ||
public static function data(): array | ||
{ | ||
if (!empty($_GET)) { | ||
return $_GET; | ||
} | ||
if (!empty($_POST)) { | ||
return $_POST; | ||
} | ||
return []; | ||
} | ||
/** | ||
* Body | ||
* | ||
* @return string|null | ||
*/ | ||
public static function body(): ?string | ||
{ | ||
$body = file_get_contents('php://input'); | ||
return (!empty($body)) ? $body : null; | ||
} | ||
} |