Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
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
     * @return mixed
28
     * @throws \Exception
29
     */
30
    public function parse(Fixture $fixture, array $fixtures, $spec)
31
    {
32
        preg_match(self::REGEX, $spec, $matches);
33
        if (empty($matches)) {
34
            return $fixtures;
35
        }
36
37
        $list = $matches[0];
38
        $name = substr($spec, 0, strpos($spec, $list));
39
40
        preg_match_all('/[^,{}]+/', $list, $keys);
41
        if (empty($keys)) {
42
            return $fixtures;
43
        }
44
45
        $keys = array_map(function ($key) {
46
            return trim($key);
47
        }, $keys[0]);
48
        foreach ($keys as $item) {
49
            $newFixture = clone $fixture;
50
            $newFixture->setName($name . $item);
51
            $newFixture->setSpec($item);
52
            $fixtures[$name . $item] = $newFixture;
53
        }
54
55
        return $fixtures;
56
    }
57
}
58