1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Date: 01.12.15 |
4
|
|
|
* |
5
|
|
|
* @author Portey Vasil <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Youshido\GraphQL\Validator\ErrorContainer; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use Youshido\GraphQL\Exception\Interfaces\DatableExceptionInterface; |
12
|
|
|
use Youshido\GraphQL\Exception\Interfaces\LocationableExceptionInterface; |
13
|
|
|
|
14
|
|
|
trait ErrorContainerTrait |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** @var \Exception[] */ |
18
|
|
|
protected $errors = []; |
19
|
|
|
|
20
|
|
|
/** @var bool */ |
21
|
|
|
protected $catchExceptions = true; |
22
|
|
|
|
23
|
42 |
|
public function addError(\Exception $exception) |
24
|
|
|
{ |
25
|
42 |
|
if (!$this->catchExceptions && !$exception instanceof DatableExceptionInterface && !$exception instanceof LocationableExceptionInterface) { |
26
|
|
|
throw $exception; |
27
|
|
|
} |
28
|
42 |
|
$this->errors[] = $exception; |
29
|
|
|
|
30
|
42 |
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
98 |
|
public function hasErrors() |
34
|
|
|
{ |
35
|
98 |
|
return !empty($this->errors); |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
public function getErrors() |
39
|
|
|
{ |
40
|
1 |
|
return $this->errors; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function setCatchExceptions($catchExceptions) |
44
|
|
|
{ |
45
|
|
|
$this->catchExceptions = $catchExceptions; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
public function mergeErrors(ErrorContainerInterface $errorContainer) |
49
|
|
|
{ |
50
|
1 |
|
if ($errorContainer->hasErrors()) { |
51
|
1 |
|
foreach ($errorContainer->getErrors() as $error) { |
52
|
1 |
|
$this->addError($error); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
42 |
|
public function getErrorsArray($inGraphQLStyle = true) |
60
|
|
|
{ |
61
|
42 |
|
$errors = []; |
62
|
|
|
|
63
|
42 |
|
foreach ($this->errors as $error) { |
64
|
42 |
|
if ($inGraphQLStyle) { |
65
|
21 |
|
if ($error instanceof DatableExceptionInterface) { |
66
|
1 |
|
$errors[] = array_merge( |
67
|
1 |
|
['message' => $error->getMessage()], |
68
|
1 |
|
$error->getData() ?: [], |
69
|
1 |
|
$error->getCode() ? ['code' => $error->getCode()] : [] |
70
|
|
|
); |
71
|
21 |
|
} elseif ($error instanceof LocationableExceptionInterface) { |
72
|
18 |
|
$errors[] = array_merge( |
73
|
18 |
|
['message' => $error->getMessage()], |
74
|
18 |
|
$error->getLocation() ? ['locations' => [$error->getLocation()->toArray()]] : [], |
75
|
18 |
|
$error->getCode() ? ['code' => $error->getCode()] : [] |
76
|
|
|
); |
77
|
|
|
} else { |
78
|
4 |
|
$errors[] = array_merge( |
79
|
4 |
|
['message' => $error->getMessage()], |
80
|
21 |
|
$error->getCode() ? ['code' => $error->getCode()] : [] |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
} else { |
84
|
42 |
|
$errors[] = $error->getMessage(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
42 |
|
return $errors; |
89
|
|
|
} |
90
|
|
|
|
91
|
123 |
|
public function clearErrors() |
92
|
|
|
{ |
93
|
123 |
|
$this->errors = []; |
94
|
|
|
|
95
|
123 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} |