Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
25 / 25 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
ImporterConfig | |
100.00% |
25 / 25 |
|
100.00% |
10 / 10 |
17 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 | |||
getBatchSize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getEncoding | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
configureColumn | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
configureField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getColumn | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAllFields | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getOutstandingCallbacks | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
getFixedFieldValues | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * VuFind CSV importer configuration |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2021. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package CSV |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org/wiki/ Wiki |
28 | */ |
29 | |
30 | namespace VuFind\CSV; |
31 | |
32 | /** |
33 | * VuFind CSV importer configuration |
34 | * |
35 | * @category VuFind |
36 | * @package CSV |
37 | * @author Demian Katz <demian.katz@villanova.edu> |
38 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
39 | * @link https://vufind.org/wiki/ Wiki |
40 | */ |
41 | class ImporterConfig |
42 | { |
43 | /** |
44 | * Column data |
45 | * |
46 | * @var array |
47 | */ |
48 | protected $columns = []; |
49 | |
50 | /** |
51 | * Field data |
52 | * |
53 | * @var array |
54 | */ |
55 | protected $fields = []; |
56 | |
57 | /** |
58 | * Batch size |
59 | * |
60 | * @var int |
61 | */ |
62 | protected $batchSize = 100; |
63 | |
64 | /** |
65 | * Input character encoding |
66 | * |
67 | * @var string |
68 | */ |
69 | protected $encoding = 'UTF-8'; |
70 | |
71 | /** |
72 | * Constructor |
73 | * |
74 | * @param array $options Config options |
75 | */ |
76 | public function __construct($options = []) |
77 | { |
78 | if (isset($options['batchSize']) && $options['batchSize'] > 0) { |
79 | $this->batchSize = $options['batchSize']; |
80 | } |
81 | if (isset($options['encoding'])) { |
82 | $this->encoding = $options['encoding']; |
83 | } |
84 | } |
85 | |
86 | /** |
87 | * Get batch size setting |
88 | * |
89 | * @return int |
90 | */ |
91 | public function getBatchSize(): int |
92 | { |
93 | return $this->batchSize; |
94 | } |
95 | |
96 | /** |
97 | * Get encoding setting |
98 | * |
99 | * @return string |
100 | */ |
101 | public function getEncoding(): string |
102 | { |
103 | return $this->encoding; |
104 | } |
105 | |
106 | /** |
107 | * Add column configuration |
108 | * |
109 | * @param int $column Column number |
110 | * @param array $config Column configuration |
111 | * |
112 | * @return void |
113 | */ |
114 | public function configureColumn(int $column, array $config): void |
115 | { |
116 | // Merge the incoming configuration with any existing configuration: |
117 | $this->columns[$column] = array_merge($this->getColumn($column), $config); |
118 | |
119 | // If the configuration contains field names, initialize those configs |
120 | // so we are sure to have a complete field list from getFields(): |
121 | if (isset($config['field'])) { |
122 | foreach ((array)$config['field'] as $field) { |
123 | if (!isset($this->fields[$field])) { |
124 | $this->fields[$field] = []; |
125 | } |
126 | } |
127 | } |
128 | } |
129 | |
130 | /** |
131 | * Add field configuration |
132 | * |
133 | * @param string $name Field name |
134 | * @param array $config Field configuration |
135 | * |
136 | * @return void |
137 | */ |
138 | public function configureField(string $name, array $config): void |
139 | { |
140 | // Merge the incoming configuration with any existing configuration: |
141 | $this->fields[$name] = array_merge($this->getField($name), $config); |
142 | } |
143 | |
144 | /** |
145 | * Get configuration for the specified column. |
146 | * |
147 | * @param int $column Column number |
148 | * |
149 | * @return array |
150 | */ |
151 | public function getColumn(int $column): array |
152 | { |
153 | return $this->columns[$column] ?? []; |
154 | } |
155 | |
156 | /** |
157 | * Get configuration for the specified field. |
158 | * |
159 | * @param string $name Field name |
160 | * |
161 | * @return array |
162 | */ |
163 | public function getField(string $name): array |
164 | { |
165 | return $this->fields[$name] ?? []; |
166 | } |
167 | |
168 | /** |
169 | * Get all field names |
170 | * |
171 | * @return string[] |
172 | */ |
173 | public function getAllFields(): array |
174 | { |
175 | return array_keys($this->fields); |
176 | } |
177 | |
178 | /** |
179 | * Get a list of fields with callbacks that have not already been processed. |
180 | * |
181 | * @param string[] $processed List of fields that have already been processed. |
182 | * |
183 | * @return string[] |
184 | */ |
185 | public function getOutstandingCallbacks(array $processed): array |
186 | { |
187 | // Get a list of fields that have not already been processed, and then |
188 | // filter out any that lack a callback configuration. |
189 | return array_filter( |
190 | array_diff($this->getAllFields(), $processed), |
191 | function ($field) { |
192 | return isset($this->getField($field)['callback']); |
193 | } |
194 | ); |
195 | } |
196 | |
197 | /** |
198 | * Initialize a field array with any fixed values configured here. |
199 | * |
200 | * @return array |
201 | */ |
202 | public function getFixedFieldValues() |
203 | { |
204 | $fields = []; |
205 | foreach ($this->getAllFields() as $field) { |
206 | $values = $this->getField($field)['value'] ?? []; |
207 | $fields[$field] = (array)$values; |
208 | } |
209 | return $fields; |
210 | } |
211 | } |