buuum / requestcheck
Filter and Validation data
Requires
- php: >=7.1.0
Requires (Dev)
README
Install
System Requirements
You need PHP >= 7.1.0 to use Buuum\RequestCheck but the latest stable version of PHP is recommended.
Composer
Buuum\RequestCheck is available on Packagist and can be installed using Composer:
composer require buuum/requestcheck
Manually
You may use your own autoloader as long as it follows PSR-0 or PSR-4 standards. Just put src directory contents in your vendor directory.
### INPUT FIELDS
INPUT
$name = new Input('name'); $name->setFilters([new FilterTrim()]); $name->setValidations([new ValidRequired()]);
INPUTOBJECT
$name = new Input('name'); $name->setFilters([new FilterTrim()]); $name->setValidations([new ValidRequired()]); $url = new Input('url'); $url->setValidations([new ValidRequired()]); $imageobject = new InputObject('image', new InputCollection([$name, $url])); $imageobject->setValidations([new ValidRequired()]);
INPUTARRAY
// Simple Array $url = new Input('url'); $url->setValidations([new ValidRequired()]); $urls = new InputArray('urls', $url); $urls->setValidations([new ValidRequired()]); // Array objects ... $images = new InputArray('urls', $imageobject); // Array arrays ... $images_array = new InputArray('images_array', $images);
HOW TO USE
$data = [ 'name' => ' dr r rwe wed ', 'url' => ' url', ]; $fields = [$name, $url]; $request = new RequestCheck($data, new InputCollection($fields)); // return RequestResponse $response = $request->checkRequest(); if($response->isValid()){ // no errors var_dump($request->getData()); }else{ var_dump($response->getErrors()); }
FILTERS
Attributes
remove all attributes
... $name->setFilters([new FilterAttributes()]);
Custom Tags
remove custom tags
- param1: allow tags
... $name->setFilters([new FilterCustomTags('<p><a>')]);
return filter email
... $name->setFilters([new FilterEmail()]);
HtmlEncode
return htmlencode data
... $name->setFilters([new FilterHtmlEncode()]);
RemovePunctuation
remove punctuation
... $name->setFilters([new FilterRemovePunctuation()]);
Sanitize Number
sanitize number data
... $name->setFilters([new FilterSanitizeNumber()]);
String
sanitize string data
... $name->setFilters([new FilterString()]);
Trim
trim data
... $name->setFilters([new FilterTrim()]);
UrlEncode
encode url
... $name->setFilters([new FilterUrlEncode()]);
Whole Number
whole number
... $name->setFilters([new FilterWholeNumber()]);
Date
Format date
- param1: string input_format
- param2: string output_format
... $name->setFilters([new FilterDate('m/y', 'ym')]);
Create custom filter
class FilterLetter implements Filter { protected $letter; public function __construct($letter) { $this->letter = $letter; } public function filter($data) { return str_replace($this->letter,'', $data); } } ... $name->setFilters([new FilterLetter('a')]);
VALIDATORS
REQUIRED
Check if input exists
- param1: string (custom message)
... $name->setValidations([new ValidRequired()]);
Check if input is a valid email
- param1: string (custom message)
... $name->setValidations([new ValidEmail()]); ### Integer Check if input is a valid integer - param1: string (custom message) ```php ... $name->setValidations([new ValidInteger()]);
Exact
Check if input value is exact to value
- param1: int value to exact
- param2: string (custom message)
... $name->setValidations([new ValidExact(34)]);
Exact Length
Check if input value has the exaxt length
- param1: int length
- param2: string (custom message)
... $name->setValidations([new ValidExactLength(34)]);
MAX
Check if input value is max
- param1: int value max
- param2: string (custom message)
... $name->setValidations([new ValidMax(34)]);
MIN
Check if input value is min
- param1: int value min
- param2: string (custom message)
... $name->setValidations([new ValidMin(34)]);
MAXLENGTH
Check if input value is max
- param1: int value max
- param2: string (custom message)
... $name->setValidations([new ValidMaxLength(34)]);
MINLENGTH
Check if input value is min
- param1: int value min
- param2: string (custom message)
... $name->setValidations([new ValidMinLength(34)]);
REGEX
Check if input is valid with regex
- param1: string regex
- param2: string (custom message)
PREDEFINE REGEXS:
- NUMERIC
- ALPHA
- ALPHA_NUMERIC
- ALPHA_SPACE
- ALPHA_NUMERIC_SPACE
- SLUG
... $name->setValidations([new ValidRegex(ValidRegex::NUMERIC)]);
URL
Check if input is valid url
- param1: string (custom message)
... $name->setValidations([new ValiUrl(34)]);
Contains
Check if string contains any value of array
- param1: array
- param2: string (custom message)
... $words = ['blue', 'yellow']; $name->setValidations([new ValidContains($words)]);
Date
Check if input is a valid date
- param1: string date format
- param2: string (custom message)
... $format = 'Y/m/d'; $name->setValidations([new ValidDate($format)]);
LICENSE
The MIT License (MIT)
Copyright (c) 2017
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.