Skip to content

Commit

Permalink
1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammetsafak committed Feb 24, 2023
1 parent 8e2abd9 commit f0f043e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 54 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ composer require initphp/input

```php
require_once "vendor/autoload.php";
use \InitPHP\Input\Facede\Inputs as Input;
use \InitPHP\Input\Facade\Inputs as Input;

// echo isset($_GET['name']) ? $_GET['name'] : 'John';
echo Input::get('name', 'John');
Expand All @@ -33,7 +33,7 @@ echo Input::get('name', 'John');

```php
require_once "vendor/autoload.php";
use \InitPHP\Input\Facede\Inputs as Input;
use \InitPHP\Input\Facade\Inputs as Input;

/**
* if(isset($_GET['year']) && $_GET['year'] >= 1970 && $_GET['year'] <= 2070){
Expand All @@ -51,7 +51,7 @@ $year = Input::getPost('year', 2015, ['range(1970...2070)']);

```php
require_once "vendor/autoload.php";
use \InitPHP\Input\Facede\Inputs as Input;
use \InitPHP\Input\Facade\Inputs as Input;

/**
* if(isset($_POST['password']) && isset($_POST['password_retype']) && !empty($_POST['password']) && $_POST['password'] == $_POST['password_retype']){
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"require": {
"php": ">=7.2",
"ext-json": "*",
"initphp/parameterbag": "^1.0",
"initphp/parameterbag": "^1.1",
"initphp/validation": "^1.0"
}
}
4 changes: 2 additions & 2 deletions src/Facede/Inputs.php → src/Facade/Inputs.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
* @author Muhammet ŞAFAK <[email protected]>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 1.1
* @version 1.2
* @link https://www.muhammetsafak.com.tr
*/

declare(strict_types=1);

namespace InitPHP\Input\Facede;
namespace InitPHP\Input\Facade;

/**
* @mixin \InitPHP\Input\Inputs
Expand Down
59 changes: 11 additions & 48 deletions src/Inputs.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <[email protected]>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 1.1
* @version 1.2
* @link https://www.muhammetsafak.com.tr
*/

Expand All @@ -33,16 +33,8 @@ class Inputs
/** @var Validation */
private static $validation;

/** @var string|false */
private static $InputBody;

public function __construct()
{
if(!isset(self::$InputBody)){
if((self::$InputBody = @\file_get_contents('php://input')) !== FALSE){
self::$InputBody = \trim(self::$InputBody);
}
}
$this->getParameterBagBoot();
$this->postParameterBagBoot();
$this->rawParameterBagBoot();
Expand Down Expand Up @@ -212,60 +204,31 @@ private function getParameterBagBoot()
if(isset(self::$get)){
return;
}
self::$get = new ParameterBag(($_GET ?? []), ['isMulti' => false]);
self::$get = new ParameterBag(!empty($_GET) ? $_GET : [], ['isMulti' => false]);
}

private function postParameterBagBoot()
{
if(isset(self::$post)){
return;
}
if(isset($_POST) && !empty($_POST)){
self::$post = new ParameterBag($_POST, [
'isMulti' => false
]);
return;
}
if(empty(self::$InputBody)){
self::$post = new ParameterBag([], ['isMulti' => false]);
return;
}
$bodyFirstChar = \substr(self::$InputBody, 0, 1);
if(isset($_SERVER['REQUEST_METHOD'])
&& $_SERVER['REQUEST_METHOD'] == 'POST'
&& $bodyFirstChar !== '{' && $bodyFirstChar !== '['
){
$data = [];
$split = \explode('&', self::$InputBody);
foreach ($split as $argument) {
if(\strpos($argument, '=') === FALSE){
$data[$argument] = '';
continue;
}
$parse = \explode('=', $argument, 2);
$data[$parse[0]] = $parse[1];
}
self::$post = new ParameterBag($data, ['isMulti' => false]);
return;
}
self::$post = new ParameterBag([], ['isMulti' => false]);
self::$post = new ParameterBag(!empty($_POST) ? $_POST : [], ['isMulti' => false]);
}

private function rawParameterBagBoot()
{
if(isset(self::$raw)){
return;
}
if(self::$InputBody === FALSE){
self::$raw = new ParameterBag([], ['isMulti' => false]);
return;
}
$body = \json_decode(self::$InputBody, true);
if(!\is_array($body) || empty($body)){
self::$raw = new ParameterBag([], ['isMulti' => false]);
return;
$body = @\file_get_contents('php://input');

if ($body === FALSE) {
$data = [];
} else {
$data = \json_decode($body, true);
}
self::$raw = new ParameterBag($body, ['isMulti' => false]);

self::$raw = new ParameterBag($data ? $data : [], ['isMulti' => false]);
}

}

0 comments on commit f0f043e

Please sign in to comment.