Completed
Pull Request — master (#15)
by Christoffer
06:08 queued 03:44
created

ExecutionResult   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrors() 0 3 1
A addError() 0 4 1
A toJSON() 0 3 1
A __construct() 0 4 1
A toArray() 0 5 1
1
<?php
2
3
namespace Digia\GraphQL\Execution;
4
5
use function Digia\GraphQL\Util\jsonEncode;
6
use Digia\GraphQL\Util\SerializationInterface;
7
use Digia\GraphQL\Error\GraphQLError;
8
9
class ExecutionResult implements SerializationInterface
10
{
11
12
    /**
13
     * @var GraphQLError[]
14
     */
15
    protected $errors;
16
17
    /**
18
     * @var mixed[]
19
     */
20
    protected $data;
21
22
    /**
23
     * ExecutionResult constructor.
24
     *
25
     * @param mixed[]        $data
26
     * @param GraphQLError[] $errors
27
     */
28
    public function __construct(?array $data, ?array $errors)
29
    {
30
        $this->errors = $errors;
31
        $this->data   = $data;
32
    }
33
34
    /**
35
     * @return array|GraphQLError[]
36
     */
37
    public function getErrors(): array
38
    {
39
        return $this->errors;
40
    }
41
42
    /**
43
     * @param GraphQLError $error
44
     * @return ExecutionResult
45
     */
46
    public function addError(GraphQLError $error): ExecutionResult
47
    {
48
        $this->errors[] = $error;
49
        return $this;
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function toArray(): array
56
    {
57
        return [
58
            'data'   => $this->data,
59
            'errors' => $this->errors,
60
        ];
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function toJSON(): string
67
    {
68
        return jsonEncode($this->toArray());
69
    }
70
}
71