| Total Complexity | 9 |
| Total Lines | 55 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 8 | trait UsesCsv |
||
| 9 | { |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @param array|FormattedItem[] $items |
||
| 13 | * @return array |
||
| 14 | */ |
||
| 15 | 7 | public function getHeaders($items) |
|
| 16 | { |
||
| 17 | 7 | $headers = []; |
|
| 18 | 7 | foreach($items as $item) { |
|
| 19 | 7 | foreach($item->getColumnNames() as $column) { |
|
| 20 | 7 | if(!in_array($column, $headers)) { |
|
| 21 | 7 | $headers[] = $column; |
|
| 22 | } |
||
| 23 | } |
||
| 24 | } |
||
| 25 | 7 | return $headers; |
|
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param array|FormattedItem[] $items |
||
| 30 | * @param null $headers |
||
| 31 | * @param $defaultIfNull |
||
| 32 | * @return array |
||
| 33 | */ |
||
| 34 | 7 | public function getRows($items, $headers = null, $defaultIfNull = null) |
|
| 35 | { |
||
| 36 | 7 | $headers = $headers ?? $this->getHeaders($items); |
|
| 37 | |||
| 38 | 7 | $rows = []; |
|
| 39 | 7 | foreach($items as $item) { |
|
| 40 | 7 | $row = []; |
|
| 41 | 7 | foreach($headers as $header) { |
|
| 42 | 7 | $row[] = $item->getItem($header, $defaultIfNull); |
|
| 43 | } |
||
| 44 | 7 | $rows[] = $row; |
|
| 45 | } |
||
| 46 | |||
| 47 | 7 | return $rows; |
|
| 48 | } |
||
| 49 | |||
| 50 | 4 | public function generateCsv($items, $defaultIfNull = null) |
|
| 63 | } |
||
| 64 | |||
| 65 | } |