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

ErrorsContainer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 16.66%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 54
ccs 2
cts 12
cp 0.1666
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addError() 0 4 1
A hasErrors() 0 4 1
A getErrors() 0 4 1
A errorsToArray() 0 11 2
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
}