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

ErrorCollectionParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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