Passed
Push — dev_2x ( 1e248d...fedccd )
by Adrian
01:40
created

GenericHydrator::extract()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 2
nc 2
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Orm\Entity;
5
6
use Sirius\Orm\CastingManager;
7
use Sirius\Orm\Contract\EntityInterface;
8
use Sirius\Orm\Contract\HydratorInterface;
9
use Sirius\Orm\Contract\LazyLoader;
10
use Sirius\Orm\Contract\Relation\ToManyInterface;
11
use Sirius\Orm\Contract\Relation\ToOneInterface;
12
use Sirius\Orm\Helpers\Arr;
13
use Sirius\Orm\Mapper;
14
use Sirius\Orm\MapperConfig;
15
16
class GenericHydrator extends AbstractHydrator
17
{
18
19
    /**
20
     * @param array $attributes
21
     *
22
     * @return mixed|GenericEntity
23
     */
24
    public function hydrate(array $attributes = [])
25
    {
26
        $attributes = Arr::renameKeys($attributes, $this->getMapperConfig()->getColumnAttributeMap());
27
        if ($this->castingManager) {
28
            $attributes = $this->castingManager
29
                ->castArray($attributes, $this->getMapperConfig()->getCasts());
30
        }
31
32
        $attributes = $this->compileRelations($attributes);
33
34
        $class = $this->getMapperConfig()->getEntityClass() ?? GenericEntity::class;
35
36
        return new $class($attributes);
37
    }
38
39
    public function get(EntityInterface $entity, string $attribute)
40
    {
41
        return $entity->{$attribute};
42
    }
43
44
    public function set(EntityInterface $entity, string $attribute, $value)
45
    {
46
        if ($value instanceof LazyLoader) {
47
            return $entity->setLazy($attribute, $value);
48
        }
49
50
        return $entity->{$attribute} = $value;
51
    }
52
}
53