• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DoclerLabs / api-client-generator / 16129767876

07 Jul 2025 11:05PM UTC coverage: 86.951% (+0.2%) from 86.755%
16129767876

Pull #125

github

web-flow
Merge 83e9c8242 into 0c611b44a
Pull Request #125: Array of enums support

50 of 53 new or added lines in 6 files covered. (94.34%)

2992 of 3441 relevant lines covered (86.95%)

6.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

5.56
/src/Generator/EnumGenerator.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace DoclerLabs\ApiClientGenerator\Generator;
6

7
use DoclerLabs\ApiClientGenerator\Entity\Field;
8
use DoclerLabs\ApiClientGenerator\Input\Specification;
9
use DoclerLabs\ApiClientGenerator\Output\Php\PhpFileCollection;
10

11
class EnumGenerator extends MutatorAccessorClassGeneratorAbstract
12
{
13
    public const SUBDIRECTORY = 'Schema/';
14

15
    public const NAMESPACE_SUBPATH = '\\Schema';
16

17
    public static function getCaseName(string $value): string
18
    {
19
        $sanitized = preg_replace('[^A-Z0-9_]', '', strtoupper(str_replace([' ', '-', '/', '.'], '_', $value)));
×
20

21
        if (is_numeric($sanitized)) {
×
22
            return 'V_' . $sanitized;
×
23
        }
24

25
        return (string)$sanitized;
×
26
    }
27

28
    public function generate(Specification $specification, PhpFileCollection $fileRegistry): void
29
    {
30
        if (!$this->phpVersion->isEnumSupported()) {
2✔
31
            return;
2✔
32
        }
33

34
        $compositeFields = $specification->getCompositeFields()->getUniqueByPhpClassName();
×
35
        foreach ($compositeFields as $field) {
×
36
            if (
37
                $field->isObject()
×
38
                && !$field->isFreeFormObject()
×
39
            ) {
40
                foreach ($field->getObjectProperties() as $propertyField) {
×
41
                    if ($propertyField->isEnum()) {
×
42
                        $this->generateEnum($propertyField, $fileRegistry);
×
43
                    }
44
                }
45
            }
46

NEW
47
            if ($field->isEnum()) {
×
NEW
48
                $this->generateEnum($field, $fileRegistry);
×
49
            }
50
        }
51
        foreach ($specification->getOperations() as $operation) {
×
52
            foreach ($operation->request->fields as $field) {
×
53
                if (!empty($field->getEnumValues())) {
×
54
                    $this->generateEnum($field, $fileRegistry);
×
55
                }
56
            }
57
        }
58
    }
×
59

60
    private function generateEnum(Field $root, PhpFileCollection $fileRegistry): void
61
    {
62
        $classBuilder = $this
×
63
            ->builder
×
64
            ->enum($root->getPhpClassName())
×
65
            ->setScalarType($root->getType()->toPhpType())
×
66
            ->addStmts($this->generateEnumConsts($root));
×
67

68
        $this->registerFile($fileRegistry, $classBuilder, self::SUBDIRECTORY, self::NAMESPACE_SUBPATH);
×
69
    }
×
70

71
    private function generateEnumConsts(Field $root): array
72
    {
73
        if ($root->getEnumValues() === null) {
×
74
            return [];
×
75
        }
76

77
        $statements = [];
×
78
        foreach ($root->getEnumValues() as $value) {
×
79
            $statements[] = $this
×
80
                ->builder
×
81
                ->enumCase(self::getCaseName((string)$value))
×
82
                ->setValue($value);
×
83
        }
84

85
        return $statements;
×
86
    }
87
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc