Passed
Push — master ( dd03d9...cef246 )
by Kévin
04:20 queued 30s
created

CompositeIdentifierParser   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 22
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A parse() 0 15 3
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
    /*
24
     * Normalize takes a string and gives back an array of identifiers.
25
     *
26
     * For example: foo=0;bar=2 returns ['foo' => 0, 'bar' => 2].
27
     */
28
    public function parse(string $identifier): array
29
    {
30
        $matches = [];
31
        $identifiers = [];
32
        $num = preg_match_all('/(\w+)=(?<=\w=)(.+?)(?=;\w+=)|(\w+)=([^;]+);?$/', $identifier, $matches, PREG_SET_ORDER);
33
34
        foreach ($matches as $i => $match) {
35
            if ($i === $num - 1) {
36
                $identifiers[$match[3]] = $match[4];
37
                continue;
38
            }
39
            $identifiers[$match[1]] = $match[2];
40
        }
41
42
        return $identifiers;
43
    }
44
}
45