-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(graphql)!:
Scalars
renamed to Operators
and moved into `…
…Builder`.
- Loading branch information
Showing
25 changed files
with
714 additions
and
550 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...SearchBy/Exceptions/ScalarNoOperators.php → ...rc/Builder/Exceptions/TypeNoOperators.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...src/SearchBy/Exceptions/ScalarUnknown.php → ...ql/src/Builder/Exceptions/TypeUnknown.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace LastDragon_ru\LaraASP\GraphQL\Builder; | ||
|
||
use GraphQL\Type\Definition\Type; | ||
use Illuminate\Container\Container; | ||
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Operator; | ||
use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\TypeNoOperators; | ||
use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\TypeUnknown; | ||
|
||
use function array_map; | ||
use function array_merge; | ||
use function array_push; | ||
use function array_unique; | ||
use function array_values; | ||
use function is_array; | ||
use function is_string; | ||
|
||
use const SORT_REGULAR; | ||
|
||
abstract class Operators { | ||
public const ID = Type::ID; | ||
public const Int = Type::INT; | ||
public const Float = Type::FLOAT; | ||
public const String = Type::STRING; | ||
public const Boolean = Type::BOOLEAN; | ||
public const Null = 'Null'; | ||
|
||
/** | ||
* Determines default operators available for each type. | ||
* | ||
* @var array<string, array<class-string<Operator>>|string> | ||
*/ | ||
protected array $operators = []; | ||
|
||
/** | ||
* Determines additional operators available for type. | ||
* | ||
* @var array<string, string> | ||
*/ | ||
protected array $extends = []; | ||
|
||
public function __construct( | ||
private Container $container, | ||
) { | ||
// empty | ||
} | ||
|
||
protected function getContainer(): Container { | ||
return $this->container; | ||
} | ||
|
||
public function hasOperators(string $type): bool { | ||
return isset($this->operators[$type]); | ||
} | ||
|
||
/** | ||
* @param array<class-string<Operator>>|string $operators | ||
*/ | ||
public function addOperators(string $type, array|string $operators): void { | ||
if (is_string($operators) && !$this->hasOperators($operators)) { | ||
throw new TypeUnknown($operators); | ||
} | ||
|
||
if (is_array($operators) && !$operators) { | ||
throw new TypeNoOperators($type); | ||
} | ||
|
||
$this->operators[$type] = $operators; | ||
} | ||
|
||
/** | ||
* @return array<Operator> | ||
*/ | ||
public function getOperators(string $type, bool $nullable): array { | ||
// Is known? | ||
if (!$this->hasOperators($type)) { | ||
throw new TypeUnknown($type); | ||
} | ||
|
||
// Base | ||
$base = $type; | ||
$operators = $type; | ||
|
||
do { | ||
$operators = $this->operators[$operators] ?? []; | ||
$isAlias = !is_array($operators); | ||
|
||
if ($isAlias) { | ||
$base = $operators; | ||
} | ||
} while ($isAlias); | ||
|
||
// Create Instances | ||
$container = $this->getContainer(); | ||
$operators = array_map(static function (string $operator) use ($container): Operator { | ||
return $container->make($operator); | ||
}, array_unique($operators)); | ||
|
||
// Extends | ||
if (isset($this->extends[$base])) { | ||
$extends = $this->getOperators($this->extends[$base], $nullable); | ||
$operators = array_merge($operators, $extends); | ||
} | ||
|
||
// Add `null` for nullable | ||
if ($nullable) { | ||
array_push($operators, ...$this->getOperators(static::Null, false)); | ||
} | ||
|
||
// Cleanup | ||
$operators = array_values(array_unique($operators, SORT_REGULAR)); | ||
|
||
// Return | ||
return $operators; | ||
} | ||
} |
Oops, something went wrong.