From 57b939103e2b6098e793f2e8a79904b7daf08d9f Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 31 Jul 2024 21:01:22 +0000 Subject: [PATCH] Removed Version class, please use Composer's SemVer instead. --- VERSION | 2 +- src/Data/MariaDB/MariaDBBackend.php | 11 +- src/Data/MariaDB/MariaDBConnection.php | 7 +- src/Data/SQLite/SQLiteBackend.php | 9 +- src/Http/HttpMessage.php | 9 +- src/Http/HttpMessageBuilder.php | 11 +- src/Http/HttpRequest.php | 7 +- src/Http/HttpResponse.php | 5 +- src/Http/Routing/HttpRouter.php | 8 +- src/Version.php | 245 ------------------------- tests/VersionTest.php | 78 -------- 11 files changed, 30 insertions(+), 362 deletions(-) delete mode 100644 src/Version.php delete mode 100644 tests/VersionTest.php diff --git a/VERSION b/VERSION index c13e3f8..f8e7505 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2407.312054 +0.2407.312100 diff --git a/src/Data/MariaDB/MariaDBBackend.php b/src/Data/MariaDB/MariaDBBackend.php index 8a5a358..805d33b 100644 --- a/src/Data/MariaDB/MariaDBBackend.php +++ b/src/Data/MariaDB/MariaDBBackend.php @@ -1,12 +1,11 @@ connection->server_version); } diff --git a/src/Data/SQLite/SQLiteBackend.php b/src/Data/SQLite/SQLiteBackend.php index 63cb273..8a4c7cc 100644 --- a/src/Data/SQLite/SQLiteBackend.php +++ b/src/Data/SQLite/SQLiteBackend.php @@ -1,13 +1,12 @@ version = $version; $this->headers = $headers; $this->content = $content; } - public function getHttpVersion(): Version { + public function getHttpVersion(): string { return $this->version; } diff --git a/src/Http/HttpMessageBuilder.php b/src/Http/HttpMessageBuilder.php index 26a32f1..673f7db 100644 --- a/src/Http/HttpMessageBuilder.php +++ b/src/Http/HttpMessageBuilder.php @@ -1,18 +1,17 @@ headers = new HttpHeadersBuilder; } - protected function getHttpVersion(): Version { - return $this->version ?? new Version(1, 1); + protected function getHttpVersion(): string { + return $this->version ?? '1.1'; } - public function setHttpVersion(Version $version): void { + public function setHttpVersion(string $version): void { $this->version = $version; } diff --git a/src/Http/HttpRequest.php b/src/Http/HttpRequest.php index 7ffb165..6bd7db4 100644 --- a/src/Http/HttpRequest.php +++ b/src/Http/HttpRequest.php @@ -1,13 +1,12 @@ setHttpVersion(new Version(...array_map('intval', explode('.', substr($_SERVER['SERVER_PROTOCOL'], 5))))); + $build->setHttpVersion($_SERVER['SERVER_PROTOCOL']); $build->setMethod($_SERVER['REQUEST_METHOD']); // this currently doesn't "properly" support the scenario where a full url is specified in the http request diff --git a/src/Http/HttpResponse.php b/src/Http/HttpResponse.php index 360f289..690460a 100644 --- a/src/Http/HttpResponse.php +++ b/src/Http/HttpResponse.php @@ -1,11 +1,10 @@ getHttpVersion(); header(sprintf( - 'HTTP/%d.%d %03d %s', - $version->getMajor(), - $version->getMinor(), + 'HTTP/%s %03d %s', + $response->getHttpVersion(), $response->getStatusCode(), $response->getStatusText() )); diff --git a/src/Version.php b/src/Version.php deleted file mode 100644 index 7a2657b..0000000 --- a/src/Version.php +++ /dev/null @@ -1,245 +0,0 @@ -major = $major; - $this->minor = $minor; - $this->patch = $patch; - $this->prerelease = empty($prerelease) ? [] : explode('.', $prerelease); - $this->build = empty($build) ? [] : explode('.', $build); - } - - /** - * Gets the value of the major version component. - * - * @return int Major version component. - */ - public function getMajor(): int { - return $this->major; - } - - /** - * Gets the value of the minor version component. - * - * @return int Minor version component. - */ - public function getMinor(): int { - return $this->minor; - } - - /** - * Gets the value of the patch version component. - * - * @return int Patch version component. - */ - public function getPatch(): int { - return $this->patch; - } - - /** - * Gets the split value of the prerelease component. - * - * @return array array containing the prerelease parts. - */ - public function getPrerelease(): array { - return $this->prerelease; - } - - /** - * Gets the split value of the build component. - * - * @return array array contains the build parts. - */ - public function getBuild(): array { - return $this->build; - } - - /** - * Tests if this Version instance represents a prerelease according to the SemVer spec. - * - * @return bool true if the version indicates a prerelease, false if not. - */ - public function isPrerelease(): bool { - return $this->major < 1 || !empty($this->prerelease); - } - - /** - * Creates a new instance of Version with the major component incremented. - * - * This will implicitly drop prerelease and build info and reset the minor and patch component. - * - * @return Version the newly created instance of Version. - */ - public function incrementMajor(): Version { - return new Version($this->major + 1, 0, 0); - } - - /** - * Creates a new instance of Version with the minor component incremented. - * - * This will implicitly drop the prerelease and build info and reset the patch component. - * - * @return Version the newly created instance of Version. - */ - public function incrementMinor(): Version { - return new Version($this->major, $this->minor + 1, 0); - } - - /** - * Creates a new instance of Version with the patch component incremented. - * - * This will implicitly drop the prerelease and build info. - * - * @return Version the newly created instance of Version. - */ - public function incrementPatch(): Version { - return new Version($this->major, $this->minor, $this->patch + 1); - } - - /** - * Checks this instance of Version with another for equality. - * - * The build component is ignored, as per the SemVer spec. - * - * @param mixed $other Another instance of Version. - * @return bool true if the instances are equal, false if not. - */ - public function equals(mixed $other): bool { - return $other instanceof Version - && $other->major === $this->major - && $other->minor === $this->minor - && $other->patch === $this->patch - && XArray::sequenceEquals($this->prerelease, $other->prerelease); - } - - /** - * Compares this instance of Version with another. - * - * The build component is ignored and prerelease component is compared as per the SemVer spec. - * - * @param mixed $other Another instance of Version. - */ - public function compare(mixed $other): int { - if(!($other instanceof Version)) - return PHP_INT_MIN; - - $diff = $this->major <=> $other->major; - if($diff) return $diff; - - $diff = $this->minor <=> $other->minor; - if($diff) return $diff; - - $diff = $this->patch <=> $other->patch; - if($diff) return $diff; - - $tpi = XArray::extractIterator($this->prerelease); - $opi = XArray::extractIterator($other->prerelease); - - $tpi->rewind(); - $opi->rewind(); - $valid = $tpi->valid(); - $diff = $opi->valid() <=> $valid; - if($diff) return $diff; - - while($valid) { - $diff = $opi->current()->compare($tpi->current()); - if($diff) return $diff; - - $tpi->next(); - $opi->next(); - $valid = $tpi->valid(); - $diff = $opi->valid() <=> $valid; - if($diff) return $diff; - } - - return 0; - } - - public function __toString(): string { - if($this->versionString === null) { - $string = $this->major . '.' . $this->minor . '.' . $this->patch; - - if(!empty($this->prerelease)) - $string .= '-' . implode('.', $this->prerelease); - - if(!empty($this->build)) - $string .= '+' . implode('.', $this->build); - - $this->versionString = $string; - } - - return $this->versionString; - } - - /** - * Returns an empty Version instance. - * - * @return Version Instance of Version with all values set to 0. - */ - public static function empty(): Version { - if(self::$empty === null) - self::$empty = new Version(0); - return self::$empty; - } - - /** - * Parses a version string. - * - * Parses a version string with a regex adapted from SemVer's documentation. Modified to allow an optional v prefix. - * - * @see https://semver.org/spec/v2.0.0.html#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string - * @param string $versionString String containing a version number. - * @throws InvalidArgumentException Provided string does not represent a valid version. - * @return Version An instance of Version representing the provided version string. - */ - public static function parse(string $versionString): Version { - // Regex adapted from https://semver.org/spec/v2.0.0.html#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string - if(!preg_match('#^v?(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)(?:-(?P(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$#', $versionString, $matches)) - throw new InvalidArgumentException('$versionString is not a valid version string.'); - - return new Version( - (int)$matches['major'], - (int)$matches['minor'], - (int)$matches['patch'], - $matches['prerelease'] ?? '', - $matches['build'] ?? '' - ); - } -} diff --git a/tests/VersionTest.php b/tests/VersionTest.php deleted file mode 100644 index 68f37e7..0000000 --- a/tests/VersionTest.php +++ /dev/null @@ -1,78 +0,0 @@ -assertEquals($test, (string)$test); - } - - $build = Version::parse('v1.0.0+21AF26D3--117B344092BD'); - $pre = Version::parse('1.0.0-alpha'); - $both = Version::parse('1.0.0-beta+exp.sha.5114f85'); - - $this->assertFalse($build->isPrerelease()); - $this->assertTrue($pre->isPrerelease()); - $this->assertTrue($both->isPrerelease()); - - $this->assertTrue(XArray::sequenceEquals($build->getBuild(), ['21AF26D3--117B344092BD'])); - $this->assertTrue(XArray::sequenceEquals($pre->getPrerelease(), ['alpha'])); - $this->assertTrue(XArray::sequenceEquals($both->getPrerelease(), ['beta'])); - $this->assertTrue(XArray::sequenceEquals($both->getBuild(), ['exp', 'sha', '5114f85'])); - } - - public function testCompare(): void { - $v1_0_0_alpha = Version::parse('v1.0.0-alpha'); - $v1_0_0 = Version::parse('1.0.0'); - $v1_0_0_build = Version::parse('1.0.0+build'); - $v2_0_0 = Version::parse('v2.0.0'); - $v2_1_0 = new Version(2, 1); - $v2_1_1 = new Version(2, 1, 1); - - $this->assertEquals(0, $v1_0_0->compare($v1_0_0_build)); - $this->assertEquals(-1, $v2_0_0->compare($v2_1_0)); - $this->assertEquals(-1, $v2_0_0->compare($v2_1_0)); - $this->assertEquals(-1, $v1_0_0->compare($v2_0_0)); - $this->assertEquals(-1, $v1_0_0_alpha->compare($v1_0_0)); - } - - public function testEquals(): void { - $v1_0_0_alpha = Version::parse('v1.0.0-alpha'); - $v1_0_0 = Version::parse('1.0.0'); - $v1_0_0_build = Version::parse('1.0.0+build'); - $v2_0_0 = Version::parse('v2.0.0'); - $v2_1_0 = new Version(2, 1); - $v2_1_1 = new Version(2, 1, 1); - - $this->assertTrue($v1_0_0->equals($v1_0_0_build)); - $this->assertFalse($v2_0_0->equals($v2_1_0)); - $this->assertFalse($v2_0_0->equals($v2_1_0)); - $this->assertFalse($v1_0_0->equals($v2_0_0)); - $this->assertFalse($v1_0_0_alpha->equals($v1_0_0)); - } -}