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

api-platform / core / 19337721455

13 Nov 2025 04:02PM UTC coverage: 0.0% (-24.6%) from 24.631%
19337721455

push

github

soyuka
Merge 4.1

0 of 56854 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/src/JsonSchema/DefinitionNameFactory.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\JsonSchema;
15

16
use ApiPlatform\Metadata\Operation;
17
use ApiPlatform\Metadata\Util\ResourceClassInfoTrait;
18
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
19

20
final class DefinitionNameFactory implements DefinitionNameFactoryInterface
21
{
22
    use ResourceClassInfoTrait;
23

24
    private const GLUE = '.';
25
    private const JSON_MERGE_PATCH_SCHEMA_POSTFIX = 'jsonMergePatch';
26

27
    private array $prefixCache = [];
28

29
    public function __construct(private ?array $distinctFormats = null)
30
    {
31
        if ($distinctFormats) {
×
32
            trigger_deprecation('api-platform/json-schema', '4.2', 'The distinctFormats argument is deprecated and will be removed in 5.0.');
×
33
        }
34
    }
35

36
    public function create(string $className, string $format = 'json', ?string $inputOrOutputClass = null, ?Operation $operation = null, array $serializerContext = []): string
37
    {
38
        if ($operation) {
×
39
            $prefix = $operation->getShortName();
×
40
        }
41

42
        if (!isset($prefix)) {
×
43
            $prefix = $this->createPrefixFromClass($className);
×
44
        }
45

46
        if (null !== $inputOrOutputClass && $className !== $inputOrOutputClass) {
×
47
            $parts = explode('\\', $inputOrOutputClass);
×
48
            $shortName = end($parts);
×
49
            $prefix .= self::GLUE.$shortName;
×
50
        }
51

52
        // TODO: remove in 5.0
53
        $v = $this->distinctFormats ? ($this->distinctFormats[$format] ?? false) : true;
×
54

55
        if (!\in_array($format, ['json', 'merge-patch+json'], true) && $v) {
×
56
            // JSON is the default, and so isn't included in the definition name
57
            // JSON merge patch is postfixed at the end
58
            $prefix .= self::GLUE.$format;
×
59
        }
60

61
        $definitionName = $serializerContext[SchemaFactory::OPENAPI_DEFINITION_NAME] ?? null;
×
62
        if (null !== $definitionName) {
×
63
            $name = \sprintf('%s%s', $prefix, $definitionName ? '-'.$definitionName : $definitionName);
×
64
        } else {
65
            $groups = (array) ($serializerContext[AbstractNormalizer::GROUPS] ?? []);
×
66
            $name = $groups ? \sprintf('%s-%s', $prefix, implode('_', $groups)) : $prefix;
×
67
        }
68

69
        if (false === ($serializerContext['gen_id'] ?? true)) {
×
70
            $name .= '_noid';
×
71
        }
72

73
        if ('merge-patch+json' === $format) {
×
74
            $name .= self::GLUE.self::JSON_MERGE_PATCH_SCHEMA_POSTFIX;
×
75
        }
76

77
        return $this->encodeDefinitionName($name);
×
78
    }
79

80
    private function encodeDefinitionName(string $name): string
81
    {
82
        return preg_replace('/[^a-zA-Z0-9.\-_]/', '.', $name);
×
83
    }
84

85
    private function createPrefixFromClass(string $fullyQualifiedClassName, int $namespaceParts = 1): string
86
    {
87
        $parts = explode('\\', $fullyQualifiedClassName);
×
88
        $name = implode(self::GLUE, \array_slice($parts, -$namespaceParts));
×
89

90
        if (!isset($this->prefixCache[$name])) {
×
91
            $this->prefixCache[$name] = $fullyQualifiedClassName;
×
92

93
            return $name;
×
94
        }
95

96
        if ($this->prefixCache[$name] !== $fullyQualifiedClassName) {
×
97
            $name = $this->createPrefixFromClass($fullyQualifiedClassName, ++$namespaceParts);
×
98
        }
99

100
        return $name;
×
101
    }
102
}
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