-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
AbstractCollectionEnumType.php
57 lines (46 loc) · 1.28 KB
/
AbstractCollectionEnumType.php
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
<?php
declare(strict_types=1);
/*
* This file is part of the "elao/enum" package.
*
* Copyright (C) Elao
*
* @author Elao <contact@elao.com>
*/
namespace Elao\Enum\Bridge\Doctrine\ODM\Types;
use Doctrine\ODM\MongoDB\Types\ClosureToPHP;
use Doctrine\ODM\MongoDB\Types\CollectionType;
/**
* @template TEnum of \BackedEnum
*/
abstract class AbstractCollectionEnumType extends CollectionType
{
use ClosureToPHP;
public function convertToDatabaseValue($value): ?array
{
if (\is_array($value)) {
return array_unique(array_values(array_map(static function ($value) {
return $value instanceof \BackedEnum ? $value->value : $value;
}, $value)));
}
return parent::convertToDatabaseValue($value);
}
/**
* {@inheritdoc}
*
* @phpstan-return array<TEnum>|null
*/
public function convertToPHPValue($value): ?array
{
$values = parent::convertToPHPValue($value);
if (\is_array($values)) {
$enumClass = $this->getEnumClass();
$values = array_map([$enumClass, 'from'], array_unique(array_values($values)));
}
return $values;
}
/**
* @return class-string<TEnum>
*/
abstract protected function getEnumClass(): string;
}