-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implemented simple version of Optional
- Loading branch information
Showing
7 changed files
with
160 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
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Optional\Exception; | ||
|
||
use RuntimeException; | ||
|
||
final class NoSuchElement extends RuntimeException implements OptionalException | ||
{ | ||
} |
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Optional\Exception; | ||
|
||
use Throwable; | ||
|
||
interface OptionalException extends Throwable | ||
{ | ||
} |
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,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Optional; | ||
|
||
use LogicException; | ||
|
||
/** | ||
* @template T of mixed type of non-null value | ||
* | ||
* @see https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html | ||
*/ | ||
final class Optional | ||
{ | ||
private bool|null $wasPresent = null; | ||
|
||
/** | ||
* @param T|null $value | ||
*/ | ||
public function __construct( | ||
private readonly mixed $value, | ||
) { | ||
} | ||
|
||
/** | ||
* @return self<null> | ||
*/ | ||
public static function empty(): self | ||
{ | ||
return new self(null); | ||
} | ||
|
||
public function isPresent(): bool | ||
{ | ||
return $this->wasPresent = $this->value !== null; | ||
} | ||
|
||
/** | ||
* @return T | ||
* | ||
* @throws Exception\NoSuchElement | ||
*/ | ||
public function get(): mixed | ||
{ | ||
/** @var T */ | ||
return match ($this->wasPresent) { | ||
true => $this->value, | ||
false => throw new Exception\NoSuchElement(), | ||
null => throw new LogicException('Call `isPresent()` before accessing the 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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Optional; | ||
|
||
use LogicException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class OptionalTest extends TestCase | ||
{ | ||
private const VALUE = 'test'; | ||
|
||
public function testGetReturnsValueWhenValueIsPresent(): void | ||
{ | ||
$optional = new Optional(self::VALUE); | ||
|
||
self::assertTrue($optional->isPresent()); | ||
self::assertSame(self::VALUE, $optional->get()); | ||
} | ||
|
||
public function testGetThrowsWhenValueIsNotPresent(): void | ||
{ | ||
$optional = Optional::empty(); | ||
|
||
self::assertFalse($optional->isPresent()); | ||
self::expectException(Exception\NoSuchElement::class); | ||
$optional->get(); | ||
} | ||
|
||
public function testGetThrowsWhenCalledSeparately(): void | ||
{ | ||
$optional = new Optional(self::VALUE); | ||
|
||
self::expectException(LogicException::class); | ||
$optional->get(); | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Optional; | ||
|
||
use PetrKnap\Shorts\PhpUnit\MarkdownFileTestInterface; | ||
use PetrKnap\Shorts\PhpUnit\MarkdownFileTestTrait; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ReadmeTest extends TestCase implements MarkdownFileTestInterface | ||
{ | ||
use MarkdownFileTestTrait; | ||
|
||
public static function getPathToMarkdownFile(): string | ||
{ | ||
return __DIR__ . '/../README.md'; | ||
} | ||
|
||
public static function getExpectedOutputsOfPhpExamples(): iterable | ||
{ | ||
return [ | ||
'isPresent() -> get()' => 'value', | ||
]; | ||
} | ||
} |