Last active
December 5, 2023 08:37
-
-
Save emmadesilva/c64628fa48d07ffd1568c1189485921b to your computer and use it in GitHub Desktop.
Idea for a strongly typed systen
This file contains hidden or 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
| <?php | |
| declare(strict_types=1); | |
| final class Types | |
| { | |
| protected static bool $strict = false; | |
| public static function string(mixed $input): string | |
| { | |
| if (self::$strict && ! is_string($input)) { | |
| self::throwTypeError('string', $input); | |
| } | |
| return (string) $input; | |
| } | |
| protected static function throwTypeError(string $type, mixed $input) | |
| { | |
| return throw new TypeError(sprintf('Argument 1 passed to %s must be of the type %s, %s given', | |
| __METHOD__, $type, gettype($input)) | |
| ); | |
| } | |
| } | |
| final class Cast | |
| { | |
| public static function toString(mixed $input): string | |
| { | |
| return (string) $input; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment