Skip to content
This repository was archived by the owner on Mar 12, 2020. It is now read-only.

Commit 2aed4c3

Browse files
committed
Added strict mode to the filter
1 parent 862ed45 commit 2aed4c3

3 files changed

Lines changed: 54 additions & 2 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ new OrFilter([
102102
])
103103
```
104104

105+
The filter comes with a third `strict` parameter where instead of declaring out of scope on missing an URL part it return `false`.
106+
105107
## Tests
106108

107109
To run the unit test suite:

src/Filter/UrlSectionFilter.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,23 @@ class UrlSectionFilter implements FilterInterface
1717
*/
1818
protected $section;
1919

20+
/**
21+
* @var null|false
22+
*/
23+
protected $strictResponse = null;
24+
2025
/**
2126
*
2227
* @param string $section
2328
* @param string $value
2429
*/
25-
public function __construct($section, $value)
30+
public function __construct($section, $value, $strict = false)
2631
{
2732
$this->section = $section;
2833
$this->value = $value;
34+
if ($strict === true) {
35+
$this->strictResponse = false;
36+
}
2937
}
3038

3139
/**
@@ -40,7 +48,7 @@ public function filter(EventInterface $event)
4048

4149
$section = $event->getUrlSection($this->section);
4250
if ($section === false) {
43-
return null;
51+
return $this->strictResponse;
4452
}
4553

4654
$pattern = '/^' . str_replace('*', '.*', $this->value) . '$/';

tests/Filter/UrlSectionFilterTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,46 @@ public function testFilter($section, $value, $url, $output)
4848
$filter = new UrlSectionFilter($section, $value);
4949
$this->assertSame($output, $filter->filter($urlEvent));
5050
}
51+
52+
public function provideStrictFilter()
53+
{
54+
yield [
55+
'host',
56+
'phergie.org',
57+
'http://phergie.org/',
58+
true,
59+
];
60+
61+
yield [
62+
'host',
63+
'phergie.org',
64+
'http://www.phergie.org/',
65+
false,
66+
];
67+
68+
yield [
69+
'host',
70+
'*.org',
71+
'http://phergie.org/',
72+
true,
73+
];
74+
75+
yield [
76+
'port',
77+
80,
78+
'http://phergie.org/',
79+
false,
80+
];
81+
}
82+
83+
/**
84+
* @dataProvider provideStrictFilter
85+
*/
86+
public function testStrictFilter($section, $value, $url, $output)
87+
{
88+
$event = Phake::mock(EventInterface::class);
89+
$urlEvent = new UrlEvent($url, $event);
90+
$filter = new UrlSectionFilter($section, $value, true);
91+
$this->assertSame($output, $filter->filter($urlEvent));
92+
}
5193
}

0 commit comments

Comments
 (0)