forked from thecodingmachine/graphqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtendType.php
More file actions
74 lines (65 loc) · 2.45 KB
/
ExtendType.php
File metadata and controls
74 lines (65 loc) · 2.45 KB
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
<?php
declare(strict_types=1);
namespace TheCodingMachine\GraphQLite\Annotations;
use Attribute;
use BadMethodCallException;
use TheCodingMachine\GraphQLite\Annotations\Exceptions\ClassNotFoundException;
use function class_exists;
use function interface_exists;
use function ltrim;
/**
* The ExtendType attribute must be put in a GraphQL type class docblock and is used to add additional fields to the underlying PHP class.
*/
#[Attribute(Attribute::TARGET_CLASS)]
class ExtendType
{
/** @var class-string<object>|null */
private string|null $class;
private string|null $name;
private string|null $description;
/** @param mixed[] $attributes */
public function __construct(
array $attributes = [],
string|null $class = null,
string|null $name = null,
string|null $description = null,
) {
$className = isset($attributes['class']) ? ltrim($attributes['class'], '\\') : null;
$className = $className ?? $class;
if ($className !== null && ! class_exists($className) && ! interface_exists($className)) {
throw ClassNotFoundException::couldNotFindClass($className);
}
$this->name = $name ?? $attributes['name'] ?? null;
$this->class = $className;
$this->description = $description ?? $attributes['description'] ?? null;
if (! $this->class && ! $this->name) {
throw new BadMethodCallException('In attribute #[ExtendType], missing one of the compulsory parameter "class" or "name".');
}
}
/**
* Returns the name of the GraphQL query/mutation/field.
* If not specified, the name of the method should be used instead.
*
* @return class-string<object>|null
*/
public function getClass(): string|null
{
return $this->class;
}
public function getName(): string|null
{
return $this->name;
}
/**
* Returns the explicit description contributed by this type extension, or null if none was provided.
*
* A GraphQL type carries exactly one description. If both the base #[Type] and this #[ExtendType]
* (or multiple #[ExtendType] attributes targeting the same class) provide a description, the
* schema builder throws DuplicateDescriptionOnTypeException. Descriptions may therefore live on
* #[Type] OR on at most one #[ExtendType], never on both.
*/
public function getDescription(): string|null
{
return $this->description;
}
}