Skip to content

Commit

Permalink
Merge pull request #10 from ergebnis/feature/int
Browse files Browse the repository at this point in the history
Enhancement: Allow creating `Major`, `Minor`, and `Patch` from `int`
  • Loading branch information
localheinz authored Dec 25, 2023
2 parents 2f8d58c + eda1816 commit 0bff890
Show file tree
Hide file tree
Showing 14 changed files with 243 additions and 30 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ $one->equals($two); // true
$one->equals($three); // false
```

### Create a `Major` from an `int`

```php
<?php

declare(strict_types=1);

use Ergebnis\Version;

$major = Version\Major::fromInt(1);

echo $major->toInt(); // 1
```

### Create a `Major` from a `string`

```php
Expand Down Expand Up @@ -94,6 +108,20 @@ $one->equals($three); // false
$one->equals($two); // true
```

### Create a `Minor` from an `int`

```php
<?php

declare(strict_types=1);

use Ergebnis\Version;

$minor = Version\Minor::fromInt(1);

echo $minor->toInt(); // 1
```

### Create a `Minor` from a `string`

```php
Expand Down Expand Up @@ -127,6 +155,20 @@ $one->equals($three); // false
$one->equals($two); // true
```

### Create a `Patch` from an `int`

```php
<?php

declare(strict_types=1);

use Ergebnis\Version;

$patch = Version\Patch::fromInt(1);

echo $patch->toInt(); // 1
```

### Create a `Patch` from a `string`

```php
Expand Down
12 changes: 6 additions & 6 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
</file>
<file src="test/Unit/MajorTest.php">
<PossiblyUnusedMethod>
<code>provideInvalidValue</code>
<code>provideValidValue</code>
<code>provideInvalidStringValue</code>
<code>provideValidStringValue</code>
</PossiblyUnusedMethod>
</file>
<file src="test/Unit/MinorTest.php">
<PossiblyUnusedMethod>
<code>provideInvalidValue</code>
<code>provideValidValue</code>
<code>provideInvalidStringValue</code>
<code>provideValidStringValue</code>
</PossiblyUnusedMethod>
</file>
<file src="test/Unit/PatchTest.php">
<PossiblyUnusedMethod>
<code>provideInvalidValue</code>
<code>provideValidValue</code>
<code>provideInvalidStringValue</code>
<code>provideValidStringValue</code>
</PossiblyUnusedMethod>
</file>
<file src="test/Unit/PreReleaseTest.php">
Expand Down
8 changes: 8 additions & 0 deletions src/Exception/InvalidMajor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@

