Issues (188)

src/Mapper/Mapper.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Mapper;
6
7
use Cycle\ORM\Mapper\Traits\SingleTableTrait;
8
use Cycle\ORM\ORMInterface;
9
use Cycle\ORM\Mapper\Proxy\ProxyEntityFactory;
10
use Cycle\ORM\SchemaInterface;
11
12
/**
13
 * Provide the ability to carry data over the specific class instances using proxy classes.
14
 *
15
 * Supports table inheritance using hidden entity field.
16
 */
17
class Mapper extends DatabaseMapper
18
{
19
    use SingleTableTrait;
20
21
    /** @var class-string */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
22
    protected string $entity;
23
24
    protected array $children = [];
25
26 6082
    public function __construct(
27
        ORMInterface $orm,
28
        protected ProxyEntityFactory $entityFactory,
29
        string $role,
30
    ) {
31 6082
        parent::__construct($orm, $role);
32
33 6082
        $this->schema = $orm->getSchema();
34 6082
        $this->entity = $this->schema->define($role, SchemaInterface::ENTITY);
35 6082
        $this->children = $this->schema->define($role, SchemaInterface::CHILDREN) ?? [];
36 6082
        $this->discriminator = $this->schema->define($role, SchemaInterface::DISCRIMINATOR) ?? $this->discriminator;
37
    }
38
39 4436
    public function init(array $data, ?string $role = null): object
40
    {
41 4436
        $class = $this->resolveClass($data, $role);
42 4428
        return $this->entityFactory->create($this->relationMap, $class);
43
    }
44
45 4804
    public function hydrate(object $entity, array $data): object
46
    {
47 4804
        $this->entityFactory->upgrade($this->relationMap, $entity, $data);
48 4804
        return $entity;
49
    }
50
51 4584
    public function extract(object $entity): array
52
    {
53 4584
        return $this->entityFactory->extractData($this->relationMap, $entity)
54 4584
            + $this->entityFactory->extractRelations($this->relationMap, $entity);
55
    }
56
57 2668
    public function fetchFields(object $entity): array
58
    {
59 2668
        $values = \array_intersect_key(
60 2668
            $this->entityFactory->extractData($this->relationMap, $entity),
61 2668
            $this->columns + $this->parentColumns,
62
        );
63 2668
        return $values + $this->getDiscriminatorValues($entity);
64
    }
65
66 1890
    public function fetchRelations(object $entity): array
67
    {
68 1890
        return $this->entityFactory->extractRelations($this->relationMap, $entity);
69
    }
70
}
71