Issues (590)

src/Mapper/VersionableMapper.php (1 issue)

1
<?php
2
3
namespace Bdf\Prime\Mapper;
4
5
use Bdf\Prime\Behaviors\Versionable;
6
use Bdf\Prime\Mapper\Builder\FieldBuilder;
7
8
/**
9
 * Class VersionableMapper
10
 *
11
 * Mapper utilisé par le Behavior Versionable
12
 *
13
 * @package Bdf\Prime\Mapper
14
 */
15
abstract class VersionableMapper extends Mapper
16
{
17
    /**
18
     * Get versioned class name
19
     *
20
     * @return 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...
21
     */
22
    abstract public function getVersionedClass(): string;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 2
    public function getEntityClass(): string
28
    {
29 2
        return $this->getVersionedClass();
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 1
    public function schema(): array
36
    {
37 1
        $entitySchema = $this->getVersionedMapper()->schema();
38
39 1
        $entitySchema['table'] .= '_version';
40
41 1
        return $entitySchema;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 1
    public function buildFields(FieldBuilder $builder): void
48
    {
49 1
        $builder->fill($this->getVersionedMapper()->fields());
50
51 1
        foreach ($builder as $name => $definition) {
52 1
            if (isset($definition['primary']) || ($name === Versionable::COLUMN_NAME)) {
53 1
                $builder->field($name)->primary();
54
            }
55
        }
56
    }
57
58
    /**
59
     * Get versioned mapper
60
     *
61
     * @return Mapper
62
     */
63 1
    private function getVersionedMapper(): Mapper
64
    {
65 1
        return $this->serviceLocator->repository($this->getVersionedClass())->mapper();
66
    }
67
}
68