Completed
Push — master ( f73309...4d4b19 )
by Antoine
03:25
created

PathResolver/UnderscoreOperationPathResolver.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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\Core\PathResolver;
15
16
use ApiPlatform\Core\Api\OperationType;
17
use ApiPlatform\Core\Api\OperationTypeDeprecationHelper;
18
use Doctrine\Common\Inflector\Inflector;
19
20
/**
21
 * Generates a path with words separated by underscores.
22
 *
23
 * @author Paul Le Corre <[email protected]>
24
 */
25
final class UnderscoreOperationPathResolver implements OperationPathResolverInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function resolveOperationPath(string $resourceShortName, array $operation, $operationType): string
31
    {
32
        $operationType = OperationTypeDeprecationHelper::getOperationType($operationType);
33
34 View Code Duplication
        if ($operationType === OperationType::SUBRESOURCE && 1 < count($operation['identifiers'])) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
            $path = str_replace('.{_format}', '', $resourceShortName);
36
        } else {
37
            $path = '/'.Inflector::pluralize(Inflector::tableize($resourceShortName));
38
        }
39
40
        if ($operationType === OperationType::ITEM) {
41
            $path .= '/{id}';
42
        }
43
44
        if ($operationType === OperationType::SUBRESOURCE) {
45
            list($key) = end($operation['identifiers']);
46
            $property = true === $operation['collection'] ? Inflector::pluralize(Inflector::tableize($operation['property'])) : Inflector::tableize($operation['property']);
47
            $path .= sprintf('/{%s}/%s', $key, $property);
48
        }
49
50
        $path .= '.{_format}';
51
52
        return $path;
53
    }
54
}
55