Passed
Pull Request — 2.2 (#2051)
by
unknown
03:08
created

CompositeIdentifierParser   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 26
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 15 3
A __construct() 0 2 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\Identifier;
15
16
/**
17
 * Normalizes a composite identifer.
18
 *
19
 * @author Antoine Bluchet <[email protected]>
20
 */
21
final class CompositeIdentifierParser
22
{
23
    private function __construct()
24
    {
25
    }
26
27
    /*
28
     * Normalize takes a string and gives back an array of identifiers.
29
     *
30
     * For example: foo=0;bar=2 returns ['foo' => 0, 'bar' => 2].
31
     */
32
    public static function parse(string $identifier): array
33
    {
34
        $matches = [];
35
        $identifiers = [];
36
        $num = preg_match_all('/(\w+)=(?<=\w=)(.+?)(?=;\w+=)|(\w+)=([^;]+);?$/', $identifier, $matches, PREG_SET_ORDER);
37
38
        foreach ($matches as $i => $match) {
39
            if ($i === $num - 1) {
40
                $identifiers[$match[3]] = $match[4];
41
                continue;
42
            }
43
            $identifiers[$match[1]] = $match[2];
44
        }
45
46
        return $identifiers;
47
    }
48
}
49