Skip to content

Commit

Permalink
Merge pull request #19 from ArondeParon/bugfix/sanitizer-chaining
Browse files Browse the repository at this point in the history
Bugfix/sanitizer chaining
  • Loading branch information
arondeparon authored Jan 11, 2021
2 parents 54af340 + c7d19c3 commit f9d3647
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Traits/SanitizesInputs.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace ArondeParon\RequestSanitizer\Traits;

use ArondeParon\RequestSanitizer\Contracts\Sanitizer;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use ArondeParon\RequestSanitizer\Contracts\Sanitizer;

trait SanitizesInputs
{
Expand Down Expand Up @@ -42,7 +42,10 @@ public function sanitize()
} else {
throw new InvalidArgumentException('Could not resolve sanitizer from given properties');
}

Arr::set($input, $formKey, $sanitizer->sanitize($this->input($formKey, null)));

$this->replace($input);
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/SanitizesInputsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace ArondeParon\RequestSanitizer\Tests;

use ArondeParon\RequestSanitizer\Contracts\Sanitizer;
use ArondeParon\RequestSanitizer\Sanitizers\Capitalize;
use ArondeParon\RequestSanitizer\Sanitizers\Lowercase;
use ArondeParon\RequestSanitizer\Sanitizers\TrimDuplicateSpaces;
use ArondeParon\RequestSanitizer\Tests\Objects\Request;
use Illuminate\Validation\ValidationException;
Expand Down Expand Up @@ -56,6 +58,23 @@ public function test_it_will_call_each_sanitizer_if_the_key_exists()
$request->validateResolved();
}

public function test_it_will_chain_the_result_of_each_sanitizer()
{
$sanitizers = [
Lowercase::class,
Capitalize::class,
];

$request = $this->createRequest([
'foo' => 'this is a lower case string with a CAPITAL word'
]);
$request->addSanitizers('foo', $sanitizers);

$request->validateResolved();

$this->assertEquals('This is a lower case string with a capital word', $request->input('foo'));
}

public function test_it_will_handle_dot_notation()
{
$request = $this->createRequest([
Expand Down

0 comments on commit f9d3647

Please sign in to comment.