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

api-platform / core / 10944614151

19 Sep 2024 03:58PM UTC coverage: 7.518% (+0.2%) from 7.34%
10944614151

push

github

soyuka
chore: fix thanks url

12416 of 165161 relevant lines covered (7.52%)

23.02 hits per line

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

94.44
/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
    public function __construct(private ?array $distinctFormats)
25
    {
26
    }
1,854✔
27

28
    public function create(string $className, string $format = 'json', ?string $inputOrOutputClass = null, ?Operation $operation = null, array $serializerContext = []): string
29
    {
30
        if ($operation) {
39✔
31
            $prefix = $operation->getShortName();
39✔
32
        }
33

34
        if (!isset($prefix)) {
39✔
35
            $prefix = (new \ReflectionClass($className))->getShortName();
39✔
36
        }
37

38
        if (null !== $inputOrOutputClass && $className !== $inputOrOutputClass) {
39✔
39
            $parts = explode('\\', $inputOrOutputClass);
39✔
40
            $shortName = end($parts);
39✔
41
            $prefix .= '.'.$shortName;
39✔
42
        }
43

44
        if ('json' !== $format && ($this->distinctFormats[$format] ?? false)) {
39✔
45
            // JSON is the default, and so isn't included in the definition name
46
            $prefix .= '.'.$format;
39✔
47
        }
48

49
        $definitionName = $serializerContext[SchemaFactory::OPENAPI_DEFINITION_NAME] ?? null;
39✔
50
        if ($definitionName) {
39✔
51
            $name = \sprintf('%s-%s', $prefix, $definitionName);
×
52
        } else {
53
            $groups = (array) ($serializerContext[AbstractNormalizer::GROUPS] ?? []);
39✔
54
            $name = $groups ? \sprintf('%s-%s', $prefix, implode('_', $groups)) : $prefix;
39✔
55
        }
56

57
        return $this->encodeDefinitionName($name);
39✔
58
    }
59

60
    private function encodeDefinitionName(string $name): string
61
    {
62
        return preg_replace('/[^a-zA-Z0-9.\-_]/', '.', $name);
39✔
63
    }
64
}
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