Completed
Push — master ( 48745e...b53abb )
by Zoltán
02:36
created

YAMLDataSetLoader::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 6
1
<?php namespace BuildR\TestTools\DataSetLoader\YAML;
2
3
use BuildR\TestTools\DataSetLoader\DataSetLoaderInterface;
4
use BuildR\TestTools\DataSetLoader\YAML\Parser\YAMLParserFactory;
5
use BuildR\TestTools\DataSetLoader\YAML\Parser\YAMLParserInterface;
6
7
/**
8
 * YAML based DataSet loader
9
 *
10
 * BuildR PHP Framework
11
 *
12
 * @author Zoltán Borsos <[email protected]>
13
 * @package TestTools
14
 * @subpackage DataSetLoader\YAML
15
 *
16
 * @copyright    Copyright 2016, Zoltán Borsos.
17
 * @license      https://github.com/BuildrPHP/Test-Tools/blob/master/LICENSE.md
18
 * @link         https://github.com/BuildrPHP/Test-Tools
19
 */
20
class YAMLDataSetLoader implements DataSetLoaderInterface {
21
22
    /**
23
     * @type string
24
     */
25
    protected $file;
26
27
    /**
28
     * @type \BuildR\TestTools\DataSetLoader\YAML\Parser\YAMLParserInterface
29
     */
30
    protected $parser;
31
32
    /**
33
     * @type string|NULL
34
     */
35
    private $dataSetName;
36
37
    /**
38
     * @type array
39
     */
40
    private $result;
41
42
    /**
43
     * YAMLDataSetLoader constructor.
44
     *
45
     * @param string $file The laoded YAML file absolute location
46
     * @param \BuildR\TestTools\DataSetLoader\YAML\Parser\YAMLParserInterface $parser
47
     */
48
    public function __construct($file, YAMLParserInterface $parser = NULL) {
49
        $this->file = $file;
50
        $this->parser = $parser;
51
52
        if($this->parser === NULL) {
53
            $this->parser = YAMLParserFactory::getParser();
54
        }
55
    }
56
57
    /**
58
     * Set the data set name that returned in getResult. If no specific DataSet are
59
     * selected, the first DataSet are selected.
60
     *
61
     * @param string $dataSetName
62
     *
63
     * @return \BuildR\TestTools\DataSetLoader\YAML\YAMLDataSetLoader
64
     */
65
    public function setDataSet($dataSetName) {
66
        $this->dataSetName = $dataSetName;
67
68
        return $this;
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74
    public function load() {
75
        $content = file_get_contents($this->file);
76
        $this->result = $this->parser->parse($content);
77
78
        return $this;
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84
    public function getResult() {
85
        if($this->dataSetName !== NULL && isset($this->result[$this->dataSetName])) {
86
            return $this->result[$this->dataSetName];
87
        }
88
89
        return current($this->result);
90
    }
91
92
}
93