Skip to content

Commit

Permalink
Free version string
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Mátyus committed Apr 18, 2024
1 parent da3955a commit 7c8f389
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
7 changes: 1 addition & 6 deletions src/EndpointIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class EndpointIdentifier implements EndpointInterface
public function __construct(string $method, string $version, string $package, ?string $apiAction = null)
{
$this->method = strtoupper($method);
if ($this->checkVersionFormat($version) === false) {
if (strpos($version, '/') !== false) {
throw new InvalidArgumentException('Version must have semantic numbering. For example "1", "1.1", "0.13.2" etc.');
}
$this->version = $version;
Expand Down Expand Up @@ -54,9 +54,4 @@ public function getUrl(): string
{
return "v{$this->version}/{$this->package}/{$this->apiAction}";
}

private function checkVersionFormat(string $version): bool
{
return (preg_match('/^(0|[1-9][0-9]*)(\.(0|[1-9][0-9]*)){0,2}$/', $version) === 1);
}
}
19 changes: 13 additions & 6 deletions tests/EndpoinIdentifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,25 @@ public function testSimpleUrl()

public function testSupportedVersions()
{
new EndpointIdentifier('GET', '0', 'core', 'show');
new EndpointIdentifier('GET', '1', 'core', 'show');
new EndpointIdentifier('GET', '1.0', 'core', 'show');
new EndpointIdentifier('GET', '1.1', 'core', 'show');
new EndpointIdentifier('GET', '1.33', 'core', 'show');
$endpoint = new EndpointIdentifier('GET', '0', 'core', 'show');
$this->assertEquals('v0/core/show', $endpoint->getUrl());
$endpoint = new EndpointIdentifier('GET', '1', 'core', 'show');
$this->assertEquals('v1/core/show', $endpoint->getUrl());
$endpoint = new EndpointIdentifier('GET', '1.0', 'core', 'show');
$this->assertEquals('v1.0/core/show', $endpoint->getUrl());
$endpoint = new EndpointIdentifier('GET', '1.1', 'core', 'show');
$this->assertEquals('v1.1/core/show', $endpoint->getUrl());
$endpoint = new EndpointIdentifier('GET', '1.33', 'core', 'show');
$this->assertEquals('v1.33/core/show', $endpoint->getUrl());
$endpoint = new EndpointIdentifier('GET', '1.33-dev', 'core', 'show');
$this->assertEquals('v1.33-dev/core/show', $endpoint->getUrl());
$endpoint = new EndpointIdentifier('GET', '0.33.43', 'core', 'show');
$this->assertEquals('v0.33.43/core/show', $endpoint->getUrl());
}

public function testFailVersion()
{
$this->expectException(InvalidArgumentException::class);
new EndpointIdentifier('GET', 'test', 'core', 'show');
new EndpointIdentifier('GET', '1.0/dev', 'core', 'show');
}
}

0 comments on commit 7c8f389

Please sign in to comment.