Browse Source

More matching functionality

main
Dustin Wilson 3 years ago
parent
commit
4e3083bfb5
  1. 2
      lib/Scope/Matcher.php
  2. 10
      lib/Scope/Matchers/AndMatcher.php
  3. 8
      lib/Scope/Matchers/CompositeMatcher.php
  4. 10
      lib/Scope/Matchers/GroupMatcher.php
  5. 8
      lib/Scope/Matchers/NegateMatcher.php
  6. 8
      lib/Scope/Matchers/OrMatcher.php
  7. 20
      lib/Scope/Matchers/PathMatcher.php

2
lib/Scope/Matcher.php

@ -7,7 +7,7 @@ declare(strict_types=1);
namespace dW\Highlighter\Scope;
abstract class Matcher {
public function getPrefix(string $scope) {
public function getPrefix(string $scope): string|null|false {
return null;
}
}

10
lib/Scope/Matchers/AndMatcher.php

@ -14,4 +14,14 @@ class AndMatcher extends Matcher {
$this->left = $left;
$this->right = $right;
}
public function matches(array $scopes): bool {
return ($this->left->matches($scopes) && $this->right->matches($scopes));
}
public function getPrefix(array $scopes): string|null|false {
if ($this->left->matches($scopes) && $this->right->matches($scopes)) {
return $this->left->getPrefix($scopes);
}
}
}

8
lib/Scope/Matchers/CompositeMatcher.php

@ -22,4 +22,12 @@ class CompositeMatcher extends Matcher {
break;
}
}
public function matches(array $scopes): bool {
return $this->matcher->matches($scopes);
}
public function getPrefix(array $scopes): string|null|false {
return $this->matcher->getPrefix($scopes);
}
}

10
lib/Scope/Matchers/GroupMatcher.php

@ -14,4 +14,14 @@ class GroupMatcher extends Matcher {
$this->prefix = ($prefix !== null) ? $prefix[0] : null;
$this->selector = $selector;
}
public function matches(array $scopes): bool {
return $this->selector->matches($scope);
}
public function getPrefix(array $scopes): string|null|false {
if ($this->selector->matches($scopes)) {
return $this->prefix;
}
}
}

8
lib/Scope/Matchers/NegateMatcher.php

@ -12,4 +12,12 @@ class NegateMatcher extends Matcher {
public function __construct(Matcher $matcher) {
$this->matcher = $matcher;
}
public function matches(array $scopes): bool {
return !($this->matcher->matches($scopes));
}
public function getPrefix(array $scopes): null {
return null;
}
}

8
lib/Scope/Matchers/OrMatcher.php

@ -14,4 +14,12 @@ class OrMatcher extends Matcher {
$this->left = $left;
$this->right = $right;
}
public function matches(array $scopes): bool {
return ($this->left->matches($scopes) || $this->right->matches($scopes));
}
public function getPrefix(array $scopes): string|null|false {
return $this->left->getPrefix($scopes) || $this->right->getPrefix($scopes);
}
}

20
lib/Scope/Matchers/PathMatcher.php

@ -14,4 +14,24 @@ class PathMatcher extends Matcher {
$this->prefix = ($prefix !== null) ? $prefix[0] : null;
$this->matchers = $matchers;
}
public function matches(array $scopes): bool {
$count = 0;
$matcher = $this->matchers[$count];
foreach ($scopes as $scope) {
if ($matcher->matches($scope)) {
$matcher = $this->matchers[++$count];
}
if ($matcher === null) {
return true;
}
}
return false;
}
public function getPrefix(array $scopes): string|null|false {
if ($this->matches($scopes)) {
return $this->prefix;
}
}
}

Loading…
Cancel
Save