From 8e2abd99c6ead65d350cbc589590f9bd5a1793c3 Mon Sep 17 00:00:00 2001 From: Muhammet SAFAK Date: Mon, 25 Jul 2022 19:36:06 +0300 Subject: [PATCH] ParameterBag is kept with static property. --- README.md | 46 ++--- composer.json | 2 +- src/{Input.php => Facede/Inputs.php} | 43 ++--- src/Inputs.php | 271 +++++++++++++++++++++++++++ src/Stack.php | 246 ------------------------ 5 files changed, 311 insertions(+), 297 deletions(-) rename src/{Input.php => Facede/Inputs.php} (71%) create mode 100644 src/Inputs.php delete mode 100644 src/Stack.php diff --git a/README.md b/README.md index 233edd4..927c319 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # InitPHP Input -It is a simple library developed to retrieve user inputs by prioritizing or verifying. +Is a library for prioritizing or verifying Get, Post and Raw inputs. [![Latest Stable Version](http://poser.pugx.org/initphp/input/v)](https://packagist.org/packages/initphp/input) [![Total Downloads](http://poser.pugx.org/initphp/input/downloads)](https://packagist.org/packages/initphp/input) [![Latest Unstable Version](http://poser.pugx.org/initphp/input/v/unstable)](https://packagist.org/packages/initphp/input) [![License](http://poser.pugx.org/initphp/input/license)](https://packagist.org/packages/initphp/input) [![PHP Version Require](http://poser.pugx.org/initphp/input/require/php)](https://packagist.org/packages/initphp/input) ## Requirements -- PHP 7.4 or higher +- PHP 7.2 or later - [InitPHP ParameterBag](https://github.com/InitPHP/ParameterBag) - [InitPHP Validation](https://github.com/InitPHP/Validation) @@ -23,7 +23,7 @@ composer require initphp/input ```php require_once "vendor/autoload.php"; -use \InitPHP\Input\Input; +use \InitPHP\Input\Facede\Inputs as Input; // echo isset($_GET['name']) ? $_GET['name'] : 'John'; echo Input::get('name', 'John'); @@ -33,7 +33,7 @@ echo Input::get('name', 'John'); ```php require_once "vendor/autoload.php"; -use \InitPHP\Input\Input; +use \InitPHP\Input\Facede\Inputs as Input; /** * if(isset($_GET['year']) && $_GET['year'] >= 1970 && $_GET['year'] <= 2070){ @@ -51,7 +51,7 @@ $year = Input::getPost('year', 2015, ['range(1970...2070)']); ```php require_once "vendor/autoload.php"; -use \InitPHP\Input\Input; +use \InitPHP\Input\Facede\Inputs as Input; /** * if(isset($_POST['password']) && isset($_POST['password_retype']) && !empty($_POST['password']) && $_POST['password'] == $_POST['password_retype']){ @@ -66,19 +66,19 @@ $password = Input::post('password', null, ['required', 'again(password_retype)'] ## Methods -#### `Input::get()` +#### `Inputs::get()` ```php public function get(string $key, mixed $default = null, ?array $validation = null): mixed; ``` -#### `Input::post()` +#### `Inputs::post()` ```php public function post(string $key, mixed $default = null, ?array $validation = null): mixed; ``` -#### `Input::raw()` +#### `Inputs::raw()` Data from reading `php://input`. @@ -88,7 +88,7 @@ public function raw(string $key, mixed $default = null, ?array $validation = nul ### Getting Input with Priority -#### `Input::getPost()` +#### `Inputs::getPost()` `$_GET` -> `$_POST` @@ -96,7 +96,7 @@ public function raw(string $key, mixed $default = null, ?array $validation = nul public function getPost(string $key, mixed $default = null, ?array $validation = null): mixed; ``` -#### `Input::getRaw()` +#### `Inputs::getRaw()` `$_GET` -> `php://input` @@ -104,7 +104,7 @@ public function getPost(string $key, mixed $default = null, ?array $validation = public function getRaw(string $key, mixed $default = null, ?array $validation = null): mixed; ``` -#### `Input::getPostRaw()` +#### `Inputs::getPostRaw()` `$_GET` -> `$_POST` -> `php://input` @@ -112,7 +112,7 @@ public function getRaw(string $key, mixed $default = null, ?array $validation = public function getPostRaw(string $key, mixed $default = null, ?array $validation = null): mixed; ``` -#### `Input::getRawPost()` +#### `Inputs::getRawPost()` `$_GET` -> `php://input` -> `$_POST` @@ -120,7 +120,7 @@ public function getPostRaw(string $key, mixed $default = null, ?array $validatio public function getRawPost(string $key, mixed $default = null, ?array $validation = null): mixed; ``` -#### `Input::postGet()` +#### `Inputs::postGet()` `$_POST` -> `$_GET` @@ -128,7 +128,7 @@ public function getRawPost(string $key, mixed $default = null, ?array $validatio public function postGet(string $key, mixed $default = null, ?array $validation = null): mixed; ``` -#### `Input::postRaw()` +#### `Inputs::postRaw()` `$_POST` -> `php://input` @@ -136,7 +136,7 @@ public function postGet(string $key, mixed $default = null, ?array $validation = public function postRaw(string $key, mixed $default = null, ?array $validation = null): mixed; ``` -#### `Input::postGetRaw()` +#### `Inputs::postGetRaw()` `$_POST` -> `$_GET` -> `php://input` @@ -144,7 +144,7 @@ public function postRaw(string $key, mixed $default = null, ?array $validation = public function postGetRaw(string $key, mixed $default = null, ?array $validation = null): mixed; ``` -#### `Input::postRawGet()` +#### `Inputs::postRawGet()` `$_POST` -> `php://input` -> `$_GET` @@ -152,7 +152,7 @@ public function postGetRaw(string $key, mixed $default = null, ?array $validatio public function postRawGet(string $key, mixed $default = null, ?array $validation = null): mixed; ``` -#### `Input::rawGet()` +#### `Inputs::rawGet()` `php://input` -> `$_GET` @@ -160,7 +160,7 @@ public function postRawGet(string $key, mixed $default = null, ?array $validatio public function rawGet(string $key, mixed $default = null, ?array $validation = null): mixed; ``` -#### `Input::rawPost()` +#### `Inputs::rawPost()` `php://input` -> `$_POST` @@ -168,7 +168,7 @@ public function rawGet(string $key, mixed $default = null, ?array $validation = public function rawPost(string $key, mixed $default = null, ?array $validation = null): mixed; ``` -#### `Input::rawGetPost()` +#### `Inputs::rawGetPost()` `php://input` -> `$_GET` -> `$_POST` @@ -176,7 +176,7 @@ public function rawPost(string $key, mixed $default = null, ?array $validation = public function rawGetPost(string $key, mixed $default = null, ?array $validation = null): mixed; ``` -#### `Input::rawPostGet()` +#### `Inputs::rawPostGet()` `php://input` -> `$_POST` -> `$_GET` @@ -188,7 +188,7 @@ public function rawPostGet(string $key, mixed $default = null, ?array $validatio Checks to see if the requested entry has been declared. -#### `Input::hasGet()` +#### `Inputs::hasGet()` It does something like `isset($_GET['key'])` , case-insensitively. @@ -196,7 +196,7 @@ It does something like `isset($_GET['key'])` , case-insensitively. public function hasGet(string $key): bool; ``` -#### `Input::hasPost()` +#### `Inputs::hasPost()` It does something like `isset($_POST['key'])` , case-insensitively. @@ -204,7 +204,7 @@ It does something like `isset($_POST['key'])` , case-insensitively. public function hasPost(string $key): bool; ``` -#### `Input::hasRaw()` +#### `Inputs::hasRaw()` Case-insensitively, it queries the body inputs for a key value. diff --git a/composer.json b/composer.json index c8957b6..f93c674 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ ], "minimum-stability": "stable", "require": { - "php": ">=7.4", + "php": ">=7.2", "ext-json": "*", "initphp/parameterbag": "^1.0", "initphp/validation": "^1.0" diff --git a/src/Input.php b/src/Facede/Inputs.php similarity index 71% rename from src/Input.php rename to src/Facede/Inputs.php index b304d57..d93fbfa 100644 --- a/src/Input.php +++ b/src/Facede/Inputs.php @@ -1,22 +1,22 @@ - * @copyright Copyright © 2022 InitPHP - * @license http://initphp.github.io/license.txt MIT - * @version 1.0.2 + * @copyright Copyright © 2022 Muhammet ŞAFAK + * @license ./LICENSE MIT + * @version 1.1 * @link https://www.muhammetsafak.com.tr */ declare(strict_types=1); -namespace InitPHP\Input; +namespace InitPHP\Input\Facede; /** - * @mixin Stack + * @mixin \InitPHP\Input\Inputs * @method static mixed get(string $key, mixed $default = null, ?array $validation = null) * @method static mixed getPost(string $key, mixed $default = null, ?array $validation = null) * @method static mixed getRaw(string $key, mixed $default = null, ?array $validation = null) @@ -36,39 +36,28 @@ * @method static bool hasRaw(string $key) * @method static bool hasPost(string $key) */ -final class Input +class Inputs { - protected static Stack $stack; + /** @var \InitPHP\Input\Inputs */ + private static $Inputs; - public function __construct() + private static function getInputInstance(): \InitPHP\Input\Inputs { - self::getStackInstance(); - } - - public function __destruct() - { - if(isset(self::$stack)){ - self::$stack->__destruct(); + if(!isset(self::$Inputs)){ + self::$Inputs = new \InitPHP\Input\Inputs(); } + return self::$Inputs; } public function __call($name, $arguments) { - return self::getStackInstance()->{$name}(...$arguments); + return self::getInputInstance()->{$name}(...$arguments); } public static function __callStatic($name, $arguments) { - return self::getStackInstance()->{$name}(...$arguments); - } - - protected static function getStackInstance(): Stack - { - if(!isset(self::$stack)){ - self::$stack = new Stack(); - } - return self::$stack; + return self::getInputInstance()->{$name}(...$arguments); } } diff --git a/src/Inputs.php b/src/Inputs.php new file mode 100644 index 0000000..737fb92 --- /dev/null +++ b/src/Inputs.php @@ -0,0 +1,271 @@ + + * @copyright Copyright © 2022 Muhammet ŞAFAK + * @license ./LICENSE MIT + * @version 1.1 + * @link https://www.muhammetsafak.com.tr + */ + +declare(strict_types=1); + +namespace InitPHP\Input; + +use InitPHP\ParameterBag\ParameterBag; +use InitPHP\Validation\Validation; + +class Inputs +{ + + /** @var ParameterBag */ + private static $get; + + /** @var ParameterBag */ + private static $post; + + /** @var ParameterBag */ + private static $raw; + + /** @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(); + if(!isset(self::$validation)){ + self::$validation = new Validation(); + } + } + + public function __destruct() + { + if(isset(self::$validation)){ + self::$validation->clear(); + } + if(isset(self::$get)){ + self::$get->close(); + } + if(isset(self::$post)){ + self::$post->close(); + } + if(isset(self::$raw)){ + self::$raw->close(); + } + } + + + public function get(string $key, $default = null, ?array $validation = null) + { + if(!self::$get->has($key)){ + return $default; + } + $data = self::$get->get($key, $default); + if(empty($validation)){ + return $data; + } + return $this->validData(self::$get->all(), $key, $validation) ? $data : $default; + } + + public function post(string $key, $default = null, ?array $validation = null) + { + if(!self::$post->has($key)){ + return $default; + } + $data = self::$post->get($key, $default); + if(empty($validation)){ + return $data; + } + return $this->validData(self::$post->all(), $key, $validation) ? $data : $default; + } + + public function raw(string $key, $default = null, ?array $validation = null) + { + if(!self::$raw->has($key)){ + return $default; + } + $data = self::$raw->get($key, $default); + if(empty($validation)){ + return $data; + } + return $this->validData(self::$raw->all(), $key, $validation) ? $data : $default; + } + + public function getPost(string $key, $default = null, ?array $validation = null) + { + return self::$get->has($key) ? $this->get($key, $default, $validation) : $this->post($key, $default, $validation); + } + + public function getRaw(string $key, $default = null, ?array $validation = null) + { + return self::$get->has($key) ? $this->get($key, $default, $validation) : $this->raw($key, $default, $validation); + } + + public function getPostRaw(string $key, $default = null, ?array $validation = null) + { + if(self::$get->has($key)){ + return $this->get($key, $default, $validation); + } + return self::$post->has($key) ? $this->post($key, $default, $validation) : $this->raw($key, $default, $validation); + } + + public function getRawPost(string $key, $default = null, ?array $validation = null) + { + if(self::$get->has($key)){ + return $this->get($key, $default, $validation); + } + return self::$raw->has($key) ? $this->raw($key, $default, $validation) : $this->post($key, $default, $validation); + } + + public function postGet(string $key, $default = null, ?array $validation = null) + { + return self::$post->has($key) ? $this->post($key, $default, $validation) : $this->get($key, $default, $validation); + } + + public function postRaw(string $key, $default = null, ?array $validation = null) + { + return self::$post->has($key) ? $this->post($key, $default, $validation) : $this->raw($key, $default, $validation); + } + + public function postGetRaw(string $key, $default = null, ?array $validation = null) + { + if(self::$post->has($key)){ + return $this->post($key, $default, $validation); + } + return self::$get->has($key) ? $this->get($key, $default, $validation) : $this->raw($key, $default, $validation); + } + + public function postRawGet(string $key, $default = null, ?array $validation = null) + { + if(self::$post->has($key)){ + return $this->post($key, $default, $validation); + } + return self::$raw->has($key) ? $this->raw($key, $default, $validation) : $this->get($key, $default, $validation); + } + + public function rawGet(string $key, $default = null, ?array $validation = null) + { + return self::$raw->has($key) ? $this->raw($key, $default, $validation) : $this->get($key, $default, $validation); + } + + public function rawPost(string $key, $default = null, ?array $validation = null) + { + return self::$raw->has($key) ? $this->raw($key, $default, $validation) : $this->post($key, $default, $validation); + } + + public function rawGetPost(string $key, $default = null, ?array $validation = null) + { + if(self::$raw->has($key)){ + return $this->raw($key, $default, $validation); + } + return self::$get->has($key) ? $this->get($key, $default, $validation) : $this->post($key, $default, $validation); + } + + public function rawPostGet(string $key, $default = null, ?array $validation = null) + { + if(self::$raw->has($key)){ + return $this->raw($key, $default, $validation); + } + return self::$post->has($key) ? $this->post($key, $default, $validation) : $this->get($key, $default, $validation); + } + + public function hasGet(string $key): bool + { + return self::$get->has($key); + } + + public function hasPost(string $key): bool + { + return self::$post->has($key); + } + + public function hasRaw(string $key): bool + { + return self::$raw->has($key); + } + + private function validData(array $data, string $key, array $validMethods): bool + { + if(!isset(self::$validation)){ + self::$validation = new Validation(); + } + $validation = self::$validation->setData($data); + $validation->rule($key, $validMethods); + return $validation->validation() !== FALSE; + } + + private function getParameterBagBoot() + { + if(isset(self::$get)){ + return; + } + self::$get = new ParameterBag(($_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]); + } + + 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; + } + self::$raw = new ParameterBag($body, ['isMulti' => false]); + } + +} diff --git a/src/Stack.php b/src/Stack.php deleted file mode 100644 index f65d989..0000000 --- a/src/Stack.php +++ /dev/null @@ -1,246 +0,0 @@ - - * @copyright Copyright © 2022 InitPHP - * @license http://initphp.github.io/license.txt MIT - * @version 1.0.2 - * @link https://www.muhammetsafak.com.tr - */ - -declare(strict_types=1); - -namespace InitPHP\Input; - -use \InitPHP\ParameterBag\ParameterBag; -use \InitPHP\Validation\Validation; - -use function json_decode; -use function file_get_contents; -use function explode; -use function strpos; - -final class Stack -{ - - protected ParameterBag $get; - protected ParameterBag $post; - protected ParameterBag $raw; - protected Validation $validation; - - public function __construct() - { - $this->resolve(); - } - - public function __destruct() - { - if(isset($this->validation)){ - $this->validation->clear(); - } - if(isset($this->get)){ - $this->get->close(); - } - if(isset($this->post)){ - $this->post->close(); - } - if(isset($this->raw)){ - $this->raw->close(); - } - } - - public function get(string $key, $default = null, ?array $validation = null) - { - if(!$this->get->has($key)){ - return $default; - } - $data = $this->get->get($key, $default); - if(empty($validation)){ - return $data; - } - return $this->validData($this->get->all(), $key, $validation) ? $data : $default; - } - - public function post(string $key, $default = null, ?array $validation = null) - { - if(!$this->post->has($key)){ - return $default; - } - $data = $this->post->get($key, $default); - if(empty($validation)){ - return $data; - } - return $this->validData($this->post->all(), $key, $validation) ? $data : $default; - } - - public function raw(string $key, $default = null, ?array $validation = null) - { - if(!$this->raw->has($key)){ - return $default; - } - $data = $this->raw->get($key, $default); - if(empty($validation)){ - return $data; - } - return $this->validData($this->raw->all(), $key, $validation) ? $data : $default; - } - - public function getPost(string $key, $default = null, ?array $validation = null) - { - return $this->get->has($key) ? $this->get($key, $default, $validation) : $this->post($key, $default, $validation); - } - - public function getRaw(string $key, $default = null, ?array $validation = null) - { - return $this->get->has($key) ? $this->get($key, $default, $validation) : $this->raw($key, $default, $validation); - } - - public function getPostRaw(string $key, $default = null, ?array $validation = null) - { - if($this->get->has($key)){ - return $this->get($key, $default, $validation); - } - return $this->post->has($key) ? $this->post($key, $default, $validation) : $this->raw($key, $default, $validation); - } - - public function getRawPost(string $key, $default = null, ?array $validation = null) - { - if($this->get->has($key)){ - return $this->get($key, $default, $validation); - } - return $this->raw->has($key) ? $this->raw($key, $default, $validation) : $this->post($key, $default, $validation); - } - - public function postGet(string $key, $default = null, ?array $validation = null) - { - return $this->post->has($key) ? $this->post($key, $default, $validation) : $this->get($key, $default, $validation); - } - - public function postRaw(string $key, $default = null, ?array $validation = null) - { - return $this->post->has($key) ? $this->post($key, $default, $validation) : $this->raw($key, $default, $validation); - } - - public function postGetRaw(string $key, $default = null, ?array $validation = null) - { - if($this->post->has($key)){ - return $this->post($key, $default, $validation); - } - return $this->get->has($key) ? $this->get($key, $default, $validation) : $this->raw($key, $default, $validation); - } - - public function postRawGet(string $key, $default = null, ?array $validation = null) - { - if($this->post->has($key)){ - return $this->post($key, $default, $validation); - } - return $this->raw->has($key) ? $this->raw($key, $default, $validation) : $this->get($key, $default, $validation); - } - - public function rawGet(string $key, $default = null, ?array $validation = null) - { - return $this->raw->has($key) ? $this->raw($key, $default, $validation) : $this->get($key, $default, $validation); - } - - public function rawPost(string $key, $default = null, ?array $validation = null) - { - return $this->raw->has($key) ? $this->raw($key, $default, $validation) : $this->post($key, $default, $validation); - } - - public function rawGetPost(string $key, $default = null, ?array $validation = null) - { - if($this->raw->has($key)){ - return $this->raw($key, $default, $validation); - } - return $this->get->has($key) ? $this->get($key, $default, $validation) : $this->post($key, $default, $validation); - } - - public function rawPostGet(string $key, $default = null, ?array $validation = null) - { - if($this->raw->has($key)){ - return $this->raw($key, $default, $validation); - } - return $this->post->has($key) ? $this->post($key, $default, $validation) : $this->get($key, $default, $validation); - } - - public function hasGet(string $key): bool - { - return $this->get->has($key); - } - - public function hasPost(string $key): bool - { - return $this->post->has($key); - } - - public function hasRaw(string $key): bool - { - return $this->raw->has($key); - } - - protected function getValidation(): Validation - { - if(!isset($this->validation)){ - $this->validation = new Validation(); - } - return $this->validation; - } - - private function validData(array $data, string $key, array $validation): bool - { - $validate = $this->getValidation()->setData($data); - $validate->rule($key, $validation); - return $validate->validation() !== FALSE; - } - - private function resolve(): void - { - $this->get = new ParameterBag(($_GET ?? []), [ - 'isMulti' => false - ]); - - if(isset($_POST) && !empty($_POST)){ - $this->post = new ParameterBag($_POST, [ - 'isMulti' => false - ]); - } - - if(($inputs = @file_get_contents("php://input")) !== FALSE){ - $raws = json_decode($inputs, true); - $this->raw = new ParameterBag((!empty($raws) ? $raws : []) , [ - 'isMulti' => false - ]); - - if( - ($raws === null || $raws === FALSE) - && !isset($this->post) - && isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST' - && !empty($inputs) - && (strpos($inputs, '&') !== FALSE || strpos($inputs, '=') !== FALSE) - ){ - $posts = []; - $parse = explode('&', $inputs); - foreach ($parse as $argument) { - if(strpos($argument, '=') === FALSE){ - continue; - } - $value = explode('=', $argument, 2); - $posts[$value[0]] = $value[1]; - } - $this->post = new ParameterBag($posts, [ - 'isMulti' => false, - ]); - } - } - - if(!isset($this->post)){ - $this->post = new ParameterBag([], [ - 'isMulti' => false - ]); - } - } - -}