final class InvalidMajor extends \InvalidArgumentException
{
public static function fromInt(int $value): self
{
return new self(\sprintf(
'Value "%d" does not appear to be valid.',
$value,
));
}

public static function fromString(string $value): self
{
return new self(\sprintf(
Expand Down
8 changes: 8 additions & 0 deletions src/Exception/InvalidMinor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@

final class InvalidMinor extends \InvalidArgumentException
{
public static function fromInt(int $value): self
{
return new self(\sprintf(
'Value "%d" does not appear to be valid.',
$value,
));
}

public static function fromString(string $value): self
{
return new self(\sprintf(
Expand Down
8 changes: 8 additions & 0 deletions src/Exception/InvalidPatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@

final class InvalidPatch extends \InvalidArgumentException
{
public static function fromInt(int $value): self
{
return new self(\sprintf(
'Value "%d" does not appear to be valid.',
$value,
));
}

public static function fromString(string $value): self
{
return new self(\sprintf(
Expand Down
23 changes: 20 additions & 3 deletions src/Major.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,22 @@ final class Major
*/
private const REGEX = '/^(?P<major>0|[1-9]\d*)$/';

private function __construct(private readonly string $value)
private function __construct(private readonly int $value)
{
}

/**
* @throws Exception\InvalidMajor
*/
public static function fromInt(int $value): self
{
if (0 > $value) {
throw Exception\InvalidMajor::fromInt($value);
}

return new self($value);
}

/**
* @throws Exception\InvalidMajor
*/
Expand All @@ -34,14 +46,19 @@ public static function fromString(string $value): self
throw Exception\InvalidMajor::fromString($value);
}

return new self($value);
return new self((int) $value);
}

public function toString(): string
public function toInt(): int
{
return $this->value;
}

public function toString(): string
{
return (string) $this->value;
}

public function equals(self $other): bool
{
return $this->value === $other->value;
Expand Down
23 changes: 20 additions & 3 deletions src/Minor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,22 @@ final class Minor
*/
private const REGEX = '/^(?P<minor>0|[1-9]\d*)$/';

private function __construct(private readonly string $value)
private function __construct(private readonly int $value)
{
}

/**
* @throws Exception\InvalidMinor
*/
public static function fromInt(int $value): self
{
if (0 > $value) {
throw Exception\InvalidMinor::fromInt($value);
}

return new self($value);
}

/**
* @throws Exception\InvalidMinor
*/
Expand All @@ -34,14 +46,19 @@ public static function fromString(string $value): self
throw Exception\InvalidMinor::fromString($value);
}

return new self($value);
return new self((int) $value);
}

public function toString(): string
public function toInt(): int
{
return $this->value;
}

public function toString(): string
{
return (string) $this->value;
}

public function equals(self $other): bool
{
return $this->value === $other->value;
Expand Down
23 changes: 20 additions & 3 deletions src/Patch.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,22 @@ final class Patch
*/
private const REGEX = '/^(?P<patch>0|[1-9]\d*)$/';

private function __construct(private readonly string $value)
private function __construct(private readonly int $value)
{
}

/**
* @throws Exception\InvalidPatch
*/
public static function fromInt(int $value): self
{
if (0 > $value) {
throw Exception\InvalidPatch::fromInt($value);
}

return new self($value);
}

/**
* @throws Exception\InvalidPatch
*/
Expand All @@ -34,14 +46,19 @@ public static function fromString(string $value): self
throw Exception\InvalidPatch::fromString($value);
}

return new self($value);
return new self((int) $value);
}

public function toString(): string
public function toInt(): int
{
return $this->value;
}

public function toString(): string
{
return (string) $this->value;
}

public function equals(self $other): bool
{
return $this->value === $other->value;
Expand Down
14 changes: 14 additions & 0 deletions test/Unit/Exception/InvalidMajorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ final class InvalidMajorTest extends Framework\TestCase
{
use Test\Util\Helper;

public function testFromIntReturnsException(): void
{
$value = self::faker()->numberBetween(0);

$exception = Exception\InvalidMajor::fromInt($value);

$message = \sprintf(
'Value "%d" does not appear to be valid.',
$value,
);

self::assertSame($message, $exception->getMessage());
}

public function testFromStringReturnsException(): void
{
$value = self::faker()->word();
Expand Down
14 changes: 14 additions & 0 deletions test/Unit/Exception/InvalidMinorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ final class InvalidMinorTest extends Framework\TestCase
{
use Test\Util\Helper;

public function testFromIntReturnsException(): void
{
$value = self::faker()->numberBetween(0);

$exception = Exception\InvalidMinor::fromInt($value);

$message = \sprintf(
'Value "%d" does not appear to be valid.',
$value,
);

self::assertSame($message, $exception->getMessage());
}

public function testFromStringReturnsException(): void
{
$value = self::faker()->word();
Expand Down
14 changes: 14 additions & 0 deletions test/Unit/Exception/InvalidPatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ final class InvalidPatchTest extends Framework\TestCase
{
use Test\Util\Helper;

public function testFromIntReturnsException(): void
{
$value = self::faker()->numberBetween(0);

$exception = Exception\InvalidPatch::fromInt($value);

$message = \sprintf(
'Value "%d" does not appear to be valid.',
$value,
);

self::assertSame($message, $exception->getMessage());
}

public function testFromStringReturnsException(): void
{
$value = self::faker()->word();
Expand Down
28 changes: 23 additions & 5 deletions test/Unit/MajorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\Version\Test\Unit;

use Ergebnis\DataProvider;
use Ergebnis\Version\Exception;
use Ergebnis\Version\Major;
use Ergebnis\Version\Test;
Expand All @@ -24,8 +25,25 @@ final class MajorTest extends Framework\TestCase
{
use Test\Util\Helper;

#[Framework\Attributes\DataProvider('provideInvalidValue')]
public function testFromStringRejectsInvalidValue(string $value): void
#[Framework\Attributes\DataProviderExternal(DataProvider\IntProvider::class, 'lessThanZero')]
public function testFromIntRejectsInvalidIntValue(int $value): void
{
$this->expectException(Exception\InvalidMajor::class);

Major::fromInt($value);
}

#[Framework\Attributes\DataProviderExternal(DataProvider\IntProvider::class, 'zero')]
#[Framework\Attributes\DataProviderExternal(DataProvider\IntProvider::class, 'greaterThanZero')]
public function testFromIntReturnsMajor(int $value): void
{
$major = Major::fromInt($value);

self::assertSame($value, $major->toInt());
}

#[Framework\Attributes\DataProvider('provideInvalidStringValue')]
public function testFromStringRejectsInvalidStringValue(string $value): void
{
$this->expectException(Exception\InvalidMajor::class);

Expand All @@ -38,7 +56,7 @@ public function testFromStringRejectsInvalidValue(string $value): void
*
* @return \Generator<string, array{0: string}>
*/
public static function provideInvalidValue(): \Generator
public static function provideInvalidStringValue(): \Generator
{
$faker = self::faker();

Expand All @@ -57,7 +75,7 @@ public static function provideInvalidValue(): \Generator
}
}

#[Framework\Attributes\DataProvider('provideValidValue')]
#[Framework\Attributes\DataProvider('provideValidStringValue')]
public function testFromStringReturnsMajor(string $value): void
{
$major = Major::fromString($value);
Expand All @@ -71,7 +89,7 @@ public function testFromStringReturnsMajor(string $value): void
*
* @return \Generator<string, array{0: string}>
*/
public static function provideValidValue(): \Generator
public static function provideValidStringValue(): \Generator
{
$values = [
'zero' => '0',
Expand Down
Loading

0 comments on commit 0bff890

Please sign in to comment.