Skip to content

Commit

Permalink
generics refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
arutyunyan committed Apr 19, 2024
1 parent 8450090 commit b40f078
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 32 deletions.
7 changes: 0 additions & 7 deletions src/PythonDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ public function getObject(): object
{
return $this->object;
}
/**
* TODO: может быть стоит переименовать в isBound
*/
public function initialized(): bool
{
return isset($this->object);
}
/**
* Получает название свойства или метода к которому привязан декоратор
* @return string
Expand Down
4 changes: 3 additions & 1 deletion src/modules/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
class Module
{
const NOT_FOUND = 400;

/**
* Абсолютно любой объект который использует декораторы
* В данный объект внедряются пользовательские модули
Expand Down Expand Up @@ -64,7 +66,7 @@ public static function getInstanceFrom(object $object)
$module->object = $object;

if (!$module->canImplement()) {
throw new ModuleError('', 400); // TODO: Придумать текст исключения
throw new ModuleError('', self::NOT_FOUND); // TODO: Придумать текст исключения
}

if (!$module->hasInstance()) {
Expand Down
48 changes: 24 additions & 24 deletions src/modules/generics/core/T.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,19 @@

use Attribute;
use Sagittaracc\PhpPythonDecorator\exceptions\GenericError;
use Sagittaracc\PhpPythonDecorator\exceptions\ModuleError;
use Sagittaracc\PhpPythonDecorator\modules\generics\Generics;
use Sagittaracc\PhpPythonDecorator\modules\Module;
use Sagittaracc\PhpPythonDecorator\modules\validation\Validation;

#[Attribute]
class T extends Generic
{
private function getEntityByValue($value)
{
if (!$this->initialized()) {
return null;
}

$modules = $this->getObject()->scope['modules'];

if (!isset($modules[Generics::class])) {
return null;
}

$cfg = $modules[Generics::class];

$entityIndex = array_search($value, $cfg['generics']);
$entity = $cfg['entities'][$entityIndex] ?? null;
$module = $this->getObject()->scope['modules'][Generics::class];
$entityIndex = array_search($value, $module['generics']);
$entity = $module['entities'][$entityIndex] ?? null;

return $entity;
}
Expand All @@ -34,10 +25,6 @@ private function checkGeneric($value)
{
$entity = $this->getEntityByValue(static::class);

if (!$entity) {
return false;
}

if (in_array($entity, Validation::$primitives)) {
if ((new $entity)->validate($value)) {
return true;
Expand All @@ -54,17 +41,30 @@ private function checkGeneric($value)
throw new GenericError;
}

private function registerGeneric(&$generics, $object)
private function registerGeneric($object, &$module)
{
$generics = Generics::getInstanceFrom($object);
$generics->addName(static::class);
if (!is_object($object)) {
return false;
}

try {
$module = Generics::getInstanceFrom($object);
}
catch (ModuleError $e) {
if ($e->getCode() === Module::NOT_FOUND) {
return false;
}
}

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

return true;
}

public function wrapper(mixed $object_or_value)
{
// TODO: Переставить местами registerGeneric и checkGeneric - что будет более логично
$this->checkGeneric($object_or_value) || $this->registerGeneric($generics, $object_or_value);
$this->registerGeneric($object_or_value, $module) || $this->checkGeneric($object_or_value);

return fn(...$args) => $generics->addEntities($args);
return fn(...$args) => $module->addEntities($args);
}
}

0 comments on commit b40f078

Please sign in to comment.