DataTable::transform()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 12
nc 8
nop 1
1
<?php
2
3
namespace Ghc\Rosetta\Transformers;
4
5
use Ghc\Rosetta\Item;
6
7
class DataTable extends Transformer
8
{
9
    /**
10
     * Boot Transformer.
11
     */
12 View Code Duplication
    protected function boot()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
    {
14
        $this->addDefaultConfig([
15
            'hasHeader'        => true,
16
            'useHeaderAsIndex' => true,
17
            'ignoreHeader'     => false,
18
            'orientation'      => 'vertical',
19
        ]);
20
    }
21
22
    /**
23
     * @param array $data
24
     *
25
     * @return array
26
     */
27
    public function transform($data)
28
    {
29
        if ('horizontal' == $this->config['orientation']) {
30
            $data = (new Item($data, [new TransposeValues()]))->toArray();
31
        }
32
33
        if ($this->config['hasHeader']) {
34
            $header = array_shift($data);
35
36
            if ($this->config['useHeaderAsIndex']) {
37
                $data = collect($data)->map(function ($row) use ($header) {
38
                    return collect($row)->mapWithKeys(function ($column, $index) use ($header) {
39
                        return [$header[$index] => $column];
40
                    });
41
                })->toArray();
42
            } elseif (!$this->config['ignoreHeader']) {
43
                array_unshift($data, $header);
44
            }
45
        }
46
47
        return $data;
48
    }
49
}
50