Completed
Pull Request — master (#15)
by Christoffer
06:33 queued 01:55
created

ExecutionResult::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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