Skip to content

Commit b3b6c63

Browse files
authored
Merge pull request #9 from nurmanhabib/feature/exact-pattern
Added method matchExact() for pattern exact activator
2 parents 7c11b48 + d522693 commit b3b6c63

File tree

6 files changed

+60
-3
lines changed

6 files changed

+60
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/.idea
12
/vendor
23
composer.phar
34
composer.lock

src/Activators/LinkActivator.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ protected function matchPath(Nav $nav)
3535
}
3636

3737
$navPath = parse_url($navPattern, PHP_URL_PATH);
38-
$match = strpos($currentPath, $navPath) !== false || fnmatch($navPath, $currentPath);
38+
39+
if ($nav->patternIsMatchExact()) {
40+
$match = $currentPath === $navPath;
41+
} else {
42+
$match = strpos($currentPath, $navPath) !== false || fnmatch($navPath, $currentPath);
43+
}
3944

4045
if ($match) {
4146
break;
@@ -58,7 +63,11 @@ protected function matchQuery(Nav $nav)
5863

5964
parse_str($navQuery, $navQuery);
6065

61-
$match = empty(array_diff($navQuery, $expectedQuery));
66+
if ($nav->patternIsMatchExact()) {
67+
$match = $navQuery == $expectedQuery;
68+
} else {
69+
$match = empty(array_diff($navQuery, $expectedQuery));
70+
}
6271

6372
if ($match) {
6473
break;

src/Items/Nav.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public function setChild(NavCollection $child);
6262
*/
6363
public function getPattern();
6464

65+
/**
66+
* @return bool
67+
*/
68+
public function patternIsMatchExact();
69+
6570
/**
6671
* @param Nav $nav
6772
* @return Nav

src/Items/NavItem.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ abstract class NavItem implements Nav
1515

1616
protected $patterns = [];
1717

18+
protected $patternMatchExact = false;
19+
1820
abstract public function getText();
1921

2022
abstract public function getUrl();
@@ -97,10 +99,40 @@ public function matches(array $patterns = [])
9799
return $this;
98100
}
99101

102+
public function matchExact($exact = true)
103+
{
104+
$this->patternMatchExact = $exact;
105+
106+
return $this;
107+
}
108+
109+
public function patternIsMatchExact()
110+
{
111+
return $this->patternMatchExact;
112+
}
113+
100114
public function getPattern()
101115
{
102116
if (empty($this->patterns)) {
103-
$this->match(parse_url($this->getUrl(), PHP_URL_PATH));
117+
$path = parse_url($this->getUrl(), PHP_URL_PATH);
118+
$query = parse_url($this->getUrl(), PHP_URL_QUERY);
119+
$fragment = parse_url($this->getUrl(), PHP_URL_FRAGMENT);
120+
121+
$relavtiveUrl = '';
122+
123+
if ($path) {
124+
$relavtiveUrl = $path;
125+
}
126+
127+
if ($query) {
128+
$relavtiveUrl .= '?' . $query;
129+
}
130+
131+
if ($fragment) {
132+
$relavtiveUrl .= '#' . $fragment;
133+
}
134+
135+
$this->match($relavtiveUrl);
104136
}
105137

106138
return $this->patterns;

src/Items/NavSeparator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public function getPattern()
5959
return [];
6060
}
6161

62+
public function patternIsMatchExact()
63+
{
64+
return false;
65+
}
66+
6267
public function getType()
6368
{
6469
return 'separator';

src/Modifiers/NavModifier.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ public function getPattern()
9797
return $this->nav->getPattern();
9898
}
9999

100+
public function patternIsMatchExact()
101+
{
102+
return $this->nav->patternIsMatchExact();
103+
}
104+
100105
public function toArray()
101106
{
102107
return [

0 commit comments

Comments
 (0)