Completed
Push — master ( 54855b...e3699c )
by Zoltán
02:43
created

YAMLDataSetLoader::setDataSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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 string
39
     */
40
    private $content;
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 4
    public function __construct($file, YAMLParserInterface $parser = NULL) {
49 4
        $this->file = $file;
50 4
        $this->parser = $parser;
51
52 4
        if($this->parser === NULL) {
53 3
            $this->parser = YAMLParserFactory::getParser();
54 3
        }
55
56 4
        $this->load();
57 4
    }
58
59
    /**
60
     * Set the data set name that returned in getResult. If no specific DataSet are
61
     * selected, the first DataSet are selected.
62
     *
63
     * @param string $dataSetName
64
     *
65
     * @return \BuildR\TestTools\DataSetLoader\YAML\YAMLDataSetLoader
66
     */
67 1
    public function setDataSet($dataSetName) {
68 1
        $this->dataSetName = $dataSetName;
69
70 1
        return $this;
71
    }
72
73
    /**
74
     * Returns the current parser
75
     *
76
     * @return \BuildR\TestTools\DataSetLoader\YAML\Parser\YAMLParserInterface
77
     */
78 2
    public function getParser() {
79 2
       return $this->parser;
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85 4
    public function load() {
86 4
        $this->content = file_get_contents($this->file);
87
88 4
        return $this;
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94 2
    public function getResult() {
95 2
        $result = $this->parser->parse($this->content);
96
97 2
        if($this->dataSetName !== NULL && isset($result[$this->dataSetName])) {
98 1
            return $result[$this->dataSetName];
99
        }
100
101 1
        return current($result);
102
    }
103
104
}
105