Completed
Pull Request — master (#14)
by Pavel
03:40
created

IdentifierFixer::fixScalarId()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
ccs 7
cts 8
cp 0.875
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
crap 3.0175
1
<?php
2
3
namespace Bankiru\Api\Doctrine\Utility;
4
5
use Bankiru\Api\Doctrine\Exception\MappingException;
6
use Bankiru\Api\Doctrine\Mapping\ApiMetadata;
7
8
final class IdentifierFixer
9
{
10
    /**
11
     * @param array|mixed $id
12
     * @param ApiMetadata $metadata
13
     *
14
     * @return array
15
     * @throws MappingException
16
     */
17 11
    public static function fixScalarId($id, ApiMetadata $metadata)
18
    {
19 11
        if (is_array($id)) {
20 5
            return $id;
21
        }
22
23 10
        $id = (array)$id;
24
25 10
        $identifiers = $metadata->getIdentifierFieldNames();
26 10
        if (count($id) !== count($identifiers)) {
27
            throw MappingException::invalidIdentifierStructure();
28
        }
29
30 10
        return array_combine($identifiers, (array)$id);
31
    }
32
}
33