StdMapper   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 47
ccs 24
cts 24
cp 1
rs 10
c 1
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 3 1
A hydrate() 0 17 5
A fetchFields() 0 5 1
A fetchRelations() 0 5 1
A extract() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Mapper;
6
7
use Cycle\ORM\Reference\Promise;
8
use Cycle\ORM\Reference\ReferenceInterface;
9
10
/**
11
 * Provide the ability to carry data over the StdClass objects. Does not support single table inheritance.
12
 */
13
final class StdMapper extends DatabaseMapper
14
{
15 352
    public function init(array $data, string $role = null): object
16
    {
17 352
        return new \stdClass();
18
    }
19
20 352
    public function hydrate($entity, array $data): object
21
    {
22 352
        $relations = $this->relationMap->getRelations();
23 352
        foreach ($data as $k => $v) {
24 344
            if ($v instanceof ReferenceInterface && \array_key_exists($k, $relations)) {
25 224
                $relation = $relations[$k];
26 224
                $relation->resolve($v, false);
27
28 224
                $entity->{$k} = $v->hasValue()
29 80
                    ? $relation->collect($v->getValue())
30 184
                    : new Promise($relation, $v);
31 224
                continue;
32
            }
33 336
            $entity->{$k} = $v;
34
        }
35
36 352
        return $entity;
37
    }
38
39 352
    public function extract($entity): array
40
    {
41 352
        return get_object_vars($entity);
42
    }
43
44
    /**
45
     * Get entity columns.
46
     */
47 184
    public function fetchFields(object $entity): array
48
    {
49 184
        return array_intersect_key(
50 184
            $this->extract($entity),
51 184
            $this->columns + $this->parentColumns
52
        );
53
    }
54
55 136
    public function fetchRelations(object $entity): array
56
    {
57 136
        return array_intersect_key(
58 136
            $this->extract($entity),
59 136
            $this->relationMap->getRelations()
60
        );
61
    }
62
}
63