LegacyFormatter::setData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace BrowscapPHP\Formatter;
5
6
/**
7
 * formatter for backwards compatibility with 2.x
8
 */
9
final class LegacyFormatter implements FormatterInterface
10
{
11
    /**
12
     * Options for the formatter
13
     *
14
     * @var array
15
     */
16
    private $options = [];
17
18
    /**
19
     * Default formatter options
20
     *
21
     * @var array
22
     */
23
    private $defaultOptions = [
24
        'lowercase' => false,
25
    ];
26
27
    /**
28
     * Variable to save the settings in, type depends on implementation
29
     *
30
     * @var array
31
     */
32
    private $data = [];
33
34
    /**
35
     * LegacyFormatter constructor.
36
     *
37
     * @param array $options Formatter optioms
38
     */
39 2
    public function __construct(array $options = [])
40
    {
41 2
        $this->options = array_merge($this->defaultOptions, $options);
42 2
    }
43
44
    /**
45
     * Sets the data (done by the parser)
46
     *
47
     * @param array $settings
48
     */
49 2
    public function setData(array $settings) : void
50
    {
51 2
        $this->data = $settings;
52 2
    }
53
54
    /**
55
     * Gets the data (in the preferred format)
56
     *
57
     * @return \stdClass
58
     */
59 2
    public function getData() : \stdClass
60
    {
61 2
        $output = new \stdClass();
62
63 2
        foreach ($this->data as $key => $property) {
64 2
            if ($this->options['lowercase']) {
65 1
                $key = strtolower($key);
66
            }
67
68 2
            $output->{$key} = $property;
69
        }
70
71 2
        return $output;
72
    }
73
}
74