summaryrefslogtreecommitdiff
path: root/vendor/doctrine/orm/src/Mapping/DiscriminatorColumnMapping.php
blob: 4ccb71c4b36b5025a3764b022132c6614e40b7eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php

declare(strict_types=1);

namespace Doctrine\ORM\Mapping;

use ArrayAccess;
use BackedEnum;
use Exception;

use function in_array;
use function property_exists;

/** @template-implements ArrayAccess<string, mixed> */
final class DiscriminatorColumnMapping implements ArrayAccess
{
    use ArrayAccessImplementation;

    /** The database length of the column. Optional. Default value taken from the type. */
    public int|null $length = null;

    public string|null $columnDefinition = null;

    /** @var class-string<BackedEnum>|null */
    public string|null $enumType = null;

    /** @var array<string, mixed> */
    public array $options = [];

    public function __construct(
        public string $type,
        public string $fieldName,
        public string $name,
    ) {
    }

    /**
     * @psalm-param array{
     *     type: string,
     *     fieldName: string,
     *     name: string,
     *     length?: int|null,
     *     columnDefinition?: string|null,
     *     enumType?: class-string<BackedEnum>|null,
     *     options?: array<string, mixed>|null,
     * } $mappingArray
     */
    public static function fromMappingArray(array $mappingArray): self
    {
        $mapping = new self(
            $mappingArray['type'],
            $mappingArray['fieldName'],
            $mappingArray['name'],
        );
        foreach ($mappingArray as $key => $value) {
            if (in_array($key, ['type', 'fieldName', 'name'])) {
                continue;
            }

            if (property_exists($mapping, $key)) {
                $mapping->$key = $value ?? $mapping->$key;
            } else {
                throw new Exception('Unknown property ' . $key . ' on class ' . static::class);
            }
        }

        return $mapping;
    }

    /** @return list<string> */
    public function __sleep(): array
    {
        $serialized = ['type', 'fieldName', 'name'];

        foreach (['length', 'columnDefinition', 'enumType', 'options'] as $stringOrArrayKey) {
            if ($this->$stringOrArrayKey !== null) {
                $serialized[] = $stringOrArrayKey;
            }
        }

        return $serialized;
    }
}