Completed
Push — master ( 52b2ea...d5cfd4 )
by Jasper
12s queued 10s
created

ErrorCollectionParser   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 35
ccs 14
cts 14
cp 1
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 15 3
A __construct() 0 3 1
1
<?php
2
3
namespace Swis\JsonApi\Client\Parsers;
4
5
use Swis\JsonApi\Client\ErrorCollection;
6
use Swis\JsonApi\Client\Exceptions\ValidationException;
7
8
/**
9
 * @internal
10
 */
11
class ErrorCollectionParser
12
{
13
    /**
14
     * @var \Swis\JsonApi\Client\Parsers\ErrorParser
15
     */
16
    private $errorParser;
17
18
    /**
19
     * @param \Swis\JsonApi\Client\Parsers\ErrorParser $errorParser
20
     */
21 204
    public function __construct(ErrorParser $errorParser)
22
    {
23 204
        $this->errorParser = $errorParser;
24 204
    }
25
26
    /**
27
     * @param mixed $data
28
     *
29
     * @return \Swis\JsonApi\Client\ErrorCollection
30
     */
31 54
    public function parse($data): ErrorCollection
32
    {
33 54
        if (!is_array($data)) {
34 36
            throw new ValidationException(sprintf('ErrorCollection has to be in an array, "%s" given.', gettype($data)));
35
        }
36 18
        if (count($data) === 0) {
37 6
            throw new ValidationException('ErrorCollection cannot be empty and MUST have at least one Error object.');
38
        }
39
40 12
        return new ErrorCollection(
41 12
            array_map(
42 4
                function ($error) {
43 12
                    return $this->errorParser->parse($error);
44 12
                },
45 10
                $data
46
            )
47
        );
48
    }
49
}
50