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

ClassMethodsHydrator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 14
c 1
b 0
f 0
dl 0
loc 38
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 9 2
A hydrate() 0 13 2
A get() 0 5 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Orm\Entity;
5
6
use Sirius\Orm\Contract\EntityInterface;
7
use Sirius\Orm\Contract\LazyLoader;
8
use Sirius\Orm\Helpers\Arr;
9
use Sirius\Orm\Helpers\Str;
10
11
class ClassMethodsHydrator extends AbstractHydrator
12
{
13
    /**
14
     * @param array $attributes
15
     *
16
     * @return mixed|ClassMethodsEntity
17
     */
18
    public function hydrate(array $attributes = [])
19
    {
20
        $attributes = Arr::renameKeys($attributes, $this->getMapperConfig()->getColumnAttributeMap());
21
        if ($this->castingManager) {
22
            $attributes = $this->castingManager
23
                ->castArray($attributes, $this->getMapperConfig()->getCasts());
24
        }
25
26
        $attributes = $this->compileRelations($attributes);
27
28
        $class = $this->getMapperConfig()->getEntityClass() ?? ClassMethodsEntity::class;
29
30
        return new $class($attributes);
31
    }
32
33
    public function get(EntityInterface $entity, $attribute)
34
    {
35
        $method = Str::methodName($attribute, 'get');
36
37
        return $entity->{$method}();
38
    }
39
40
    public function set(EntityInterface $entity, $attribute, $value)
41
    {
42
        if ($value instanceof LazyLoader) {
43
            return $entity->setLazy($attribute, $value);
44
        }
45
46
        $method = Str::methodName($attribute, 'set');
47
48
        return $entity->{$method}($value);
49
    }
50
}
51