Passed
Push — master ( 71375f...32edb5 )
by Alexander
07:08
created

MongoHydrator::hydrate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php declare(strict_types=1);
2
3
namespace Indigerd\Repository\Hydrator;
4
5
use Indigerd\Hydrator\Hydrator;
6
7
class MongoHydrator extends Hydrator
8
{
9
    public function hydrate($target, array $data): object
10
    {
11
        if (isset($data['_id'])) {
12
            $data['id'] = (string)$data['_id'];
13
            unset($data['_id']);
14
        }
15
        return parent::hydrate($target, $data);
16
    }
17
18
    public function extract(object $object, array $fields = []): array
19
    {
20
        $result = parent::extract($object, $fields);
21
        if (isset($result['id'])) {
22
            $result['_id'] = $result['id'];
23
            unset($result['id']);
24
        }
25
        return $result;
26
    }
27
}
28