1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Explicit Architecture POC, |
7
|
|
|
* which is created on top of the Symfony Demo application. |
8
|
|
|
* |
9
|
|
|
* (c) Herberto Graça <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Acme\App\Infrastructure\Persistence\Doctrine\Type; |
16
|
|
|
|
17
|
|
|
use Acme\PhpExtension\Enum\AbstractEnum; |
18
|
|
|
use Acme\PhpExtension\ScalarObjectInterface; |
19
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
20
|
|
|
use Doctrine\DBAL\Platforms\SqlitePlatform; |
21
|
|
|
use Doctrine\DBAL\Types\Type; |
22
|
|
|
|
23
|
|
|
abstract class AbstractEnumType extends Type |
24
|
|
|
{ |
25
|
|
|
use TypeTrait; |
26
|
|
|
|
27
|
|
|
public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string |
28
|
|
|
{ |
29
|
|
|
$allowedValues = "'" . implode("', '", $this->getValues()) . "'"; |
30
|
|
|
|
31
|
|
|
if ($platform instanceof SqlitePlatform) { |
32
|
|
|
$name = $fieldDeclaration['name']; |
33
|
|
|
|
34
|
|
|
return "TEXT CHECK( $name IN ($allowedValues) )"; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return "ENUM($allowedValues)"; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string |
41
|
|
|
{ |
42
|
|
|
if ($value === null && $this->allowsNullValues()) { |
43
|
|
|
return null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($value instanceof ScalarObjectInterface) { |
|
|
|
|
47
|
|
|
$value = $value->toScalar(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (!\in_array($value, $this->getValues(), true)) { |
51
|
|
|
$type = $this->getName(); |
52
|
|
|
throw new \InvalidArgumentException("Invalid value '$value' for type '$type'."); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $value; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function createSpecificObject($value) |
59
|
|
|
{ |
60
|
|
|
/** @var string|AbstractEnum $class */ |
61
|
|
|
$class = $this->getMappedClass(); |
62
|
|
|
|
63
|
|
|
return $class::get($value); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function getValues(): array |
67
|
|
|
{ |
68
|
|
|
/** @var string|AbstractEnum $class */ |
69
|
|
|
$class = $this->getMappedClass(); |
70
|
|
|
|
71
|
|
|
return $class::getValidOptions(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.