Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

Kunstmaan/FixturesBundle/Parser/Spec/Listed.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
namespace Kunstmaan\FixturesBundle\Parser\Spec;
4
5
use Kunstmaan\FixturesBundle\Loader\Fixture;
6
7
class Listed implements SpecParserInterface
8
{
9
    const REGEX = '/{([^,]+,?)+}$/';
10
11
    /**
12
     * Check if this parser is applicable
13
     *
14
     * @return bool
0 ignored issues
show
Should the return type not be integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
15
     */
16
    public function canParse($value)
17
    {
18
        return preg_match(self::REGEX, $value);
19
    }
20
21
    /**
22
     * Parse provided value into new data
23
     *
24
     * @param $spec
25
     * @param $fixture
26
     * @param $fixtures
27
     *
28
     * @return mixed
29
     *
30
     * @throws \Exception
31
     */
32
    public function parse(Fixture $fixture, array $fixtures, $spec)
33
    {
34
        preg_match(self::REGEX, $spec, $matches);
35
        if (empty($matches)) {
36
            return $fixtures;
37
        }
38
39
        $list = $matches[0];
40
        $name = substr($spec, 0, strpos($spec, $list));
41
42
        preg_match_all('/[^,{}]+/', $list, $keys);
43
        if (empty($keys)) {
44
            return $fixtures;
45
        }
46
47
        $keys = array_map(function ($key) {
48
            return trim($key);
49
        }, $keys[0]);
50
        foreach ($keys as $item) {
51
            $newFixture = clone $fixture;
52
            $newFixture->setName($name . $item);
53
            $newFixture->setSpec($item);
54
            $fixtures[$name . $item] = $newFixture;
55
        }
56
57
        return $fixtures;
58
    }
59
}
60