Passed
Push — dev_2x ( fedccd...fb9ebe )
by Adrian
02:16
created

AbstractHydrator::hydrateToArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
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\Relation\ToManyInterface;
10
use Sirius\Orm\Contract\Relation\ToOneInterface;
11
use Sirius\Orm\Helpers\Arr;
12
use Sirius\Orm\Mapper;
13
use Sirius\Orm\MapperConfig;
14
15
abstract class AbstractHydrator implements HydratorInterface
16
{
17
    /**
18
     * @var CastingManager
19
     */
20
    protected $castingManager;
21
22
    /**
23
     * @var Mapper
24
     */
25
    protected $mapper;
26
27
    public function __construct(CastingManager $castingManager)
28
    {
29
        $this->castingManager = $castingManager;
30
    }
31
32
    public function setMapper(Mapper $mapper)
33
    {
34
        $this->mapper = $mapper;
35
    }
36
37
    /**
38
     * @param array $attributes
39
     *
40
     * @return mixed|GenericEntity
41
     */
42
    abstract public function hydrate(array $attributes = []);
43
44
    /**
45
     * @param EntityInterface $entity
46
     *
47
     * @return array
48
     */
49
    public function extract(EntityInterface $entity)
50
    {
51
        $data = Arr::renameKeys(
52
            $entity->toArray(),
53
            array_flip($this->getMapperConfig()->getColumnAttributeMap())
54
        );
55
        if ($this->castingManager) {
56
            $data = $this->castingManager
57
                ->castArrayForDb($data, $this->getMapperConfig()->getCasts());
58
        }
59
60
        return Arr::only($data, $this->getMapperConfig()->getColumns());
61
    }
62
63
    abstract public function get(EntityInterface $entity, string $attribute);
64
65
    abstract public function set(EntityInterface $entity, string $attribute, $value);
66
67
    public function getPk($entity)
68
    {
69
        return $this->get($entity, $this->getMapperConfig()->getPrimaryKey());
0 ignored issues
show
Bug introduced by
It seems like $this->getMapperConfig()->getPrimaryKey() can also be of type array; however, parameter $attribute of Sirius\Orm\Entity\AbstractHydrator::get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
        return $this->get($entity, /** @scrutinizer ignore-type */ $this->getMapperConfig()->getPrimaryKey());
Loading history...
70
    }
71
72
    public function setPk($entity, $value)
73
    {
74
        return $this->set($entity, $this->getMapperConfig()->getPrimaryKey(), $value);
0 ignored issues
show
Bug introduced by
It seems like $this->getMapperConfig()->getPrimaryKey() can also be of type array; however, parameter $attribute of Sirius\Orm\Entity\AbstractHydrator::set() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
        return $this->set($entity, /** @scrutinizer ignore-type */ $this->getMapperConfig()->getPrimaryKey(), $value);
Loading history...
75
    }
76
77
    public function hydrateToArray(array $attributes= []): array
78
    {
79
        $attributes = Arr::renameKeys($attributes, $this->getMapperConfig()->getColumnAttributeMap());
80
        if ($this->castingManager) {
81
            $attributes = $this->castingManager
82
                ->castArray($attributes, $this->getMapperConfig()->getCasts());
83
        }
84
85
        return $this->compileRelations($attributes);
86
    }
87
88
    protected function compileRelations(array $attributes)
89
    {
90
        foreach ($this->mapper->getRelations() as $name) {
91
            $relation = $this->mapper->getRelation($name);
92
            if ($relation instanceof ToOneInterface
93
                && isset($attributes[$name])
94
                && ! is_object($attributes[$name])) {
95
                $attributes[$name] = $relation->getForeignMapper()->newEntity($attributes[$name]);
96
            } elseif ($relation instanceof ToManyInterface
97
                      && ! $relation instanceof ToOneInterface) {
98
                /**
99
                 * here we need to check against ToOneInterface as well
100
                 * because OneToOne relation extends
101
                 * OneToMany which implements ToOneInterface
102
                 * @todo remove this quirk
103
                 */
104
                if (isset($attributes[$name]) && is_array($attributes[$name])) {
105
                    $attributes[$name] = $relation->getForeignMapper()->newCollection($attributes[$name]);
106
                } else {
107
                    $attributes[$name] = new LazyValue($relation->getForeignMapper()->newCollection([]));
108
                }
109
            }
110
        }
111
112
        return $attributes;
113
    }
114
115
    protected function getMapperConfig(): MapperConfig
116
    {
117
        return $this->mapper->getConfig();
118
    }
119
}
120