From ece9d3090507bca7ea880c749b1a98cd8ad26517 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Thu, 20 May 2021 15:03:56 +0200 Subject: [PATCH] Added support for InputHandler::exists to check array of indexes. - Updated documentation to reflect changes. --- README.md | 22 ++++++++++++++++++++++ src/Pecee/Http/Input/InputHandler.php | 18 +++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 29cd8c5..92e2b1b 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ You can donate any amount of your choice by [clicking here](https://www.paypal.c - [Get parameter object](#get-parameter-object) - [Managing files](#managing-files) - [Get all parameters](#get-all-parameters) + - [Check if parameters exists](#check-if-parameters-exists) - [Events](#events) - [Available events](#available-events) - [Registering new event](#registering-new-event) @@ -1236,8 +1237,11 @@ $values = input()->all([ All object implements the `IInputItem` interface and will always contain these methods: - `getIndex()` - returns the index/key of the input. +- `setIndex()` - set the index/key of the input. - `getName()` - returns a human friendly name for the input (company_name will be Company Name etc). +- `setName()` - sets a human friendly name for the input (company_name will be Company Name etc). - `getValue()` - returns the value of the input. +- `setValue()` - sets the value of the input. `InputFile` has the same methods as above along with some other file-specific methods like: @@ -1253,6 +1257,24 @@ All object implements the `IInputItem` interface and will always contain these m --- +### Check if parameters exists + +You can easily if multiple items exists by using the `exists` method. It's simular to `value` as it can be used +to filter on request-methods and supports both `string` and `array` as parameter value. + +**Example:** + +```php +if(input()->exists(['name', 'lastname'])) { + // Do stuff +} + +/* Similar to code above */ +if(input()->exists('name') && input()->exists('lastname')) { + // Do stuff +} +``` + # Events This section will help you understand how to register your own callbacks to events in the router. diff --git a/src/Pecee/Http/Input/InputHandler.php b/src/Pecee/Http/Input/InputHandler.php index aa54c6d..e909382 100644 --- a/src/Pecee/Http/Input/InputHandler.php +++ b/src/Pecee/Http/Input/InputHandler.php @@ -293,14 +293,26 @@ class InputHandler } /** - * Check if a input-item exist + * Check if a input-item exist. + * If an array is as $index parameter the method returns true if all elements exist. * - * @param string $index + * @param string|array $index * @param array ...$methods * @return bool */ - public function exists(string $index, ...$methods): bool + public function exists($index, ...$methods): bool { + // Check array + if(is_array($index) === true) { + foreach($index as $key) { + if($this->value($key, null, ...$methods) === null) { + return false; + } + } + + return true; + } + return $this->value($index, null, ...$methods) !== null; }