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

Alnum and letters filter rules #27

Merged
merged 3 commits into from
Jun 12, 2015
Merged
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
70 changes: 47 additions & 23 deletions docs/filter-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
Particle\Filter tries to provide you the most common filters. An overview is listed below. If you want to add custom
filters, take a look at the callback filter-rule, or check out "Extending the Filter" in the menu.

* [alnum](#alnum)
* [append](#append)
* [bool](#bool)
* [callback](#callback)
* [float](#float)
* [int](#int)
* [letters](#letters)
* [lower](#lower)
* [numbers](#numbers)
* [prepend](#prepend)
Expand All @@ -19,15 +21,26 @@ filters, take a look at the callback filter-rule, or check out "Extending the Fi
* [upper](#upper)
* [upperFirst](#upperfirst)

## Alnum

Filters everything but alphabetic numeric characters out of the value

```php
$f = new Filter;
$f->value('name')->alnum();
$result = $f->filter(['name' => '1!23-abc?']);
// array(1) { ["name"]=> string(6) "123abc"
```

## Append

Appends a value to the end of the provided value.

```php
$f = new Filter;
$f->value('name')->append(' is your name.');
$result = $f->filter(['name' => 'Rick']);
// array(1) { ["name"]=> string(18) "Rick is your name."
$result = $f->filter(['name' => 'John']);
// array(1) { ["name"]=> string(18) "John is your name."
```

## Bool
Expand All @@ -51,8 +64,8 @@ $f = new Filter;
$f->value('name')->callback(function($value) {
return '<strong>' . $value . '</strong>';
});
$result = $f->filter(['name' => 'Rick']);
// array(1) { ["name"]=> string(21) "<strong>Rick</strong>"
$result = $f->filter(['name' => 'John']);
// array(1) { ["name"]=> string(21) "<strong>John</strong>"
```

## Float
Expand All @@ -77,15 +90,26 @@ $result = $f->filter(['value' => '123.123']);
// array(1) { ["value"]=> int(123) }
```

## Letters

Filters everything but letters out of the value

```php
$f = new Filter;
$f->value('name')->letters();
$result = $f->filter(['name' => 'john99!']);
// array(1) { ["name"]=> string(4) "john"
```

## Lower

Lowercase the full value.

```php
$f = new Filter;
$f->value('name')->lower();
$result = $f->filter(['name' => 'RICK']);
// array(1) { ["name"]=> string(4) "rick"
$result = $f->filter(['name' => 'JOHN']);
// array(1) { ["name"]=> string(4) "john"
```

## Numbers
Expand All @@ -106,8 +130,8 @@ Appends a value to the end of the provided value.
```php
$f = new Filter;
$f->value('name')->prepend('Hello ');
$result = $f->filter(['name' => 'Rick']);
// array(1) { ["name"]=> string(10) "Hello Rick"
$result = $f->filter(['name' => 'John']);
// array(1) { ["name"]=> string(10) "Hello John"
```

## RegexReplace
Expand All @@ -128,8 +152,8 @@ Replace a needle for a replacement in the value.
```php
$f = new Filter;
$f->value('value')->replace(' ', '-');
$result = $f->filter(['name' => 'hello im rick']);
// array(1) { ["name"]=> string(13) "hello-im-rick" }
$result = $f->filter(['name' => 'hello im john']);
// array(1) { ["name"]=> string(13) "hello-im-john" }
```

## String
Expand All @@ -150,17 +174,17 @@ Strip all tags from the value.
```php
$f = new Filter;
$f->value('name')->stripHtml();
$result = $f->filter(['name' => '<p><strong>Rick</strong></p>']);
// array(1) { ["name"]=> string(4) "Rick"
$result = $f->filter(['name' => '<p><strong>John</strong></p>']);
// array(1) { ["name"]=> string(4) "John"
```

Exclude some tags:

```php
$f = new Filter;
$f->value('name')->stripHtml('<strong>');
$result = $f->filter(['name' => '<p><strong>Rick</strong></p>']);
// array(1) { ["name"]=> string(21) "<strong>Rick</strong>"
$result = $f->filter(['name' => '<p><strong>John</strong></p>']);
// array(1) { ["name"]=> string(21) "<strong>John</strong>"
```

## Trim
Expand All @@ -170,17 +194,17 @@ Strip all 'white-space' characters from the beginning and end of the string.
```php
$f = new Filter;
$f->value('name')->trim();
$result = $f->filter(['name' => ' Rick ']);
// array(1) { ["name"]=> string(4) "Rick"
$result = $f->filter(['name' => ' John ']);
// array(1) { ["name"]=> string(4) "John"
```

You can also provide the specific characters that you want to strip.

```php
$f = new Filter;
$f->value('name')->trim("\s");
$result = $f->filter(['name' => ' Rick ']);
// array(1) { ["name"]=> string(4) "Rick"
$result = $f->filter(['name' => ' John ']);
// array(1) { ["name"]=> string(4) "John"
```

## Upper
Expand All @@ -190,8 +214,8 @@ Uppercase the full value.
```php
$f = new Filter;
$f->value('name')->upper();
$result = $f->filter(['name' => 'rick']);
// array(1) { ["name"]=> string(4) "RICK"
$result = $f->filter(['name' => 'john']);
// array(1) { ["name"]=> string(4) "JOHN"
```

## UpperFirst
Expand All @@ -201,6 +225,6 @@ Uppercase the first character of the value.
```php
$f = new Filter;
$f->value('name')->upperFirst();
$result = $f->filter(['name' => 'rick']);
// array(1) { ["name"]=> string(4) "Rick"
```
$result = $f->filter(['name' => 'john']);
// array(1) { ["name"]=> string(4) "John"
```
40 changes: 30 additions & 10 deletions src/FilterResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ public function __construct(Filter $filter, $keys = null)
$this->keys = $keys;
}

/**
* Add the alphabetic numeric rule to the chain
*
* @return $this
*/
public function alnum()
{
return $this->addRule(new FilterRule\Alnum);
}

/**
* Add append filter-rule to the chain
*
Expand Down Expand Up @@ -85,6 +95,16 @@ public function int()
return $this->addRule(new FilterRule\CastInt);
}

/**
* Add the letters-rule to the chain
*
* @return $this
*/
public function letters()
{
return $this->addRule(new FilterRule\Letters);
}

/**
* Add lower filter-rule to the chain
*
Expand All @@ -95,6 +115,16 @@ public function lower()
return $this->addRule(new FilterRule\Lower);
}

/**
* Add the numbers-rule to the chain
*
* @return $this
*/
public function numbers()
{
return $this->addRule(new FilterRule\Numbers);
}

/**
* Add prepend filter-rule to the chain
*
Expand Down Expand Up @@ -181,16 +211,6 @@ public function upperFirst()
{
return $this->addRule(new FilterRule\UpperFirst);
}

/**
* Add the numbers-rule to the chain
*
* @return $this
*/
public function numbers()
{
return $this->addRule(new FilterRule\Numbers);
}

/**
* Add a new rule to the chain
Expand Down
30 changes: 30 additions & 0 deletions src/FilterRule/Alnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Particle.
*
* @link http://github.com/particle-php for the canonical source repository
* @copyright Copyright (c) 2005-2015 Particle (http://particle-php.com)
* @license https://github.com/particle-php/Filter/blob/master/LICENSE New BSD License
*/
namespace Particle\Filter\FilterRule;

use Particle\Filter\FilterRule;

/**
* Class AlNum
*
* @package Particle\Filter\FilterRule
*/
class AlNum extends FilterRule
{
/**
* Only return alphabetic numeric characters
*
* @param mixed $value
* @return string
*/
public function filter($value)
{
return preg_replace('/\W|_/', '', $value);
}
}
30 changes: 30 additions & 0 deletions src/FilterRule/Letters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Particle.
*
* @link http://github.com/particle-php for the canonical source repository
* @copyright Copyright (c) 2005-2015 Particle (http://particle-php.com)
* @license https://github.com/particle-php/Filter/blob/master/LICENSE New BSD License
*/
namespace Particle\Filter\FilterRule;

use Particle\Filter\FilterRule;

/**
* Class Letters
*
* @package Particle\Filter\FilterRule
*/
class Letters extends FilterRule
{
/**
* Only return letters
*
* @param mixed $value
* @return string
*/
public function filter($value)
{
return preg_replace('/[^\pL]/', '', $value);
}
}
49 changes: 49 additions & 0 deletions tests/FilterRule/AlnumTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace Particle\Tests\Filter\FilterRule;

use Particle\Filter\Filter;

class AlnumTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Filter
*/
protected $filter;

/**
* Prepare the filter
*/
public function setUp()
{
$this->filter = new Filter();
}

/**
* @dataProvider getAlnumResults
* @param string $value
* @param string $filteredValue
*/
public function testAlnumFilterRule($value, $filteredValue)
{
$this->filter->value('test')->alnum();

$result = $this->filter->filter([
'test' => $value
]);

$this->assertEquals($result['test'], $filteredValue);
}

/**
* @return array
*/
public function getAlnumResults()
{
return [
['', ''],
['0123456789', '0123456789'],
['onlylettersinhere', 'onlylettersinhere'],
['0l!e#t$t.e-r+s9', '0letters9'],
];
}
}
48 changes: 48 additions & 0 deletions tests/FilterRule/LettersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
namespace Particle\Tests\Filter\FilterRule;

use Particle\Filter\Filter;

class LettersTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Filter
*/
protected $filter;

/**
* Prepare the filter
*/
public function setUp()
{
$this->filter = new Filter();
}

/**
* @dataProvider getLettersResults
* @param string $value
* @param string $filteredValue
*/
public function testLettersFilterRule($value, $filteredValue)
{
$this->filter->value('test')->letters();

$result = $this->filter->filter([
'test' => $value
]);

$this->assertEquals($result['test'], $filteredValue);
}

/**
* @return array
*/
public function getLettersResults()
{
return [
['', ''],
['onlylettersinhere', 'onlylettersinhere'],
['0l!e#t$t.e-r+s9', 'letters'],
];
}
}