ErrorsContainer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

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