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

ClassMethodsHydrator::getPk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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