-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRelationship.php
168 lines (148 loc) · 5.64 KB
/
Relationship.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php declare(strict_types = 1);
namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Complex;
use Closure;
use GraphQL\Language\DirectiveLocation;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model as EloquentModel;
use LastDragon_ru\LaraASP\Eloquent\ModelHelper;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\BuilderFieldResolver;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Context;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Handler;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeProvider;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\OperatorUnsupportedBuilder;
use LastDragon_ru\LaraASP\GraphQL\Builder\Field;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Definitions\SearchByOperatorConditionDirective;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Exceptions\OperatorInvalidArgumentValue;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Operator;
use Nuwave\Lighthouse\Execution\Arguments\Argument;
use Nuwave\Lighthouse\Execution\Arguments\ArgumentSet;
use Override;
use function is_a;
use function is_array;
use function is_int;
use function is_string;
use function reset;
class Relationship extends Operator {
public function __construct(
protected readonly SearchByOperatorConditionDirective $field,
BuilderFieldResolver $resolver,
) {
parent::__construct($resolver);
}
// <editor-fold desc="Directive">
// =========================================================================
/**
* @inheritDoc
*/
#[Override]
protected static function locations(): array {
return [
DirectiveLocation::SCALAR,
DirectiveLocation::FIELD_DEFINITION,
];
}
// </editor-fold>
// <editor-fold desc="Operator">
// =========================================================================
#[Override]
public static function getName(): string {
return 'relation';
}
#[Override]
public function isAvailable(TypeProvider $provider, TypeSource $source, Context $context): bool {
return parent::isAvailable($provider, $source, $context)
&& $source->isObject();
}
#[Override]
public function getFieldType(TypeProvider $provider, TypeSource $source, Context $context): ?string {
return $provider->getType(RelationshipType::class, $source, $context);
}
#[Override]
public function getFieldDescription(): ?string {
return 'Relationship condition.';
}
#[Override]
protected function isBuilderSupported(string $builder): bool {
return is_a($builder, EloquentBuilder::class, true);
}
#[Override]
public function call(
Handler $handler,
object $builder,
Field $field,
Argument $argument,
Context $context,
): object {
// Supported?
if (!($builder instanceof EloquentBuilder)) {
throw new OperatorUnsupportedBuilder($this, $builder);
}
// ArgumentSet?
if (!($argument->value instanceof ArgumentSet)) {
throw new OperatorInvalidArgumentValue($this, ArgumentSet::class, $argument->value);
}
// Possible variants:
// * where = whereHas
// * where + count = whereHas
// * where + exists = whereHas
// * where + notExists = doesntHave
// Conditions
$relation = (new ModelHelper($builder))->getRelation($field->getName());
$has = $argument->value->arguments['where'] ?? null;
$hasCount = $argument->value->arguments['count'] ?? null;
$notExists = (bool) ($argument->value->arguments['notExists']->value ?? false);
// Build
$alias = $relation->getRelationCountHash(false);
$count = 1;
$operator = '>=';
if ($hasCount instanceof Argument) {
$query = $builder->getQuery()->newQuery();
$query = $this->field->call($handler, $query, new Field(), $hasCount, $context);
$where = reset($query->wheres);
$count = is_array($where) && isset($where['value']) && is_int($where['value'])
? $where['value']
: $count;
$operator = is_array($where) && isset($where['operator']) && is_string($where['operator'])
? $where['operator']
: $operator;
} elseif ($notExists) {
$count = 1;
$operator = '<';
} else {
// empty
}
// Build
$this->build(
$builder,
$field,
$operator,
$count,
static function (EloquentBuilder $builder) use ($context, $relation, $handler, $alias, $has): void {
if ($alias === '' || $alias === $relation->getRelationCountHash(false)) {
$alias = $builder->getModel()->getTable();
}
if ($has instanceof Argument && $has->value instanceof ArgumentSet) {
$handler->handle($builder, new Field($alias), $has->value, $context);
}
},
);
// Return
return $builder;
}
/**
* @template TBuilder of EloquentBuilder<EloquentModel>
*
* @param TBuilder $builder
* @param Closure(TBuilder): void $closure
*/
protected function build(
EloquentBuilder $builder,
Field $field,
string $operator,
int $count,
Closure $closure,
): void {
$builder->whereHas($field->getName(), $closure, $operator, $count);
}
}