48 lines
1.6 KiB
PHP
48 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\Marketplace\Services;
|
|
|
|
final class VersionConstraint
|
|
{
|
|
public function matches(string $version, string $constraint): bool
|
|
{
|
|
$constraint = trim($constraint);
|
|
if ($constraint === '' || $constraint === '*') {
|
|
return true;
|
|
}
|
|
foreach (preg_split('/\s*\|\|\s*/', $constraint) ?: [] as $alternative) {
|
|
$ok = true;
|
|
foreach (preg_split('/\s*,?\s+/', trim($alternative)) ?: [] as $part) {
|
|
if ($part === '') {
|
|
continue;
|
|
}
|
|
if (str_starts_with($part, '^')) {
|
|
$base = substr($part, 1);
|
|
$major = (int) explode('.', $base)[0];
|
|
$ok = $ok && version_compare($version, $base, '>=') && version_compare($version, ($major + 1).'.0.0', '<');
|
|
|
|
continue;
|
|
}
|
|
if (str_starts_with($part, '~')) {
|
|
$base = substr($part, 1);
|
|
$bits = array_pad(explode('.', $base), 3, '0');
|
|
$upper = $bits[0].'.'.((int) $bits[1] + 1).'.0';
|
|
$ok = $ok && version_compare($version, $base, '>=') && version_compare($version, $upper, '<');
|
|
|
|
continue;
|
|
}
|
|
if (preg_match('/^(>=|<=|>|<|=)?\s*([0-9][0-9A-Za-z.\-+]*)$/', $part, $m)) {
|
|
$ok = $ok && version_compare($version, $m[2], $m[1] ?: '=');
|
|
} else {
|
|
$ok = $ok && $version === $part;
|
|
}
|
|
}
|
|
if ($ok) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|