Completed
Pull Request — master (#128)
by Christoffer
02:13
created

ExecutionResult::toJSON()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Execution;
4
5
use Digia\GraphQL\Error\ExecutionException;
6
use Digia\GraphQL\Util\ArrayToJsonTrait;
7
use Digia\GraphQL\Util\SerializationInterface;
8
9
class ExecutionResult implements SerializationInterface
10
{
11
    use ArrayToJsonTrait;
12
13
    /**
14
     * @var ExecutionException[]
15
     */
16
    protected $errors;
17
18
    /**
19
     * @var mixed[]
20
     */
21
    protected $data;
22
23
    /**
24
     * ExecutionResult constructor.
25
     * @param mixed[]              $data
26
     * @param ExecutionException[] $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|mixed[]
36
     */
37
    public function getData()
38
    {
39
        return $this->data;
40
    }
41
42
    /**
43
     * @return array|ExecutionException[]
44
     */
45
    public function getErrors(): array
46
    {
47
        return $this->errors;
48
    }
49
50
    /**
51
     * @param ExecutionException $error
52
     * @return ExecutionResult
53
     */
54
    public function addError(ExecutionException $error): ExecutionResult
55
    {
56
        $this->errors[] = $error;
57
        return $this;
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function toArray(): array
64
    {
65
        $array = ['data' => $this->data];
66
67
        if (!empty($this->errors)) {
68
            $array['errors'] = $this->errors;
69
        }
70
71
        return $array;
72
    }
73
}
74