Skip to content

Commit

Permalink
feat(matching-context): expose list of domains in API
Browse files Browse the repository at this point in the history
Exposes the list of domains on which a matching context is applicable. It also exposes the *path* part of the regexp uses in mathching context...

Some current and previous vocabulary seems dodgy... maybe a `DomainName` should be renamed to `Domain` so we can use `domainName` only for the actual string version of the domain name...

Also `urlPathRegex` is not truly a *path* __regex__ since the user can choose to match all domains... is that still supposed to be possible?

> fix #446
  • Loading branch information
lutangar committed Sep 14, 2021
1 parent 87aaaaf commit 03b4f84
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/Entity/MatchingContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,25 +257,39 @@ public function getUrlRegex(): string
return $this->urlRegex;
}

public function getFullUrlRegex(Escaper $escaper = null): string
/**
* @Groups({
* "read"
* })
*
* @return string[]
*/
public function getDomains(Escaper $escaper = null): array
{
$domains = $this->getAllRelatedDomains();
if (0 === \count($domains)) {
return $this->urlRegex;
return [];
}

return '('.implode(
'|',
array_reduce($domains, static function ($accumulator, DomainName $dn) use ($escaper) {
return array_merge(
return array_reduce($domains, static function ($accumulator, DomainName $dn) use ($escaper) {
return array_merge(
$accumulator,
[escape($dn->getFullName(), $escaper)],
array_map(static function (string $alias) use ($escaper) {
return escape($alias, $escaper);
}, $dn->getAliases())
);
}, [])
).')'.$this->urlRegex;
}, []);
}

public function getFullUrlRegex(Escaper $escaper = null): string
{
$domains = $this->getDomains();
if (0 === \count($domains)) {
return $this->urlRegex;
}

return '('.implode('|', $domains).')'.$this->urlRegex;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Serializer/V3/MatchingContextNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,16 @@ public function normalize($matchingContext, $format = null, array $context = [])

return array_filter([
'id' => $matchingContext->getId(),
'domains' => $matchingContext->getDomains(),
'noticeId' => $matchingContext->getNotice()->getId(),
'noticeUrl' => $this->router->generate(
'app_api_v3_getnoticeaction__invoke',
['id' => $matchingContext->getNotice()->getId()],
RouterInterface::ABSOLUTE_URL),
// @todo should be renamed `fullUrlRegex` in next major release
'urlRegex' => $matchingContext->getFullUrlRegex($this->escaper),
// @todo should probably be named `urlRegex` in next major release
'urlPathRegex' => $matchingContext->getUrlRegex(),
'excludeUrlRegex' => $matchingContext->getCompleteExcludeUrlRegex(),
'querySelector' => $matchingContext->getQuerySelector(),
'xpath' => $matchingContext->getXpath(),
Expand Down
4 changes: 4 additions & 0 deletions tests/Entity/MatchingContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public function testItGetFullUrlRegex(): void

/** @var MatchingContext $mc */
$mc = $this->referenceRepository->getReference('mc_with_domain_name');

$domains = $mc->getDomains($escaper);
self::assertEquals(['duckduckgo.com', 'www.bing.com', 'www.google.fr', 'www.qwant.com', 'www.yahoo.com', 'first.domainname.fr', 'alias.first.domainname.fr', 'second.domainname.fr'], $domains);

$regex = $mc->getFullUrlRegex($escaper);
self::assertEquals('(duckduckgo.com|www.bing.com|www.google.fr|www.qwant.com|www.yahoo.com|first.domainname.fr|alias.first.domainname.fr|second.domainname.fr)'.$mc->getUrlRegex(), $regex);

Expand Down

0 comments on commit 03b4f84

Please sign in to comment.