-
-
Notifications
You must be signed in to change notification settings - Fork 221
/
DomainMatchingStrategy.php
69 lines (61 loc) · 2.2 KB
/
DomainMatchingStrategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace Neos\Neos\Domain\Service;
/*
* This file is part of the Neos.Neos package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Annotations as Flow;
/**
* Strategy for matching domains
*
* @Flow\Scope("singleton")
*/
class DomainMatchingStrategy
{
const EXACTMATCH = 300;
const NOMATCH = -300;
/**
* Returns those of the given domains which match the specified hostname.
*
* The domains are sorted by their match exactness.
* If none really matches an empty array is returned.
*
* @param string $hostnameToMatch The hostname to match against (eg. "localhost" or "www.neos.io")
* @param array<\Neos\Neos\Domain\Model\Domain> $domains The domains to check
* @return array The matching domains
*/
public function getSortedMatches($hostnameToMatch, array $domains)
{
$matchingDomains = [];
$matchQualities = [];
$hostnameToMatchPartsReverse = array_reverse(explode('.', $hostnameToMatch));
foreach ($domains as $domain) {
$domainHostname = $domain->getHostname();
if ($hostnameToMatch === $domainHostname) {
$matchQuality = self::EXACTMATCH;
} else {
$matchQuality = 0;
$domainHostnamePartsReverse = array_reverse(explode('.', $domainHostname));
foreach ($domainHostnamePartsReverse as $index => $domainHostnamePart) {
if (isset($hostnameToMatchPartsReverse[$index]) && $domainHostnamePart === $hostnameToMatchPartsReverse[$index]) {
$matchQuality++;
} else {
$matchQuality = self::NOMATCH;
break;
}
}
}
if ($matchQuality > 0) {
$matchingDomains[] = $domain;
$matchQualities[] = $matchQuality;
}
}
array_multisort($matchQualities, SORT_DESC, $matchingDomains);
return $matchingDomains;
}
}