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

DoclerLabs / api-client-generator / 9254068657

27 May 2024 11:30AM UTC coverage: 86.981% (-1.4%) from 88.428%
9254068657

push

github

web-flow
Merge pull request #112 from DoclerLabs/php81

php 8.1 features

106 of 172 new or added lines in 20 files covered. (61.63%)

4 existing lines in 2 files now uncovered.

2913 of 3349 relevant lines covered (86.98%)

4.92 hits per line

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

5.88
/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
    {
NEW
19
        $sanitized = preg_replace('[^A-Z0-9_]', '', strtoupper(str_replace([' ', '-', '/', '.'], '_', $value)));
×
20

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

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

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

NEW
34
        $compositeFields = $specification->getCompositeFields()->getUniqueByPhpClassName();
×
NEW
35
        foreach ($compositeFields as $field) {
×
36
            if (
NEW
37
                $field->isObject()
×
NEW
38
                && !$field->isFreeFormObject()
×
39
            ) {
NEW
40
                foreach ($field->getObjectProperties() as $propertyField) {
×
NEW
41
                    if ($propertyField->isEnum()) {
×
NEW
42
                        $this->generateEnum($propertyField, $fileRegistry);
×
43
                    }
44
                }
45
            }
46
        }
NEW
47
        foreach ($specification->getOperations() as $operation) {
×
NEW
48
            foreach ($operation->request->fields as $field) {
×
NEW
49
                if (!empty($field->getEnumValues())) {
×
NEW
50
                    $this->generateEnum($field, $fileRegistry);
×
51
                }
52
            }
53
        }
NEW
54
    }
×
55

56
    private function generateEnum(Field $root, PhpFileCollection $fileRegistry): void
57
    {
NEW
58
        $classBuilder = $this
×
NEW
59
            ->builder
×
NEW
60
            ->enum($root->getPhpClassName())
×
NEW
61
            ->setScalarType($root->getType()->toPhpType())
×
NEW
62
            ->addStmts($this->generateEnumConsts($root));
×
63

NEW
64
        $this->registerFile($fileRegistry, $classBuilder, self::SUBDIRECTORY, self::NAMESPACE_SUBPATH);
×
NEW
65
    }
×
66

67
    private function generateEnumConsts(Field $root): array
68
    {
NEW
69
        if ($root->getEnumValues() === null) {
×
NEW
70
            return [];
×
71
        }
72

NEW
73
        $statements = [];
×
NEW
74
        foreach ($root->getEnumValues() as $value) {
×
NEW
75
            $statements[] = $this
×
NEW
76
                ->builder
×
NEW
77
                ->enumCase(self::getCaseName((string)$value))
×
NEW
78
                ->setValue($value);
×
79
        }
80

NEW
81
        return $statements;
×
82
    }
83
}
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

© 2025 Coveralls, Inc