-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25d2dc1
commit 52902ec
Showing
14 changed files
with
463 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpweb\Release; | ||
|
||
final readonly class Major | ||
{ | ||
private function __construct(private string $value) | ||
{ | ||
} | ||
|
||
/** | ||
* @throws \ValueError | ||
*/ | ||
public static function fromString(string $value): self | ||
{ | ||
if (1 !== preg_match('/^(0|[1-9]\d*)$/', $value)) { | ||
throw new \ValueError(\sprintf( | ||
'Value "%s" does not appear to be a valid value for a major version.', | ||
$value, | ||
)); | ||
} | ||
|
||
return new self($value); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return $this->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpweb\Release; | ||
|
||
final readonly class Minor | ||
{ | ||
private function __construct(private string $value) | ||
{ | ||
} | ||
|
||
/** | ||
* @throws \ValueError | ||
*/ | ||
public static function fromString(string $value): self | ||
{ | ||
if (1 !== preg_match('/^(0|[1-9]\d*)$/', $value)) { | ||
throw new \ValueError(\sprintf( | ||
'Value "%s" does not appear to be a valid value for a minor version.', | ||
$value, | ||
)); | ||
} | ||
|
||
return new self($value); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return $this->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpweb\Release; | ||
|
||
final readonly class Patch | ||
{ | ||
private function __construct(private string $value) | ||
{ | ||
} | ||
|
||
/** | ||
* @throws \ValueError | ||
*/ | ||
public static function fromString(string $value): self | ||
{ | ||
if (1 !== preg_match('/^(0|[1-9]\d*)$/', $value)) { | ||
throw new \ValueError(\sprintf( | ||
'Value "%s" does not appear to be a valid value for a patch version.', | ||
$value, | ||
)); | ||
} | ||
|
||
return new self($value); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return $this->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpweb\Release; | ||
|
||
final readonly class Version | ||
{ | ||
private function __construct( | ||
private Major $major, | ||
private Minor $minor, | ||
private Patch $patch, | ||
) { | ||
} | ||
|
||
public static function create( | ||
Major $major, | ||
Minor $minor, | ||
Patch $patch, | ||
): self { | ||
return new self( | ||
$major, | ||
$minor, | ||
$patch, | ||
); | ||
} | ||
|
||
/** | ||
* @throws \ValueError | ||
*/ | ||
public static function fromString(string $value): self | ||
{ | ||
if (1 !== preg_match('/^(?P<major>(0|[1-9]\d*))\.(?P<minor>(0|[1-9]\d*))\.(?P<patch>(0|[1-9]\d*))$/', $value, $matches)) { | ||
throw new \ValueError(\sprintf( | ||
'Value "%s" does not appear to be a valid value for a version.', | ||
$value, | ||
)); | ||
} | ||
|
||
return new self( | ||
Major::fromString($matches['major']), | ||
Minor::fromString($matches['minor']), | ||
Patch::fromString($matches['patch']), | ||
); | ||
} | ||
|
||
public function major(): Major | ||
{ | ||
return $this->major; | ||
} | ||
|
||
public function minor(): Minor | ||
{ | ||
return $this->minor; | ||
} | ||
|
||
public function patch(): Patch | ||
{ | ||
return $this->patch; | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return sprintf( | ||
'%s.%s.%s', | ||
$this->major->toString(), | ||
$this->minor->toString(), | ||
$this->patch->toString(), | ||
); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
tests/Release/Major/from-string-rejects-invalid-value.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--TEST-- | ||
Major::fromString() rejects invalid values | ||
--FILE-- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use phpweb\Release\Major; | ||
|
||
require_once __DIR__ . '/../../../src/autoload.php'; | ||
|
||
$values = [ | ||
'string-blank' => ' ', | ||
'string-empty' => '', | ||
'string-leading-zero' => '01', | ||
'string-word' => 'foo', | ||
]; | ||
|
||
$invalidValues = array_filter($values, static function (string $value): bool { | ||
try { | ||
Major::fromString($value); | ||
} catch (\ValueError) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}); | ||
|
||
var_dump($invalidValues); | ||
?> | ||
--EXPECT-- | ||
array(4) { | ||
["string-blank"]=> | ||
string(1) " " | ||
["string-empty"]=> | ||
string(0) "" | ||
["string-leading-zero"]=> | ||
string(2) "01" | ||
["string-word"]=> | ||
string(3) "foo" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--TEST-- | ||
Major::fromString() returns Major | ||
--FILE-- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use phpweb\Release\Major; | ||
|
||
require_once __DIR__ . '/../../../src/autoload.php'; | ||
|
||
$major = Major::fromString('8'); | ||
|
||
var_dump($major->toString()); | ||
?> | ||
--EXPECT-- | ||
string(1) "8" |
41 changes: 41 additions & 0 deletions
41
tests/Release/Minor/from-string-rejects-invalid-value.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--TEST-- | ||
Minor::fromString() rejects invalid values | ||
--FILE-- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use phpweb\Release\Minor; | ||
|
||
require_once __DIR__ . '/../../../src/autoload.php'; | ||
|
||
$values = [ | ||
'string-blank' => ' ', | ||
'string-empty' => '', | ||
'string-leading-zero' => '01', | ||
'string-word' => 'foo', | ||
]; | ||
|
||
$invalidValues = array_filter($values, static function (string $value): bool { | ||
try { | ||
Minor::fromString($value); | ||
} catch (\ValueError) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}); | ||
|
||
var_dump($invalidValues); | ||
?> | ||
--EXPECT-- | ||
array(4) { | ||
["string-blank"]=> | ||
string(1) " " | ||
["string-empty"]=> | ||
string(0) "" | ||
["string-leading-zero"]=> | ||
string(2) "01" | ||
["string-word"]=> | ||
string(3) "foo" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--TEST-- | ||
Minor::fromString() returns Minor | ||
--FILE-- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use phpweb\Release\Minor; | ||
|
||
require_once __DIR__ . '/../../../src/autoload.php'; | ||
|
||
$minor = Minor::fromString('3'); | ||
|
||
var_dump($minor->toString()); | ||
?> | ||
--EXPECT-- | ||
string(1) "3" |
41 changes: 41 additions & 0 deletions
41
tests/Release/Patch/from-string-rejects-invalid-value.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--TEST-- | ||
Patch::fromString() rejects invalid values | ||
--FILE-- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use phpweb\Release\Patch; | ||
|
||
require_once __DIR__ . '/../../../src/autoload.php'; | ||
|
||
$values = [ | ||
'string-blank' => ' ', | ||
'string-empty' => '', | ||
'string-leading-zero' => '01', | ||
'string-word' => 'foo', | ||
]; | ||
|
||
$invalidValues = array_filter($values, static function (string $value): bool { | ||
try { | ||
Patch::fromString($value); | ||
} catch (\ValueError) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}); | ||
|
||
var_dump($invalidValues); | ||
?> | ||
--EXPECT-- | ||
array(4) { | ||
["string-blank"]=> | ||
string(1) " " | ||
["string-empty"]=> | ||
string(0) "" | ||
["string-leading-zero"]=> | ||
string(2) "01" | ||
["string-word"]=> | ||
string(3) "foo" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--TEST-- | ||
Patch::fromString() returns Patch | ||
--FILE-- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use phpweb\Release\Patch; | ||
|
||
require_once __DIR__ . '/../../../src/autoload.php'; | ||
|
||
$patch = Patch::fromString('8'); | ||
|
||
var_dump($patch->toString()); | ||
?> | ||
--EXPECT-- | ||
string(1) "8" |
Oops, something went wrong.