From 705a50d6818e4bd4aa1016fdaae0f5a48e18c813 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 18 Oct 2024 22:27:29 +0300 Subject: [PATCH] creating configuration and type casting --- app.php | 16 ++++++- composer.json | 2 +- src/Casting/Casting.php | 10 ++++ src/Casting/MapperType.php | 46 +++++++++++++++++++ src/Casting/Type/AbstractType.php | 21 +++++++++ src/Casting/Type/IntegerType.php | 14 ++++++ src/Casting/Type/StringType.php | 14 ++++++ src/Casting/Type/Type.php | 10 ++++ src/Configuration/Configuration.php | 39 ++++++++++++++++ .../Casting/TypeNotFountException.php | 9 ++++ src/Exception/Exception.php | 7 +++ tests/Casting/MapperTypeTest.php | 44 ++++++++++++++++++ tests/Configuration/ConfigurationTest.php | 23 ++++++++++ tests/benchmark.php | 1 + 14 files changed, 253 insertions(+), 3 deletions(-) create mode 100644 src/Casting/Casting.php create mode 100644 src/Casting/MapperType.php create mode 100644 src/Casting/Type/AbstractType.php create mode 100644 src/Casting/Type/IntegerType.php create mode 100644 src/Casting/Type/StringType.php create mode 100644 src/Casting/Type/Type.php create mode 100644 src/Configuration/Configuration.php create mode 100644 src/Exception/Casting/TypeNotFountException.php create mode 100644 src/Exception/Exception.php create mode 100644 tests/Casting/MapperTypeTest.php create mode 100644 tests/Configuration/ConfigurationTest.php create mode 100644 tests/benchmark.php diff --git a/app.php b/app.php index daa7c14..bad2cd4 100644 --- a/app.php +++ b/app.php @@ -1,6 +1,18 @@ set('param', 123) +; + +dd($configuration->getCastingType('param', 'string')->getAsCasting()); + +exit(); \define('RED', "\033[0;31m"); \define('GREEN', "\033[1;32m"); @@ -40,7 +52,7 @@ function sendRequest ($method = 'GET', $endpoint = '', $data = []): array { $endpoint .= '?access_token=' . \getenv('gitea_access_token'); } - \curl_setopt($curl, CURLOPT_URL, app . php\getenv('gitea_instance_base_url') . $endpoint /* . '?access_token=' . \getenv('gitea_access_token')*/); + \curl_setopt($curl, CURLOPT_URL, \getenv('gitea_instance_base_url') . $endpoint /* . '?access_token=' . \getenv('gitea_access_token')*/); \curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); \curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); \curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); diff --git a/composer.json b/composer.json index d7c9201..c63acce 100644 --- a/composer.json +++ b/composer.json @@ -62,7 +62,7 @@ "@phpunit", "@infection" ], - "unused": "composer-unused --excludePackage=ext-zip", + "unused": "composer-unused --excludePackage=ext-curl", "validate": "composer validate --strict --ansi" } } diff --git a/src/Casting/Casting.php b/src/Casting/Casting.php new file mode 100644 index 0000000..5f9bf8a --- /dev/null +++ b/src/Casting/Casting.php @@ -0,0 +1,10 @@ + StringType::class, + self::INTEGER => IntegerType::class, + self::INT => IntegerType::class, + ]; + + private string $class; + + /** + * @throws TypeNotFountException + */ + public function __construct(string $type) + { + if (!isset(self::MAP[$type])) { + throw new TypeNotFountException( + \sprintf('Casting type "%s" is not found.', $type), + ); + } + + $this->class = self::MAP[$type]; + } + + /** + * @return mixed + */ + public function create(mixed $value): Type + { + return new $this->class($value); + } +} diff --git a/src/Casting/Type/AbstractType.php b/src/Casting/Type/AbstractType.php new file mode 100644 index 0000000..7b9368c --- /dev/null +++ b/src/Casting/Type/AbstractType.php @@ -0,0 +1,21 @@ +value; + } + + #[\Override] + abstract public function getAsCasting(): mixed; +} diff --git a/src/Casting/Type/IntegerType.php b/src/Casting/Type/IntegerType.php new file mode 100644 index 0000000..cae613d --- /dev/null +++ b/src/Casting/Type/IntegerType.php @@ -0,0 +1,14 @@ +getValue(); + } +} diff --git a/src/Casting/Type/StringType.php b/src/Casting/Type/StringType.php new file mode 100644 index 0000000..8eadcf8 --- /dev/null +++ b/src/Casting/Type/StringType.php @@ -0,0 +1,14 @@ +getValue(); + } +} diff --git a/src/Casting/Type/Type.php b/src/Casting/Type/Type.php new file mode 100644 index 0000000..a98cb6a --- /dev/null +++ b/src/Casting/Type/Type.php @@ -0,0 +1,10 @@ +options[$key] = $value; + + return $this; + } + + public function get(string $key): mixed + { + return $this->options[$key] ?? null; + } + + /** + * @throws TypeNotFountException + */ + public function getCastingType(string $key, string $type): Type + { + return (new MapperType($type))->create($this->get($key)); + } +} diff --git a/src/Exception/Casting/TypeNotFountException.php b/src/Exception/Casting/TypeNotFountException.php new file mode 100644 index 0000000..7384676 --- /dev/null +++ b/src/Exception/Casting/TypeNotFountException.php @@ -0,0 +1,9 @@ +create('value'); + + self::assertInstanceOf( + $type, + StringType::class, + ); + } + + public function testGetIntegerType(): void + { + $type = (new MapperType(MapperType::INTEGER))->create(1); + + self::assertInstanceOf( + $type, + IntegerType::class, + ); + } + + public function testGetIntType(): void + { + $type = (new MapperType(MapperType::INT))->create(1); + + self::assertInstanceOf( + $type, + IntegerType::class, + ); + } +} diff --git a/tests/Configuration/ConfigurationTest.php b/tests/Configuration/ConfigurationTest.php new file mode 100644 index 0000000..6b9c653 --- /dev/null +++ b/tests/Configuration/ConfigurationTest.php @@ -0,0 +1,23 @@ +set('test-option', 'test-value'); + + self::assertEquals( + $configuration->get('test-option'), + 'test-value', + ); + } +} diff --git a/tests/benchmark.php b/tests/benchmark.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/tests/benchmark.php @@ -0,0 +1 @@ +