CSVHelper_Builder::setSeparatorChar()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * File containing the {@see AppUtils\CSVHelper_Builder} class.
4
 *
5
 * @package Application Utils
6
 * @subpackage CSVHelper
7
 * @see AppUtils\CSVHelper_Builder
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppUtils;
13
14
/**
15
 * The CSV builder allows creating CSV contents 
16
 * with an object-oriented interface.
17
 *
18
 * @package Application Utils
19
 * @subpackage CSVHelper
20
 */
21
class CSVHelper_Builder
22
{
23
    /**
24
     * @var string[]
25
     */
26
    protected array $lines = array();
27
28
    /**
29
     * @var array<string,mixed>|NULL
30
     */
31
    protected ?array $options = null;
32
33
    /**
34
     * @return array<string,mixed>
35
     */
36
    public function getDefaultOptions() : array
37
    {
38
        return array(
39
            'separatorChar' => ';',
40
            'trailingNewline' => false
41
        );
42
    }
43
    
44
    public function setSeparatorChar(string $char) : self
45
    {
46
        return $this->setOption('separatorChar', $char);
47
    }
48
49
    public function setTrailingNewline(bool $useNewline=true) : self
50
    {
51
        return $this->setOption('trailingNewline', $useNewline);
52
    }
53
54
    /**
55
     * Adds a line of data keys to be added to the CSV.
56
     *
57
     * @param mixed ...$args An array with values, or each entry as a separate argument to addLine().
58
     * @see renderCSV()
59
     * @return $this
60
     */
61
    public function addLine(...$args) : self
62
    {
63
        if (is_array($args[0])) {
64
            $args = $args[0];
65
        }
66
67
        $this->lines[] = '"' . implode('"'.$this->getOption('separatorChar').'"', $args) . '"';
68
        
69
        return $this;
70
    }
71
72
    /**
73
     * Renders the CSV from the data lines that have been
74
     * added by {@link addLine()}.
75
     *
76
     * @see addLine()
77
     * @return string
78
     */
79
    public function render() : string
80
    {
81
        $csv = implode(PHP_EOL, $this->lines);
82
83
        if($this->getOption('trailingNewline')) {
84
            $csv .= PHP_EOL;
85
        }
86
87
        return $csv;
88
    }
89
90
    /**
91
     * @param string $name
92
     * @param mixed $value
93
     * @return $this
94
     */
95
    public function setOption(string $name, $value) : self
96
    {
97
        if(!isset($this->options)) {
98
            $this->options = $this->getDefaultOptions();
99
        }
100
        
101
        $this->options[$name] = $value;
102
        return $this;
103
    }
104
105
    /**
106
     * @param array<string,mixed> $options
107
     * @return $this
108
     */
109
    public function setOptions(array $options) : self
110
    {
111
        foreach($options as $name => $value) {
112
            $this->setOption($name, $value);
113
        }
114
        
115
        return $this;
116
    }
117
118
    /**
119
     * @param string $name
120
     * @param mixed|NULL $default
121
     * @return mixed|NULL
122
     */
123
    public function getOption(string $name, $default=null)
124
    {
125
        if(!isset($this->options)) {
126
            $this->options = $this->getDefaultOptions();
127
        }
128
129
        return $this->options[$name] ?? $default;
130
    }
131
    
132
    public function hasOption(string $name) : bool
133
    {
134
        if(!isset($this->options)) {
135
            $this->options = $this->getDefaultOptions();
136
        }
137
        
138
        return array_key_exists($name, $this->options);
139
    }
140
141
    /**
142
     * @return array<string,mixed>
143
     */
144
    public function getOptions() : array
145
    {
146
        if(!isset($this->options)) {
147
            $this->options = $this->getDefaultOptions();
148
        }
149
        
150
        return $this->options;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->options could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
151
    }
152
153
    /**
154
     * @param string $name
155
     * @param mixed|NULL $value
156
     * @return bool
157
     */
158
    public function isOption(string $name, $value) : bool
159
    {
160
        return $this->getOption($name) === $value;
161
    }
162
}
163