Completed
Push — master ( 131c91...75b368 )
by Kévin
03:30
created

DashPathSegmentNameGenerator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 17
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSegmentName() 0 6 2
A dashize() 0 4 1
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\Operation;
15
16
use Doctrine\Common\Util\Inflector;
17
18
/**
19
 * Generate a path name with a dash separator according to a string and whether it's a collection or not.
20
 *
21
 * @author Antoine Bluchet <[email protected]>
22
 */
23
final class DashPathSegmentNameGenerator implements PathSegmentNameGeneratorInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function getSegmentName(string $name, bool $collection = true): string
29
    {
30
        $name = $this->dashize($name);
31
32
        return $collection ? Inflector::pluralize($name) : $name;
33
    }
34
35
    private function dashize(string $string): string
36
    {
37
        return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '-$1', $string));
38
    }
39
}
40