API-Skeletons /
doctrine-orm-graphql
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace ApiSkeletons\Doctrine\ORM\GraphQL; |
||
| 6 | |||
| 7 | use ApiSkeletons\Doctrine\ORM\GraphQL\Filter\Filters; |
||
| 8 | use InvalidArgumentException; |
||
| 9 | |||
| 10 | use function array_merge; |
||
| 11 | use function property_exists; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * This class is used for setting parameters when |
||
| 15 | * creating the driver |
||
| 16 | */ |
||
| 17 | class Config |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var string The GraphQL group. This allows multiple GraphQL |
||
| 21 | * configurations within the same application or |
||
| 22 | * even within the same group of entities and Object Manager. |
||
| 23 | */ |
||
| 24 | protected readonly string $group; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string|null The group is usually suffixed to GraphQL type names. |
||
| 28 | * You may specify a different string for the group suffix |
||
| 29 | * or you my supply an empty string to exclude the suffix. |
||
| 30 | * Be warned, using the same groupSuffix with two different |
||
| 31 | * groups can cause collisions. |
||
| 32 | */ |
||
| 33 | protected readonly string|null $groupSuffix; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var bool When set to true hydrator results will be cached for the |
||
| 37 | * duration of the request thereby saving multiple extracts for |
||
| 38 | * the same entity. |
||
| 39 | */ |
||
| 40 | protected readonly bool $useHydratorCache; |
||
| 41 | |||
| 42 | /** @var int A hard limit for fetching any collection within the schema */ |
||
| 43 | protected readonly int $limit; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var bool When set to true all fields and all associations will be |
||
| 47 | * enabled. This is best used as a development setting when |
||
| 48 | * the entities are subject to change. |
||
| 49 | */ |
||
| 50 | protected readonly bool $globalEnable; |
||
| 51 | |||
| 52 | /** @var string[] An array of field names to ignore when using globalEnable. */ |
||
| 53 | protected readonly array $ignoreFields; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var bool|null When set to true, all entities will be extracted by value |
||
| 57 | * across all hydrators in the driver. When set to false, |
||
| 58 | * all hydrators will extract by reference. This overrides |
||
| 59 | * per-entity attribute configuration. |
||
| 60 | */ |
||
| 61 | protected readonly bool|null $globalByValue; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string|null When set, the entityPrefix will be removed from each |
||
| 65 | * type name. This simplifies type names and makes reading |
||
| 66 | * the GraphQL documentation easier. |
||
| 67 | */ |
||
| 68 | protected readonly string|null $entityPrefix; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var bool|null When set to true entity fields will be |
||
| 72 | * sorted alphabetically |
||
| 73 | */ |
||
| 74 | protected readonly bool|null $sortFields; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var Filters[] An array of filters to exclude from |
||
| 78 | * available filters for all fields and |
||
| 79 | * associations in every entity |
||
| 80 | */ |
||
| 81 | protected readonly array $excludeFilters; |
||
| 82 | |||
| 83 | /** @param mixed[] $config */ |
||
| 84 | public function __construct(array $config = []) |
||
| 85 | { |
||
| 86 | $default = [ |
||
| 87 | 'group' => 'default', |
||
| 88 | 'groupSuffix' => null, |
||
| 89 | 'useHydratorCache' => false, |
||
| 90 | 'limit' => 1000, |
||
| 91 | 'globalEnable' => false, |
||
| 92 | 'ignoreFields' => [], |
||
| 93 | 'globalByValue' => null, |
||
| 94 | 'entityPrefix' => null, |
||
| 95 | 'sortFields' => null, |
||
| 96 | 'excludeFilters' => [], |
||
| 97 | ]; |
||
| 98 | |||
| 99 | $mergedConfig = array_merge($default, $config); |
||
| 100 | |||
| 101 | foreach ($mergedConfig as $field => $value) { |
||
| 102 | if (! property_exists($this, $field)) { |
||
| 103 | throw new InvalidArgumentException('Invalid configuration setting: ' . $field); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | // Assigning properties explicitly is phpstan friendly |
||
| 108 | $this->group = $mergedConfig['group']; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 109 | $this->groupSuffix = $mergedConfig['groupSuffix']; |
||
|
0 ignored issues
–
show
|
|||
| 110 | $this->useHydratorCache = $mergedConfig['useHydratorCache']; |
||
|
0 ignored issues
–
show
|
|||
| 111 | $this->limit = $mergedConfig['limit']; |
||
|
0 ignored issues
–
show
|
|||
| 112 | $this->globalEnable = $mergedConfig['globalEnable']; |
||
|
0 ignored issues
–
show
|
|||
| 113 | $this->ignoreFields = $mergedConfig['ignoreFields']; |
||
|
0 ignored issues
–
show
|
|||
| 114 | $this->globalByValue = $mergedConfig['globalByValue']; |
||
|
0 ignored issues
–
show
|
|||
| 115 | $this->entityPrefix = $mergedConfig['entityPrefix']; |
||
|
0 ignored issues
–
show
|
|||
| 116 | $this->sortFields = $mergedConfig['sortFields']; |
||
|
0 ignored issues
–
show
|
|||
| 117 | $this->excludeFilters = $mergedConfig['excludeFilters']; |
||
|
0 ignored issues
–
show
|
|||
| 118 | } |
||
| 119 | |||
| 120 | public function getGroup(): string |
||
| 121 | { |
||
| 122 | return $this->group; |
||
| 123 | } |
||
| 124 | |||
| 125 | public function getGroupSuffix(): string|null |
||
| 126 | { |
||
| 127 | return $this->groupSuffix; |
||
| 128 | } |
||
| 129 | |||
| 130 | public function getUseHydratorCache(): bool |
||
| 131 | { |
||
| 132 | return $this->useHydratorCache; |
||
| 133 | } |
||
| 134 | |||
| 135 | public function getLimit(): int |
||
| 136 | { |
||
| 137 | return $this->limit; |
||
| 138 | } |
||
| 139 | |||
| 140 | public function getGlobalEnable(): bool |
||
| 141 | { |
||
| 142 | return $this->globalEnable; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** @return string[] */ |
||
| 146 | public function getIgnoreFields(): array |
||
| 147 | { |
||
| 148 | return $this->ignoreFields; |
||
| 149 | } |
||
| 150 | |||
| 151 | public function getGlobalByValue(): bool|null |
||
| 152 | { |
||
| 153 | return $this->globalByValue; |
||
| 154 | } |
||
| 155 | |||
| 156 | public function getEntityPrefix(): string|null |
||
| 157 | { |
||
| 158 | return $this->entityPrefix; |
||
| 159 | } |
||
| 160 | |||
| 161 | public function getSortFields(): bool|null |
||
| 162 | { |
||
| 163 | return $this->sortFields; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** @return Filters[] */ |
||
| 167 | public function getExcludeFilters(): array |
||
| 168 | { |
||
| 169 | return $this->excludeFilters; |
||
| 170 | } |
||
| 171 | } |
||
| 172 |