YamlParser::yamlToArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2.0625
1
<?php
2
3
namespace Amock\Parser;
4
5
use ArrayAccess;
6
use Symfony\Component\Yaml\Yaml;
7
8
class YamlParser implements Parser, ArrayAccess
9
{
10
    private $parsed = [];
11
12 10
    public function parse(string $rawYaml): void
13
    {
14 10
        foreach ($this->yamlToArray($rawYaml) as $className => $yamlArray) {
15 10
            foreach ($yamlArray as $id => $mockArray) {
16 10
                $this->parsed[$id] = [$className => $mockArray];
17
            }
18
        }
19 10
    }
20
21 10
    protected function yamlToArray(string $rawYaml): array
22
    {
23 10
        if (function_exists('yaml_parse')) {
24
            return yaml_parse($rawYaml);
25
        }
26
27 10
        return Yaml::parse($rawYaml);
28
    }
29
30
    public function offsetSet($offset, $value)
31
    {
32
        if (is_null($offset)) {
33
            $this->parsed[] = $value;
34
        } else {
35
            $this->parsed[$offset] = $value;
36
        }
37
    }
38
39 1
    public function offsetExists($offset)
40
    {
41 1
        return isset($this->parsed[$offset]);
42
    }
43
44
    public function offsetUnset($offset)
45
    {
46
        unset($this->parsed[$offset]);
47
    }
48
49 10
    public function offsetGet($offset)
50
    {
51 10
        return isset($this->parsed[$offset]) ? $this->parsed[$offset] : null;
52
    }
53
}
54