SchemaDefinitionNode::create()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 21
rs 9.5222
c 0
b 0
f 0
cc 5
nc 5
nop 1
1
<?php
2
3
/**
4
 * This file is part of Railt package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Railt\SDL\Frontend\Ast\Definition;
13
14
use Railt\SDL\Frontend\Ast\Description;
15
use Railt\SDL\Frontend\Ast\Executable\DirectiveNode;
16
use Railt\SDL\Frontend\Ast\Node;
17
use Railt\TypeSystem\Value\StringValue;
18
19
/**
20
 * Class SchemaDefinitionNode
21
 *
22
 * <code>
23
 *  export interface SchemaDefinitionNode {
24
 *      readonly kind: 'SchemaDefinition';
25
 *      readonly loc?: Location;
26
 *      readonly directives?: ReadonlyArray<DirectiveNode>;
27
 *      readonly operationTypes: ReadonlyArray<OperationTypeDefinitionNode>;
28
 *  }
29
 * </code>
30
 *
31
 * Railt Extras:
32
 *
33
 * <code>
34
 *  export interface RailtSchemaDefinitionNode {
35
 *      readonly name?: IdentifierNode;
36
 *  }
37
 * </code>
38
 */
39
class SchemaDefinitionNode extends TypeSystemDefinitionNode
40
{
41
    /**
42
     * @var StringValue|null
43
     */
44
    public ?StringValue $description = null;
45
46
    /**
47
     * @var DirectiveNode[]
48
     */
49
    public array $directives = [];
50
51
    /**
52
     * @var OperationTypeDefinitionNode[]
53
     */
54
    public array $operationTypes = [];
55
56
    /**
57
     * @param array|Node[] $children
58
     * @return static
59
     */
60
    public static function create(array $children): self
61
    {
62
        $schema = new static();
63
64
        foreach ($children as $child) {
65
            switch (true) {
66
                case $child instanceof Description:
67
                    $schema->description = $child->value;
68
                    break;
69
70
                case $child instanceof DirectiveNode:
71
                    $schema->directives[] = $child;
72
                    break;
73
74
                case $child instanceof OperationTypeDefinitionNode:
75
                    $schema->operationTypes[] = $child;
76
                    break;
77
            }
78
        }
79
80
        return $schema;
81
    }
82
}
83