Passed
Push — master ( 99e8d5...14b05a )
by Daniel
01:54
created

ArrayToCsv   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setHeaderFromIndexes() 0 10 4
A __construct() 0 3 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * This file is part of the Csv-Machine package.
5
 *
6
 * (c) Dan McAdams <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace RoadBunch\Csv;
13
14
15
use RoadBunch\Csv\Header\Header;
16
use RoadBunch\Csv\Header\HeaderInterface;
17
18
class ArrayToCsv
19
{
20
    /** @var array */
21
    protected $rawData;
22
23
    /** @var HeaderInterface */
24
    protected $header;
25
26
    /**
27
     * Csv constructor.
28
     *
29
     * Passed data array should be a two dimensional array only or an exception will be thrown
30
     * when the time comes to build the CSV
31
     *
32
     * $data = [
33
     *      ['first_name' => 'John', 'last_name' => 'Doe', 'employee_id' => '742617000027'],
34
     *      ['first_name' => 'Jane', 'last_name' => 'Jackson', 'employee_id' => '0003645'],
35
     *      ['first_name' => 'Dede', 'last_name' => 'Gore', 'OMG12324']
36
     * ];
37
     *
38
     * OR
39
     *
40
     * $data = [
41
     *      ['John', 'Doe', '742617000027'],
42
     *      ['Jane', 'Jackson', '01011970'],
43
     *      ['Dede', 'Gore', 'OMG1234']
44
     * ];
45
     *
46
     * @param array $data
47
     */
48 4
    public function __construct(array $data)
49
    {
50 4
        $this->rawData = $data;
51 4
    }
52
53
    /**
54
     * Will set the header array from the indexes of the two dimensional array
55
     * if the array is empty, the header will be set to an empty array.
56
     */
57 3
    public function setHeaderFromIndexes()
58
    {
59 3
        if (empty($this->rawData)) {
60 1
            $this->header = new Header([]);
61 1
            return;
62
        }
63 2
        if ((!isset($this->rawData[0])) || (!is_array($this->rawData[0]))) {
64 1
            throw new \InvalidArgumentException('Expected two dimensional array');
65
        }
66 1
        $this->header = new Header(array_keys($this->rawData[0]));
67 1
    }
68
}
69