Completed
Push — master ( ad7fd3...b10ab8 )
by Théo
03:40
created

YamlValidator::validate()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 24
rs 8.5125
cc 5
eloc 12
nc 5
nop 2
1
<?php
2
3
/*
4
 * This file is part of the LaravelYaml package.
5
 *
6
 * (c) Théo FIDRY <[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
namespace Fidry\LaravelYaml\FileLoader\Yaml;
13
14
use Fidry\LaravelYaml\Exception\Configuration\InvalidArgumentException;
15
16
/**
17
 * @author Théo FIDRY <[email protected]>
18
 */
19
final class YamlValidator
20
{
21
    /**
22
     * Checks that the content returned by the YAML parser has an understandable definition.
23
     *
24
     * @param $content
25
     * @param $file
26
     *
27
     * @return mixed
28
     * @throws InvalidArgumentException
29
     */
30
    public function validate($content, $file)
31
    {
32
        if (null === $content) {
33
            return $content;
34
        }
35
36
        if (false === is_array($content)) {
37
            throw new InvalidArgumentException(
38
                sprintf('The service file "%s" is not valid. It should contain an array. Check your YAML syntax.', $file)
39
            );
40
        }
41
42
        foreach ($content as $namespace => $data) {
43
            if (in_array($namespace, ['parameters', 'services'])) {
44
                continue;
45
            }
46
47
            throw new InvalidArgumentException(
48
                sprintf('Invalid namespace name "%s" in "%s".', $namespace, $file)
49
            );
50
        }
51
52
        return $content;
53
    }
54
}
55