Completed
Push — master ( f979a7...d9e3ed )
by Portey
06:50
created

ErrorContainerTrait::clearErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
crap 1
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\Validator\Exception\DatableResolveException;
12
13
trait ErrorContainerTrait
14
{
15
16
    /** @var \Exception[] */
17
    protected $errors = [];
18
19 2
    public function addError(\Exception $exception)
20
    {
21 2
        $this->errors[] = $exception;
22 2
    }
23
24 27
    public function hasErrors()
25
    {
26 27
        return !empty($this->errors);
27
    }
28
29
    public function getErrors()
30
    {
31
        return $this->errors;
32
    }
33
34 2
    public function getErrorsArray($asObject = true)
35
    {
36 2
        $errors = [];
37
38 2
        foreach ($this->errors as $error) {
39 2
            if ($asObject) {
40 1
                if (is_object($error)) {
41 1
                    if ($error instanceof DatableResolveException) {
42
                        $errors[] = array_merge(
43
                            ['message' => $error->getMessage()],
44
                            $error->getData() ?: [],
45
                            $error->getCode() ? ['code' => $error->getCode()] : []
46
                        );
47
                    } else {
48 1
                        $errors[] = array_merge(
49 1
                            ['message' => $error->getMessage()],
50 1
                            $error->getCode() ? ['code' => $error->getCode()] : []
51 1
                        );
52
                    }
53 1
                } else {
54
                    $errors[] = ['message' => $error];
55
                }
56 1
            } else {
57 1
                if (is_object($error)) {
58 1
                    $errors[] = $error->getMessage();
59 1
                } else {
60
                    $errors[] = $error;
61
                }
62
            }
63 2
        }
64
65 2
        return $errors;
66
    }
67
68 18
    public function clearErrors()
69
    {
70 18
        $this->errors = [];
71
72 18
        return $this;
73
    }
74
75
}