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

api-platform / core / 15255731762

26 May 2025 01:55PM UTC coverage: 0.0% (-26.5%) from 26.526%
15255731762

Pull #7176

github

web-flow
Merge 66f6cf4d2 into 79edced67
Pull Request #7176: Merge 4.1

0 of 387 new or added lines in 28 files covered. (0.0%)

11394 existing lines in 372 files now uncovered.

0 of 51334 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 array $prefixCache = [];
26

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

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

UNCOV
40
        if (!isset($prefix)) {
×
UNCOV
41
            $prefix = $this->createPrefixFromClass($className);
×
42
        }
43

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

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

UNCOV
53
        if ('json' !== $format && $v) {
×
54
            // JSON is the default, and so isn't included in the definition name
UNCOV
55
            $prefix .= self::GLUE.$format;
×
56
        }
57

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

UNCOV
66
        return $this->encodeDefinitionName($name);
×
67
    }
68

69
    private function encodeDefinitionName(string $name): string
70
    {
UNCOV
71
        return preg_replace('/[^a-zA-Z0-9.\-_]/', '.', $name);
×
72
    }
73

74
    private function createPrefixFromClass(string $fullyQualifiedClassName, int $namespaceParts = 1): string
75
    {
UNCOV
76
        $parts = explode('\\', $fullyQualifiedClassName);
×
UNCOV
77
        $name = implode(self::GLUE, \array_slice($parts, -$namespaceParts));
×
78

UNCOV
79
        if (!isset($this->prefixCache[$name])) {
×
UNCOV
80
            $this->prefixCache[$name] = $fullyQualifiedClassName;
×
81

UNCOV
82
            return $name;
×
83
        }
84

UNCOV
85
        if ($this->prefixCache[$name] !== $fullyQualifiedClassName) {
×
86
            $name = $this->createPrefixFromClass($fullyQualifiedClassName, ++$namespaceParts);
×
87
        }
88

UNCOV
89
        return $name;
×
90
    }
91
}
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