You've already forked gitea-composer-upload-action
creating configuration and type casting
This commit is contained in:
16
app.php
16
app.php
@@ -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);
|
||||
|
||||
@@ -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
10
src/Casting/Casting.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Casting;
|
||||
|
||||
interface Casting
|
||||
{
|
||||
public function getAsCasting(): mixed;
|
||||
}
|
||||
46
src/Casting/MapperType.php
Normal file
46
src/Casting/MapperType.php
Normal 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);
|
||||
}
|
||||
}
|
||||
21
src/Casting/Type/AbstractType.php
Normal file
21
src/Casting/Type/AbstractType.php
Normal 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;
|
||||
}
|
||||
14
src/Casting/Type/IntegerType.php
Normal file
14
src/Casting/Type/IntegerType.php
Normal 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();
|
||||
}
|
||||
}
|
||||
14
src/Casting/Type/StringType.php
Normal file
14
src/Casting/Type/StringType.php
Normal 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
10
src/Casting/Type/Type.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Casting\Type;
|
||||
|
||||
interface Type
|
||||
{
|
||||
public function getValue();
|
||||
}
|
||||
39
src/Configuration/Configuration.php
Normal file
39
src/Configuration/Configuration.php
Normal 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));
|
||||
}
|
||||
}
|
||||
9
src/Exception/Casting/TypeNotFountException.php
Normal file
9
src/Exception/Casting/TypeNotFountException.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Exception\Casting;
|
||||
|
||||
use App\Exception\Exception;
|
||||
|
||||
final class TypeNotFountException extends Exception {}
|
||||
7
src/Exception/Exception.php
Normal file
7
src/Exception/Exception.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Exception;
|
||||
|
||||
final class Exception extends \Exception {}
|
||||
44
tests/Casting/MapperTypeTest.php
Normal file
44
tests/Casting/MapperTypeTest.php
Normal 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,
|
||||
);
|
||||
}
|
||||
}
|
||||
23
tests/Configuration/ConfigurationTest.php
Normal file
23
tests/Configuration/ConfigurationTest.php
Normal 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
1
tests/benchmark.php
Normal file
@@ -0,0 +1 @@
|
||||
<?php
|
||||
Reference in New Issue
Block a user