Completed
Push — master ( fba701...cc5f30 )
by Sébastien
10:03 queued 08:37
created

AbstractWriter::setOptions()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.4558

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 14
cts 19
cp 0.7368
rs 9.1288
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5.4558
1
<?php
2
3
/*
4
 * soluble-flexstore library
5
 *
6
 * @author    Vanvelthem Sébastien
7
 * @link      https://github.com/belgattitude/soluble-flexstore
8
 * @copyright Copyright (c) 2016-2017 Vanvelthem Sébastien
9
 * @license   MIT License https://github.com/belgattitude/soluble-flexstore/blob/master/LICENSE.md
10
 *
11
 */
12
13
namespace Soluble\FlexStore\Writer;
14
15
use Soluble\FlexStore\Exception;
16
use Traversable;
17
use Soluble\FlexStore\Options;
18
use Soluble\FlexStore\Store\StoreInterface;
19
20
abstract class AbstractWriter
21
{
22
    /**
23
     * @var StoreInterface
24
     */
25
    protected $store;
26
27
    /**
28
     * @var array
29
     */
30
    protected $options = [
31
        'debug' => false,
32
        'charset' => 'UTF-8'
33
    ];
34
35
    /**
36
     * @param StoreInterface|null    $store
37
     * @param array|Traversable|null $options
38
     */
39 21
    public function __construct(StoreInterface $store = null, $options = null)
40
    {
41 21
        if ($store !== null) {
42 13
            $this->setStore($store);
43
        }
44 21
        if ($options !== null) {
45
            $this->setOptions($options);
46
        }
47 21
    }
48
49
    /**
50
     * @param StoreInterface $store
51
     *
52
     * @return AbstractWriter
53
     */
54 21
    public function setStore(StoreInterface $store)
55
    {
56 21
        $this->store = $store;
57
58 21
        return $this;
59
    }
60
61
    /**
62
     * Return data.
63
     *
64
     * @param Options $options
65
     */
66
    abstract public function getData(Options $options = null);
67
68
    /**
69
     * Save content to a file.
70
     *
71
     * @param string $filename
72
     * @param string $charset
73
     */
74
    public function save($filename, $charset = null)
0 ignored issues
show
Unused Code introduced by
The parameter $charset is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76
        $data = $this->getData();
77
78
        /*
79
        if ($charset === null) {
80
            $charset = $this->options['charset'];
81
        }
82
83
        */
84
        // UTF-8 : file_put_contents("file.txt", "\xEF\xBB\xBF" . $data);
85
86
        /*
87
88
            $data = file_get_contents($npath);
89
            $data = mb_convert_encoding($data, 'UTF-8', 'OLD-ENCODING');
90
            file_put_contents('tempfolder/'.$a, $data);
91
92
            Or alternatively, with PHP's stream filters:
93
94
            $fd = fopen($file, 'r');
95
            stream_filter_append($fd, 'convert.iconv.UTF-8/OLD-ENCODING');
96
            stream_copy_to_stream($fd, fopen($output, 'w'));
97
         *
98
         * mb_convert_encoding($data, 'UTF-8', 'auto');
99
         * mb_convert_encoding($data, 'UTF-8', mb_detect_encoding($data));
100
         */
101
102
        $ret = file_put_contents($filename, $data);
103
        if (!$ret) {
104
            throw new \Exception("Filename $filename cannot be written");
105
        }
106
    }
107
108
    /**
109
     * @param bool $debug
110
     *
111
     * @return AbstractWriter
112
     */
113 2
    public function setDebug($debug = true)
114
    {
115 2
        $this->options['debug'] = $debug;
116
117 2
        return $this;
118
    }
119
120
    /**
121
     * Set options.
122
     *
123
     * @throws Exception\InvalidArgumentException
124
     *
125
     * @return AbstractWriter
126
     */
127 9
    public function setOptions(iterable $options)
128
    {
129 9
        if (!is_iterable($options)) {
130
            throw new Exception\InvalidArgumentException(
131
                sprintf(
132
                '"%s" expects an array or Traversable; received "%s"',
133
                __METHOD__,
134
                gettype($options)
135
            )
136
            );
137
        }
138
139 9
        foreach ($options as $key => $value) {
140 9
            $setter = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
141 9
            if (method_exists($this, $setter)) {
142 1
                $this->{$setter}($value);
143 8
            } elseif (array_key_exists($key, $this->options)) {
144 7
                $this->options[$key] = $value;
145
            } else {
146 1
                throw new Exception\InvalidArgumentException(sprintf(
147 1
                    'The option "%s" does not have a matching %s setter method or options[%s] array key',
148 1
                    $key,
149 1
                    $setter,
150 9
                    $key
151
                ));
152
            }
153
        }
154
155 8
        return $this;
156
    }
157
158
    /**
159
     * Retrieve options.
160
     *
161
     * @return array
162
     */
163
    public function getOptions()
164
    {
165
        return $this->options;
166
    }
167
}
168