|
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
|
|
|
|