creating configuration and type casting

This commit is contained in:
Your Name
2024-10-18 22:27:29 +03:00
parent 7744d22039
commit 705a50d681
14 changed files with 253 additions and 3 deletions

16
app.php
View File

@@ -1,6 +1,18 @@
<?php
exit('++++');
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use App\Configuration\Configuration;
$configuration = (new Configuration())
->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);

View File

@@ -62,7 +62,7 @@
"@phpunit",
"@infection"
],
"unused": "composer-unused --excludePackage=ext-zip",
"unused": "composer-unused --excludePackage=ext-curl",
"validate": "composer validate --strict --ansi"
}
}

10
src/Casting/Casting.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace App\Casting;
interface Casting
{
public function getAsCasting(): mixed;
}

View File

@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace App\Casting;
use App\Casting\Type\IntegerType;
use App\Casting\Type\StringType;
use App\Casting\Type\Type;
use App\Exception\Casting\TypeNotFountException;
final readonly class MapperType
{
public const string STRING = 'string';
public const string INTEGER = 'integer';
public const string INT = 'int';
private const array MAP = [
self::STRING => 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);
}
}

View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace App\Casting\Type;
use App\Casting\Casting;
abstract class AbstractType implements Type, Casting
{
public function __construct(protected mixed $value) {}
#[\Override]
final public function getValue()
{
return $this->value;
}
#[\Override]
abstract public function getAsCasting(): mixed;
}

View File

@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace App\Casting\Type;
final class IntegerType extends AbstractType
{
#[\Override]
public function getAsCasting(): int
{
return (int) $this->getValue();
}
}

View File

@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace App\Casting\Type;
final class StringType extends AbstractType
{
#[\Override]
public function getAsCasting(): string
{
return (string) $this->getValue();
}
}

10
src/Casting/Type/Type.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace App\Casting\Type;
interface Type
{
public function getValue();
}

View File

@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace App\Configuration;
use App\Casting\MapperType;
use App\Casting\Type\Type;
use App\Exception\Casting\TypeNotFountException;
final class Configuration
{
public array $options = [];
public function __construct()
{
return $this;
}
public function set(string $key, mixed $value): self
{
$this->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));
}
}

View File

@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace App\Exception\Casting;
use App\Exception\Exception;
final class TypeNotFountException extends Exception {}

View File

@@ -0,0 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Exception;
final class Exception extends \Exception {}

View File

@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace App\Casting;
use App\Casting\Type\IntegerType;
use App\Casting\Type\StringType;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(MapperType::class)]
final class MapperTypeTest extends TestCase
{
public function testGetStringType(): void
{
$type = (new MapperType(MapperType::STRING))->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,
);
}
}

View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace App\Configuration;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(Configuration::class)]
final class ConfigurationTest extends TestCase
{
public function testUseConfiguration(): void
{
$configuration = new Configuration();
$configuration->set('test-option', 'test-value');
self::assertEquals(
$configuration->get('test-option'),
'test-value',
);
}
}

1
tests/benchmark.php Normal file
View File

@@ -0,0 +1 @@
<?php