Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
sharifzadesina committed Aug 19, 2020
1 parent 29c47a5 commit 88d3211
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 30 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
}
},
"autoload-dev": {
"classmap": ["tests"]
"psr-4": {
"Elegant\\Sanitizer\\Tests\\": "tests/"
}
},
"extra": {
"laravel": {
Expand Down
54 changes: 35 additions & 19 deletions src/Sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ class Sanitizer
{
/**
* Data to sanitize.
*
*
* @var array
*/
protected $data;

/**
* Filters to apply.
*
*
* @var array
*/
protected $rules;

/**
* Available filters as $name => $classPath.
*
*
* @var array
*/
protected $filters = [
Expand Down Expand Up @@ -79,11 +79,8 @@ protected function parseRules(array $rules)
$rawRules = (new ValidationRuleParser($this->data))->explode($rules);

foreach ($rawRules->rules as $attribute => $attributeRules) {
foreach ($attributeRules as $attributeRule) {
$parsedRule = $this->parseRule($attributeRule);
if ($parsedRule) {
$parsedRules[$attribute][] = $parsedRule;
}
foreach (array_filter($attributeRules) as $attributeRule) {
$parsedRules[$attribute][] = $this->parseRule($attributeRule);
}
}

Expand All @@ -94,6 +91,7 @@ protected function parseRules(array $rules)
* Parse a rule.
*
* @param string|Closure $rule
* @throws InvalidArgumentException for unsupported rule type
* @return array|Closure
*/
protected function parseRule($rule)
Expand All @@ -111,10 +109,15 @@ protected function parseRule($rule)
* Parse a rule string formatted as filterName:option1, option2 into an array formatted as [name => filterName, options => [option1, option2]]
*
* @param string $rule Formatted as 'filterName:option1, option2' or just 'filterName'
* @return array Formatted as [name => filterName, options => [option1, option2]]. Empty array if no filter name was found.
* @throws InvalidArgumentException for empty rule string
* @return array Formatted as [name => filterName, options => [option1, option2]]
*/
protected function parseRuleString($rule)
{
if ('' == $rule) {
throw new InvalidArgumentException("Invalid rule string.");
}

if (strpos($rule, ':') !== false) {
list($name, $options) = explode(':', $rule, 2);
$options = array_map('trim', explode(',', $options));
Expand All @@ -123,18 +126,18 @@ protected function parseRuleString($rule)
$options = [];
}

if (!$name) {
return [];
}

return compact('name', 'options');
return [
'name' => $name,
'options' => $options,
];
}

/**
* Apply the given filter by its name
* Apply the given filter to the value.
*
* @param string|Closure $rule
* @return Filter
* @param mixed $value
* @return mixed
*/
protected function applyFilter($rule, $value)
{
Expand All @@ -161,6 +164,21 @@ protected function applyFilter($rule, $value)
}
}

/**
* Apply the given filters to the value.
*
* @param string|Closure $rule
* @param mixed $value
* @return mixed
*/
protected function applyFilters(array $rules, $value)
{
foreach ($rules as $rule)
$value = $this->applyFilter($rule, $value);

return $value;
}

/**
* Sanitize the given data.
*
Expand All @@ -172,9 +190,7 @@ public function sanitize()

foreach ($this->rules as $attr => $rules) {
if (Arr::has($sanitized, $attr)) {
foreach ($rules as $rule) {
Arr::set($sanitized, $attr, $this->applyFilter($rule, Arr::get($sanitized, $attr)));
}
Arr::set($sanitized, $attr, $this->applyFilters($rules, Arr::get($sanitized, $attr)));
}
}

Expand Down
7 changes: 5 additions & 2 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

use PHPUnit\Framework\TestCase;
namespace Elegant\Sanitizer\Tests;

use Elegant\Sanitizer\Tests\Fixtures\Filters\CustomFilter;
use Elegant\Sanitizer\Laravel\Factory;
use PHPUnit\Framework\TestCase;

class FactoryTest extends TestCase
{
Expand Down Expand Up @@ -53,7 +56,7 @@ public function test_replace_filter()
'name' => 'trim',
];
$newData = $factory->make($data, $rules)->sanitize();

$this->assertEquals(sha1('TEST'), $newData['name']);
}
}
3 changes: 3 additions & 0 deletions tests/Filters/CapitalizeTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

namespace Elegant\Sanitizer\Tests\Filters;

use Elegant\Sanitizer\Tests\SanitizesData;
use PHPUnit\Framework\TestCase;

class CapitalizeTest extends TestCase
Expand Down
5 changes: 4 additions & 1 deletion tests/Filters/CastTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

namespace Elegant\Sanitizer\Tests\Filters;

use Elegant\Sanitizer\Tests\SanitizesData;
use PHPUnit\Framework\TestCase;

class CastTest extends TestCase
Expand Down Expand Up @@ -115,7 +118,7 @@ public function test_casts_json_to_collection()
];
$encodedData = json_encode($data);
$results = $this->sanitize(['var' => $encodedData], ['var' => 'cast:collection']);

$this->assertInstanceOf('\Illuminate\Support\Collection', $results['var']);
$this->assertEquals('Name', $results['var']->first());
}
Expand Down
5 changes: 4 additions & 1 deletion tests/Filters/DigitTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

namespace Elegant\Sanitizer\Tests\Filters;

use Elegant\Sanitizer\Tests\SanitizesData;
use PHPUnit\Framework\TestCase;

class DigitTest extends TestCase
Expand Down Expand Up @@ -28,7 +31,7 @@ public function test_string_to_digits2()
'name' => 'digit',
];
$data = $this->sanitize($data, $rules);

$this->assertEquals('', $data['name']);
}
}
5 changes: 4 additions & 1 deletion tests/Filters/EscapeHTMLTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

namespace Elegant\Sanitizer\Tests\Filters;

use Elegant\Sanitizer\Tests\SanitizesData;
use PHPUnit\Framework\TestCase;

class EscapeHTMLTest extends TestCase
Expand All @@ -15,7 +18,7 @@ public function test_escapes_strings()
'name' => 'escape',
];
$data = $this->sanitize($data, $rules);

$this->assertEquals('Hello! Unicode chars as Ñ are not escaped. Neither is content inside HTML tags', $data['name']);
}
}
5 changes: 4 additions & 1 deletion tests/Filters/FormatDateTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

namespace Elegant\Sanitizer\Tests\Filters;

use Elegant\Sanitizer\Tests\SanitizesData;
use PHPUnit\Framework\TestCase;

class FormatDateTest extends TestCase
Expand All @@ -22,7 +25,7 @@ public function test_formats_dates()
public function test_requires_two_arguments()
{
$this->expectException(\InvalidArgumentException::class);

$data = [
'name' => '21/03/1983',
];
Expand Down
5 changes: 4 additions & 1 deletion tests/Filters/LowercaseTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

namespace Elegant\Sanitizer\Tests\Filters;

use Elegant\Sanitizer\Tests\SanitizesData;
use PHPUnit\Framework\TestCase;

class LowercaseTest extends TestCase
Expand Down Expand Up @@ -28,7 +31,7 @@ public function test_lowercases_special_characters_strings()
'name' => 'lowercase',
];
$data = $this->sanitize($data, $rules);

$this->assertEquals('τάχιστη αλώπηξ', $data['name']);
}
}
5 changes: 4 additions & 1 deletion tests/Filters/StripTagsTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

namespace Elegant\Sanitizer\Tests\Filters;

use Elegant\Sanitizer\Tests\SanitizesData;
use PHPUnit\Framework\TestCase;

class StripTagsTest extends TestCase
Expand All @@ -15,7 +18,7 @@ public function test_trims_strings()
'name' => 'strip_tags',
];
$data = $this->sanitize($data, $rules);

$this->assertEquals('Test paragraph. Other text', $data['name']);
}
}
3 changes: 3 additions & 0 deletions tests/Filters/TrimTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

namespace Elegant\Sanitizer\Tests\Filters;

use Elegant\Sanitizer\Tests\SanitizesData;
use PHPUnit\Framework\TestCase;

class TrimTest extends TestCase
Expand Down
3 changes: 3 additions & 0 deletions tests/Filters/UppercaseTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

namespace Elegant\Sanitizer\Tests\Filters;

use Elegant\Sanitizer\Tests\SanitizesData;
use PHPUnit\Framework\TestCase;

class UppercaseTest extends TestCase
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Elegant\Sanitizer\Tests\Fixtures\Filters;

use Elegant\Sanitizer\Contracts\Filter;

class CustomFilter implements Filter
Expand Down
35 changes: 35 additions & 0 deletions tests/SanitizerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

namespace Elegant\Sanitizer\Tests;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

class SanitizerTest extends TestCase
Expand Down Expand Up @@ -112,4 +115,36 @@ public function test_closure_rule()

$this->assertEquals('SINA', $data['name']);
}

public function test_removed_array_elements_are_persistent()
{
$actual = null;

$data = [
'users' => [
['name' => 'Mohammad', 'age' => 32],
['name' => 'Ali', 'age' => 25]
]
];
$rules = [
'users' => [function ($value) {
unset($value[0]);
return $value;
}],
'users.*.age' => [function ($value) use (&$actual) {
$actual[] = $value;
return $value;
}]
];
$data = $this->sanitize($data, $rules);

$sanitized = [
'users' => [
1 => ['name' => 'Ali', 'age' => 25]
]
];

$this->assertEquals(['25'], $actual);
$this->assertEquals($sanitized, $data);
}
}
6 changes: 4 additions & 2 deletions tests/SanitizesData.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<?php

namespace Elegant\Sanitizer\Tests;

use Elegant\Sanitizer\Sanitizer;

trait SanitizesData
{
/**
* Sanitizes the data.
*
*
* @param array $data
* @param array $data
* @return array
*/
public function sanitize(array $data, array $rules)
{
$sanitizer = new Sanitizer($data, $rules);

return $sanitizer->sanitize();
}
}

0 comments on commit 88d3211

Please sign in to comment.