1 | <?php |
||
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) |
||
107 | |||
108 | /** |
||
109 | * @param bool $debug |
||
110 | * |
||
111 | * @return AbstractWriter |
||
112 | */ |
||
113 | 2 | public function setDebug($debug = true) |
|
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() |
||
167 | } |
||
168 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.