Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#4 :: Add new regexp sanitizer type #5

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ First, you must define your template
'double' => 'double',
'boolean' => 'bool',
'datetime' => 'datetime',
'validation_code' => 'regexp[^[0-9]{1,6}$]',
"persons[]" => [
'name' => 'string',
'eyes' => 'int',
Expand Down Expand Up @@ -73,6 +74,7 @@ After that, you are good to sanitize any input
'double' => '7876',
'boolean' => true,
'datetime' => '2017-11-11 13:50:10',
'validation_code' => 123456,
'persons' => [
[
'name' => 'jhon',
Expand Down Expand Up @@ -132,6 +134,8 @@ Output of the previous call
[timezone] => America/Santiago
)

[validation_code] => 123456

[persons] => Array
(
[0] => Array
Expand Down
11 changes: 10 additions & 1 deletion src/Sanitizer/Template/TemplateSanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Gcore\Sanitizer\Type\TypeString;
use Gcore\Sanitizer\Type\TypeFixedValues;
use Gcore\Sanitizer\Template\RequiredFieldsException;
use Gcore\Sanitizer\Type\TypeRegexp;
use InvalidArgumentException;
use LogicException;
use RuntimeException;
Expand Down Expand Up @@ -75,7 +76,7 @@ public function getType($rule, string $fieldName = '') : TypeInterface
case 'datetime' : $type = new TypeDatetime; break;
case 'double' : $type = new TypeDouble; break;
case 'email' : $type = new TypeEmail; break;
default: throw new InvalidArgumentException('Invalid type ' . $rule);
default: $type = $this->getCustomSanitizer($rule);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this function public

}

$type = $isList ? new TypeArray($type) : $type;
Expand Down Expand Up @@ -220,4 +221,12 @@ private static function isAssoc(array $arr) : bool
if (array() === $arr) return false;
return array_keys($arr) !== range(0, count($arr) - 1);
}

private function getCustomSanitizer(string $rule) {
if (strpos($rule, 'regexp') > 0) {
return new TypeRegexp;
}

throw new InvalidArgumentException('Invalid type ' . $rule);
}
}
17 changes: 17 additions & 0 deletions src/Sanitizer/Type/TypeRegexp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php namespace Gcore\Sanitizer\Type;

use Gcore\Sanitizer\Type\TypeInterface;

/**
* Class to sanitize regular expressions
*/
class TypeRegexp implements TypeInterface
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change name to AcmeClass

{
/**
* {@inheritDoc}
*/
public function sanitize($value)
{
return $value;
}
}