Passed
Push — master ( e9bd9f...92cbc5 )
by Michael
03:05
created

ErrorsContainer::errorsToArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 6
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
    public function addError(ErrorObject $error)
28
    {
29
        $this->errors[] = $error;
30
    }
31
32
    /**
33
     * Contains any errors ?
34
     *
35
     * @return bool
36
     */
37 11
    public function hasErrors(): bool
38
    {
39 11
        return count($this->errors) > 0;
40
    }
41
42
    /**
43
     * Get all errors
44
     *
45
     * @return ErrorObject[]
46
     */
47
    public function getErrors(): array
48
    {
49
        return $this->errors;
50
    }
51
52
    /**
53
     * Cast errors to an array
54
     *
55
     * @return array
56
     */
57
    protected function errorsToArray(): array
58
    {
59
        $errors = [];
60
61
        foreach ($this->errors as $error)
62
        {
63
            $errors[] = $error->toArray();
64
        }
65
66
        return $errors;
67
    }
68
}