Skip to content

Commit

Permalink
generic refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
arutyunyan committed Apr 19, 2024
1 parent b92646a commit 096e8ad
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 82 deletions.
10 changes: 10 additions & 0 deletions src/modules/generics/aliases/T.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Sagittaracc\PhpPythonDecorator\modules\generics\aliases;

use Attribute;
use Sagittaracc\PhpPythonDecorator\modules\generics\core\Generic;

#[Attribute]
class T extends Generic
{}
4 changes: 2 additions & 2 deletions src/modules/generics/aliases/U.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Sagittaracc\PhpPythonDecorator\modules\generics\aliases;

use Attribute;
use Sagittaracc\PhpPythonDecorator\modules\generics\core\T;
use Sagittaracc\PhpPythonDecorator\modules\generics\core\Generic;

#[Attribute]
class U extends T
class U extends Generic
{}
62 changes: 60 additions & 2 deletions src/modules/generics/core/Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,65 @@

namespace Sagittaracc\PhpPythonDecorator\modules\generics\core;

use Sagittaracc\PhpPythonDecorator\exceptions\GenericError;
use Sagittaracc\PhpPythonDecorator\exceptions\module\NotFoundModuleError;
use Sagittaracc\PhpPythonDecorator\modules\generics\Generics;
use Sagittaracc\PhpPythonDecorator\modules\validation\Validation;
use Sagittaracc\PhpPythonDecorator\PythonDecorator;

abstract class Generic extends PythonDecorator implements GenericInterface
{}
class Generic extends PythonDecorator
{
protected function getEntityByValue($value)
{
$module = $this->getObject()->scope['modules'][Generics::class];
$entityIndex = array_search($value, $module['generics']);
$entity = $module['entities'][$entityIndex];

return $entity;
}

protected function checkGeneric($value)
{
$entity = $this->getEntityByValue(static::class);

if (in_array($entity, Validation::$primitives)) {
if ((new $entity)->validation($value)) {
return true;
}
else {
throw new GenericError();
}
}

if ($value instanceof $entity) {
return true;
}

throw new GenericError;
}

protected function registerGeneric($object, &$module)
{
if (!is_object($object)) {
return false;
}

try {
$module = Generics::getInstanceFrom($object);
}
catch (NotFoundModuleError $e) {
return false;
}

$module->addName(static::class);

return true;
}

public function wrapper(mixed $object_or_value)
{
$this->registerGeneric($object_or_value, $module) || $this->checkGeneric($object_or_value);

return fn(...$args) => $module->addEntities($args);
}
}
6 changes: 0 additions & 6 deletions src/modules/generics/core/GenericInterface.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/modules/generics/core/GenericList.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function wrapper(mixed $object)
$generic = new $genericClass;

if (!($generic instanceof Generic)) {
throw new GenericError('', 400);
throw new GenericError;
}

$generic->wrapper($object);
Expand Down
67 changes: 0 additions & 67 deletions src/modules/generics/core/T.php

This file was deleted.

2 changes: 1 addition & 1 deletion tests/GenericsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use PHPUnit\Framework\TestCase;
use Sagittaracc\PhpPythonDecorator\exceptions\GenericError;
use Sagittaracc\PhpPythonDecorator\modules\generics\aliases\T;
use Sagittaracc\PhpPythonDecorator\modules\generics\aliases\U;
use Sagittaracc\PhpPythonDecorator\modules\generics\core\T;
use Sagittaracc\PhpPythonDecorator\modules\generics\Generics;
use Sagittaracc\PhpPythonDecorator\modules\validation\primitives\Number;
use Sagittaracc\PhpPythonDecorator\modules\validation\primitives\Str;
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Sagittaracc\PhpPythonDecorator\tests\examples;

use Sagittaracc\PhpPythonDecorator\Decorator;
use Sagittaracc\PhpPythonDecorator\modules\generics\core\T;
use Sagittaracc\PhpPythonDecorator\modules\generics\aliases\T;
use Sagittaracc\PhpPythonDecorator\modules\validation\primitives\Str;
use Sagittaracc\PhpPythonDecorator\modules\validation\validators\ArrayOf;
use Sagittaracc\PhpPythonDecorator\modules\validation\validators\Length;
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/MyAnotherBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Sagittaracc\PhpPythonDecorator\tests\examples;

use Sagittaracc\PhpPythonDecorator\Decorator;
use Sagittaracc\PhpPythonDecorator\modules\generics\aliases\T;
use Sagittaracc\PhpPythonDecorator\modules\generics\aliases\U;
use Sagittaracc\PhpPythonDecorator\modules\generics\core\GenericList;
use Sagittaracc\PhpPythonDecorator\modules\generics\core\T;

#[GenericList(T::class, U::class)]
class MyAnotherBox
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/PaymentInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Sagittaracc\PhpPythonDecorator\tests\examples;

use Sagittaracc\PhpPythonDecorator\Decorator;
use Sagittaracc\PhpPythonDecorator\modules\generics\core\T;
use Sagittaracc\PhpPythonDecorator\modules\generics\aliases\T;

#[T]
class PaymentInfo
Expand Down

0 comments on commit 096e8ad

Please sign in to comment.