Skip to content

Commit

Permalink
Changed "getAll()" to "getFilters()" to have the same signature as th…
Browse files Browse the repository at this point in the history
…e \Sirius\Validation library
  • Loading branch information
Adrian Miu committed Apr 15, 2014
1 parent 62481a2 commit 6bd0c77
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ build
/.settings
/.buildpath
/.project
.idea/
10 changes: 5 additions & 5 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ $filtrator->add('*', 'trim', null, true);
$filteredPostData = $filtrator->filter($_POST);
```

### The `$callbackOrFilterName` parameter can be:
### The `$callbackOrFilterName` parameter can be:

####1. a class name that extends `\Sirius\Filtration\Filter\AbstractFilter`
```php
Expand All @@ -38,7 +38,7 @@ $filtrator->registerFilterClass('sluggify', '\MyApp\Filtration\Filter\Sluggify')
$filtrator->add('slug', 'sluggify');
```

####4. anything that is callable: a PHP function, a static method class, an invokable object etc.
####4. anything that is callable: a PHP function, a static method class, an invokable object etc.
The only things to keep in mind are:

- The first argument must be the value you want filtered. `trim`, `strtolower`, `ucwords` are good candidates, but not `str_replace`.
Expand Down Expand Up @@ -92,7 +92,7 @@ $filtrator->add('selector', 'stringrim | truncate(limit=10)(true)(10)');
```php
$filtrator->add(array(
// use parantheses to pass parameters, recursiveness and priority
'key_a' => 'stringtrim | nullify | truncate(limit=10)(true)(10)',
'key_a' => 'stringtrim | nullify | truncate(limit=10)(true)(10)',
'key_b' => 'stringtrim | nullify'
));

Expand Down Expand Up @@ -138,7 +138,7 @@ function convertDateArraysToString ($data, $source, $destination) {
return $data;
}

$filtrator->add(Filtrator::SELECTOR_ROOT, 'convertDateArraysToString', array('date_as_array', 'date'));
$filtrator->add(Filtrator::SELECTOR_ROOT, 'convertDateArraysToString', array('date_as_array', 'date'));
$data = $filtrator->filter($data);
$data['date'] == '2012-1-12'; // true
```
Expand All @@ -156,7 +156,7 @@ The code above will apply all the filters associated with the selector that matc

You may need to retrieve the list of filters for various reasons (eg: you need to converted into a list of javascript filters for the client side)
```php
$filters = $filtrator->getAll();
$filters = $filtrator->getFilters();
// returns an array
array(
'selector' => array(
Expand Down
2 changes: 1 addition & 1 deletion src/Filtrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ function remove($selector, $callbackOrName = true)
*
* @return array
*/
function getAll()
function getFilters()
{
return $this->filters;
}
Expand Down
34 changes: 17 additions & 17 deletions tests/src/FiltratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ function testDuplicateCallbacksNotAllowed()
{
$this->filtrator->add('*', 'StringTrim', null, true);
$this->filtrator->add('*', 'StringTrim', null, true);
$this->assertEquals(1, count($this->filtrator->getAll()['*']));

$this->assertEquals(1, count($this->filtrator->getFilters()['*']));
}

function testExceptionThrownForUncallableFilters()
Expand Down Expand Up @@ -130,9 +130,9 @@ function testCustomFilterClass() {
'real' => 'fake'
), $this->filtrator->filter(array(
'real' => 'real'
)));
)));
}

function testAddingObjectsAsFilters() {
$this->filtrator->add('trim', new \Sirius\Filtration\Filter\StringTrim());
$this->assertEquals(array(
Expand All @@ -141,25 +141,25 @@ function testAddingObjectsAsFilters() {
'trim' => ' abc ',
)));
}

function testExceptionThrownIfFilterCannotBeConstructed() {
$this->setExpectedException('\InvalidArgumentException');
$this->filtrator->add('trim', new \stdClass());
}

function testFilteringNonArrays() {
$obj = new \stdClass();
$obj->key = 'value';
$this->assertEquals($obj, $this->filtrator->filter($obj));
}

function testRemovingAllFiltersForASelector()
{
$this->filtrator->add('whitespace', __NAMESPACE__ . '\postFiltrationFunction')->add('whitespace', __NAMESPACE__ . '\preFiltrationFunction');
$this->filtrator->remove('whitespace', true);
$this->assertEquals(array(), $this->filtrator->getAll());
$this->assertEquals(array(), $this->filtrator->getFilters());
}

function testRemovingObjectsAsFilters() {
$this->filtrator->add('trim', new \Sirius\Filtration\Filter\StringTrim());
$this->filtrator->remove('trim', new \Sirius\Filtration\Filter\StringTrim());
Expand All @@ -169,7 +169,7 @@ function testRemovingObjectsAsFilters() {
'trim' => ' abc ',
)));
}

function testFilterOptions() {
$this->filtrator->add('text', 'stringtrim', '{"side": "right"}');
$this->filtrator->add('another_text', 'stringtrim', "side=left");
Expand All @@ -181,12 +181,12 @@ function testFilterOptions() {
'another_text'=> ' abc '
)));
}

function testExceptionThrownOnInvalidSelector() {
$this->setExpectedException('\InvalidArgumentException');
$this->filtrator->add(new \stdClass());
}

function testAddingMultipleRulesAtOnce() {
$this->filtrator->add(array(
'text' => array(array('stringtrim', '{"side": "right"}')),
Expand All @@ -200,7 +200,7 @@ function testAddingMultipleRulesAtOnce() {
'another_text'=> ' abc '
)));
}

function testAddingMultipleRulesAsArrayPerSelectorAtOnce() {
$this->filtrator->add('text', array('stringtrim', 'truncate(limit=10)(true)(10)'));
$this->assertEquals(array(
Expand All @@ -209,8 +209,8 @@ function testAddingMultipleRulesAsArrayPerSelectorAtOnce() {
'text' => ' A text that is more than 10 characters long'
)));
}


function testAddingMultipleRulesAsStringPerSelectorAtOnce() {
$this->filtrator->add('text', 'stringtrim | truncate(limit=10)(true)(10)');
$this->assertEquals(array(
Expand All @@ -219,5 +219,5 @@ function testAddingMultipleRulesAsStringPerSelectorAtOnce() {
'text' => ' A text that is more than 10 characters long'
)));
}
}

}

0 comments on commit 6bd0c77

Please sign in to comment.