Passed
Push — master ( 92cbc5...dc473e )
by Michael
02:54
created

ErrorsContainer::getErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Document\Behaviour;
5
6
use Mikemirten\Component\JsonApi\Document\ErrorObject;
7
8
/**
9
 * Errors-container behaviour
10
 *
11
 * @see http://jsonapi.org/format/#errors
12
 *
13
 * @package Mikemirten\Component\JsonApi\Document\Behaviour
14
 */
15
trait ErrorsContainer
16
{
17
    /**
18
     * @var ErrorObject[];
19
     */
20
    protected $errors = [];
21
22
    /**
23
     * Add error
24
     *
25
     * @param ErrorObject $error
26
     */
27 8
    public function addError(ErrorObject $error)
28
    {
29 8
        $this->errors[] = $error;
30 8
    }
31
32
    /**
33
     * Contains any errors ?
34
     *
35
     * @return bool
36
     */
37 19
    public function hasErrors(): bool
38
    {
39 19
        return count($this->errors) > 0;
40
    }
41
42
    /**
43
     * Get all errors
44
     *
45
     * @return ErrorObject[]
46
     */
47 4
    public function getErrors(): array
48
    {
49 4
        return $this->errors;
50
    }
51
52
    /**
53
     * Cast errors to an array
54
     *
55
     * @return array
56
     */
57 4
    protected function errorsToArray(): array
58
    {
59 4
        $errors = [];
60
61 4
        foreach ($this->errors as $error)
62
        {
63 4
            $errors[] = $error->toArray();
64
        }
65
66 4
        return $errors;
67
    }
68
}