-
-
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.
Merge pull request #11 from petrknap/typed-optionals
Implemented `TypedOptional`
- Loading branch information
Showing
7 changed files
with
149 additions
and
1 deletion.
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Optional\Exception; | ||
|
||
use RuntimeException; | ||
|
||
final class CouldNotFindTypedOptionalForValue extends RuntimeException implements TypedOptionalException | ||
{ | ||
public function __construct( | ||
private readonly mixed $value, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
public function getValue(): mixed | ||
{ | ||
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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Optional\Exception; | ||
|
||
use RuntimeException; | ||
|
||
final class CouldNotRegisterNonOptional extends RuntimeException implements TypedOptionalException | ||
{ | ||
/** | ||
* @param class-string $className | ||
*/ | ||
public function __construct( | ||
private readonly string $className, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* @return class-string | ||
*/ | ||
public function getClassName(): string | ||
{ | ||
return $this->className; | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Optional\Exception; | ||
|
||
interface TypedOptionalException extends 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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\Optional; | ||
|
||
use InvalidArgumentException; | ||
|
||
final class TypedOptional | ||
{ | ||
/** @var array<class-string> must be iterated in reverse order */ | ||
private static array $typedOptionals = [ | ||
OptionalArray::class, | ||
OptionalBool::class, | ||
OptionalFloat::class, | ||
OptionalInt::class, | ||
OptionalObject::class, | ||
OptionalObject\OptionalStdClass::class, | ||
OptionalResource::class, | ||
OptionalResource\OptionalStream::class, | ||
OptionalString::class, | ||
]; | ||
|
||
/** | ||
* @template T of mixed | ||
* | ||
* @param T $value | ||
* | ||
* @return Optional<T> | ||
* | ||
* @throws Exception\CouldNotFindTypedOptionalForValue | ||
*/ | ||
public static function of(mixed $value): Optional | ||
{ | ||
/** @var class-string<Optional<T>> $typedOptional */ | ||
foreach (array_reverse(self::$typedOptionals) as $typedOptional) { | ||
try { | ||
return $typedOptional::of($value); | ||
} catch (InvalidArgumentException) { | ||
} | ||
} | ||
throw new Exception\CouldNotFindTypedOptionalForValue($value); | ||
} | ||
|
||
/** | ||
* @param class-string $typedOptionalClassName | ||
* | ||
* @throws Exception\CouldNotRegisterNonOptional | ||
*/ | ||
public static function register(string $typedOptionalClassName): void | ||
{ | ||
if (!is_a($typedOptionalClassName, Optional::class, allow_string: true)) { | ||
throw new Exception\CouldNotRegisterNonOptional($typedOptionalClassName); | ||
} | ||
self::$typedOptionals[] = $typedOptionalClassName; | ||
} | ||
} |
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