|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Ecodev\Felix\Api\Output; |
|
6
|
|
|
|
|
7
|
|
|
use Ecodev\Felix\Model\Model; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
use GraphQL\Type\Definition\ObjectType; |
|
10
|
|
|
use Laminas\ServiceManager\Factory\AbstractFactoryInterface; |
|
11
|
|
|
use Psr\Container\ContainerInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Create a Pagination type for a Doctrine entity or an ObjectType extracted from name. |
|
15
|
|
|
* |
|
16
|
|
|
* For example, if given "ActionPagination", it will create a Pagination type for the Action entity. |
|
17
|
|
|
* Alternatively, if given "ReportRowPagination", it will create a Pagination type for the custom `OutputType` of `ReportRowType`. |
|
18
|
|
|
*/ |
|
19
|
|
|
class PaginationTypeFactory implements AbstractFactoryInterface |
|
20
|
|
|
{ |
|
21
|
|
|
private const PATTERN = '~^(.*)Pagination$~'; |
|
22
|
|
|
|
|
23
|
|
|
public function canCreate(ContainerInterface $container, $requestedName): bool |
|
24
|
|
|
{ |
|
25
|
|
|
$class = $this->getClass($requestedName); |
|
26
|
|
|
|
|
27
|
|
|
return $class && (is_a($class, Model::class, true) || is_a($class, ObjectType::class, true)); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): PaginationType |
|
31
|
|
|
{ |
|
32
|
|
|
$class = $this->getClass($requestedName); |
|
33
|
|
|
if (!$class) { |
|
34
|
|
|
throw new Exception('Cannot create a PaginationType for a name not matching a model: ' . $requestedName); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$extraFields = $this->getExtraFields($class); |
|
38
|
|
|
|
|
39
|
|
|
$type = new PaginationType($class, $requestedName, $extraFields); |
|
40
|
|
|
|
|
41
|
|
|
return $type; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return null|class-string |
|
46
|
|
|
*/ |
|
47
|
|
|
private function getClass(string $requestedName): ?string |
|
48
|
|
|
{ |
|
49
|
|
|
if (!preg_match(self::PATTERN, $requestedName, $m)) { |
|
50
|
|
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$possibilities = [ |
|
54
|
|
|
'Application\Model\\' . $m[1], |
|
55
|
|
|
'Application\Model\Relation\\' . $m[1], |
|
56
|
|
|
'Application\Api\Output\\' . $m[1] . 'Type', |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
foreach ($possibilities as $class) { |
|
60
|
|
|
if (class_exists($class)) { |
|
61
|
|
|
return $class; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* GraphQL configuration for extra fields, typically for aggregated fields only available on some entities. |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function getExtraFields(string $class): array |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
return []; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.