Skip to content

Commit 892e371

Browse files
committed
initial commit
0 parents  commit 892e371

15 files changed

+413
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build
2+
composer.lock
3+
vendor
4+
.idea

.travis.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
language: php
2+
3+
php:
4+
- 7.0
5+
- 7.1
6+
- 7.2
7+
8+
env:
9+
matrix:
10+
- COMPOSER_FLAGS="--prefer-lowest"
11+
- COMPOSER_FLAGS=""
12+
13+
before_script:
14+
- travis_retry composer self-update
15+
- travis_wait composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
16+
17+
script:
18+
- vendor/bin/phpunit --testdox

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Aron Rotteveel <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Laravel Request Sanitizer
2+
3+
[![Build Status](https://travis-ci.org/ArondeParon/laravel-request-sanitizer.svg?branch=master)](https://travis-ci.org/ArondeParon/laravel-request-sanitizer)
4+
5+
The `arondeparon/laravel-request-sanitizer` package provides a fluent interface to sanitize form requests before validating them.
6+
7+
## Why should I use this package?
8+
9+
Often, validating your request is not enough. The request sanitizer allows you to easily
10+
sanitize your form data before passing it to the validator. You can start using it in a matter
11+
of minutes and it is fully compatible with Laravel's `FormRequest` object.
12+
13+
## How to use
14+
15+
Syntax is similar to the way `rules` are added to a Form Request.
16+
17+
```php
18+
class StoreCustomerInformationRequest extends FormRequest
19+
{
20+
use SanitizesInputs;
21+
22+
protected $sanitizers = [
23+
'lastname' => [
24+
Capitalize::class,
25+
],
26+
'mobile_phone' => [
27+
RemoveNonNumeric::class
28+
],
29+
];
30+
}
31+
```
32+
33+
## Installing
34+
35+
`composer require arondeparon/laravel-request-sanitizer`
36+
37+
## Usage
38+
39+
- Add the `SanitizesInputs` trait to your form request.
40+
- Write your own sanitizers or use one of the supplied sanitizers and add them to the `$sanitizers`
41+
property of your form request.
42+
- Your request data will not be sanitized before being validated.
43+
44+
## Predefined Sanitizers
45+
46+
- [`RemoveNonNumeric`](./src/Sanitizers/RemoveNonNumeric.php) - removes any non numeric character
47+
- [`Trim`](./src/Sanitizers/Trim.php) - simple PHP `trim()` implementation
48+
- [`TrimDuplicateSpaces`](./src/Sanitizers/TrimDuplicateSpaces.php) replaces duplicate spaces with a single space.
49+
- ...
50+
- Contributions are appreciated!
51+
52+
## Writing your own Sanitizers
53+
54+
Writing your own sanitizer can be done by implementing the `Sanitizer` interface, which requires only
55+
one method.
56+
57+
```php
58+
interface Sanitizer
59+
{
60+
public function sanitize($input);
61+
}
62+
```
63+
64+
## Testing
65+
66+
`$ phpunit`
67+
68+
## Credits
69+
70+
- [Aron Rotteveel](https://github.com/arondeparon)
71+
- [All Contributors](../../contributors)
72+
73+
## License
74+
75+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

composer.json

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "arondeparon/laravel-request-sanitizer",
3+
"description": "An easy to use request sanitizer that allows you to sanitize your form data before validating it.",
4+
"homepage": "https://github.com/arondeparon/laravel-request-sanitizer",
5+
"keywords": [
6+
"laravel",
7+
"arondeparon",
8+
"request",
9+
"sanitizer",
10+
"validation",
11+
"form"
12+
],
13+
"type": "library",
14+
"authors": [
15+
{
16+
"name": "Aron Rotteveel",
17+
"email": "[email protected]",
18+
"homepage": "https://github.com/arondeparon",
19+
"role": "Developer"
20+
}
21+
],
22+
"require": {
23+
"php": "^7.0",
24+
"illuminate/contracts": "~5.5.0|~5.6.0",
25+
"illuminate/support": "~5.5.0|~5.6.0"
26+
},
27+
"require-dev": {
28+
"phpunit/phpunit": "^6.2|7.2",
29+
"orchestra/testbench": "~3.5.0|~3.6.0",
30+
"mockery/mockery": "0.9.*"
31+
},
32+
"autoload": {
33+
"psr-4": {
34+
"ArondeParon\\RequestSanitizer\\": "src"
35+
}
36+
},
37+
"autoload-dev": {
38+
"psr-4": {
39+
"ArondeParon\\RequestSanitizer\\Tests\\": "tests"
40+
}
41+
},
42+
"scripts": {
43+
"test": "vendor/bin/phpunit"
44+
},
45+
"license": "MIT"
46+
}

phpunit.xml.dist

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="Laravel Request Sanitizer Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
</phpunit>

src/Contracts/Sanitizer.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace ArondeParon\RequestSanitizer\Contracts;
4+
5+
interface Sanitizer
6+
{
7+
/**
8+
* Sanitize an input and return it.
9+
*
10+
* @param $input
11+
* @return mixed
12+
*/
13+
public function sanitize($input);
14+
}

src/RequestSanitizerServiceProvider.php

Whitespace-only changes.

src/Sanitizers/RemoveNonNumeric.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace ArondeParon\RequestSanitizer\Sanitizers;
4+
5+
use ArondeParon\RequestSanitizer\Contracts\Sanitizer;
6+
7+
class RemoveNonNumeric implements Sanitizer
8+
{
9+
/**
10+
* @param $input
11+
* @return string
12+
*/
13+
public function sanitize($input)
14+
{
15+
return preg_replace('~[^0-9]+~', '', $input);
16+
}
17+
}

src/Sanitizers/Trim.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace ArondeParon\RequestSanitizer\Sanitizers;
4+
5+
use ArondeParon\RequestSanitizer\Contracts\Sanitizer;
6+
7+
class Trim implements Sanitizer
8+
{
9+
/**
10+
* @param $input
11+
* @return string
12+
*/
13+
public function sanitize($input)
14+
{
15+
return trim($input);
16+
}
17+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace ArondeParon\RequestSanitizer\Sanitizers;
4+
5+
use ArondeParon\RequestSanitizer\Contracts\Sanitizer;
6+
7+
class TrimDuplicateSpaces implements Sanitizer
8+
{
9+
/**
10+
* @param $input
11+
* @return string
12+
*/
13+
public function sanitize($input)
14+
{
15+
return preg_replace('~\s{2,}~', ' ', $input);
16+
}
17+
}

src/Traits/SanitizesInputs.php

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace ArondeParon\RequestSanitizer\Traits;
4+
5+
use ArondeParon\RequestSanitizer\Contracts\Sanitizer;
6+
7+
trait SanitizesInputs
8+
{
9+
/**
10+
* Get data to be validated from the request.
11+
*
12+
* @return array
13+
*/
14+
protected function validationData()
15+
{
16+
// Sanitize data before passing it to the validator.
17+
$this->sanitize();
18+
19+
return $this->all();
20+
}
21+
22+
public function sanitize()
23+
{
24+
$input = $this->all();
25+
26+
if (!property_exists($this, 'sanitizers')) {
27+
$this->sanitizers = [];
28+
}
29+
30+
foreach ($this->sanitizers as $formKey => $sanitizers) {
31+
$sanitizers = (array) $sanitizers;
32+
foreach ($sanitizers as $sanitizer) {
33+
if (is_string($sanitizer)) {
34+
$sanitizer = app()->make($sanitizer);
35+
}
36+
$input[$formKey] = $sanitizer->sanitize($input[$formKey] ?? null);
37+
}
38+
}
39+
40+
return $this->replace($input);
41+
}
42+
43+
/**
44+
* Add a single sanitizer.
45+
*
46+
* @param string $formKey
47+
* @param Sanitizer $sanitizer
48+
* @return $this
49+
*/
50+
public function addSanitizer(string $formKey, $sanitizer)
51+
{
52+
if (!isset($this->sanitizers[$formKey])) {
53+
$this->sanitizers[$formKey] = [];
54+
}
55+
56+
$this->sanitizers[$formKey][] = $sanitizer;
57+
58+
return $this;
59+
}
60+
61+
/**
62+
* Add multiple sanitizers.
63+
*
64+
* @param $formKey
65+
* @param array $sanitizers
66+
* @return $this
67+
*/
68+
public function addSanitizers($formKey, $sanitizers = [])
69+
{
70+
foreach ($sanitizers as $sanitizer) {
71+
$this->addSanitizer($formKey, $sanitizer);
72+
}
73+
74+
return $this;
75+
}
76+
77+
/**
78+
* @param null $formKey
79+
* @return array
80+
*/
81+
public function getSanitizers($formKey = null)
82+
{
83+
if ($formKey !== null) {
84+
return $this->sanitizers[$formKey] ?? [];
85+
}
86+
87+
return $this->sanitizers;
88+
}
89+
}

tests/Objects/Request.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace ArondeParon\RequestSanitizer\Tests\Objects;
4+
5+
use ArondeParon\RequestSanitizer\Traits\SanitizesInputs;
6+
use Illuminate\Foundation\Http\FormRequest;
7+
8+
class Request extends FormRequest
9+
{
10+
use SanitizesInputs;
11+
}

0 commit comments

Comments
 (0)