-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathEnumClassMutator.php
More file actions
54 lines (45 loc) · 1.57 KB
/
EnumClassMutator.php
File metadata and controls
54 lines (45 loc) · 1.57 KB
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
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\SchemaGenerator\Schema\ClassMutator;
use ApiPlatform\SchemaGenerator\ClassMutator\EnumClassMutator as BaseEnumClassMutator;
use ApiPlatform\SchemaGenerator\Model\Class_;
use ApiPlatform\SchemaGenerator\PhpTypeConverterInterface;
use ApiPlatform\SchemaGenerator\Schema\Model\Constant as SchemaConstant;
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfGraph;
final class EnumClassMutator extends BaseEnumClassMutator
{
/**
* @var RdfGraph[]
*/
private array $graphs;
/**
* @param RdfGraph[] $graphs
*/
public function __construct(PhpTypeConverterInterface $phpTypeConverter, array $graphs, string $desiredNamespace)
{
parent::__construct($phpTypeConverter, $desiredNamespace);
$this->graphs = $graphs;
}
protected function addEnumConstants(Class_ $class): void
{
if (!$class->rdfType()) {
return;
}
foreach ($this->graphs as $graph) {
foreach ($graph->allOfType($class->rdfType()) as $instance) {
$class->addConstant($instance->localName(), new SchemaConstant(
$this->phpTypeConverter->escapeIdentifier(strtoupper(substr(preg_replace('/([A-Z])/', '_$1', $instance->localName()), 1))),
$instance
));
}
}
}
}