CsvConverter   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 1
dl 0
loc 21
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A toJson() 0 13 2
1
<?php
2
3
/**
4
 * This file contains the CSV converter used by PhpSoda to officially support CSV as a data format
5
 *
6
 * @copyright 2015 Vladimir Jimenez
7
 * @license   https://github.com/allejo/PhpSoda/blob/master/LICENSE.md MIT
8
 */
9
10
namespace allejo\Socrata\Converters;
11
12
/**
13
 * The class used to officially support CSV as a data format that can be submitted to Socrata
14
 *
15
 * This class extends the Converter base class which implements the abstract `toJson()` method that PhpSoda calls when
16
 * sending data to Socrata.
17
 *
18
 * @package allejo\Socrata\Converters
19
 * @see     allejo\Socrata\Converters\Converter
20
 * @since   0.1.0
21
 */
22
class CsvConverter extends Converter
23
{
24
    /**
25
     * {@inheritdoc}
26
     *
27
     * @link   http://steindom.com/articles/shortest-php-code-convert-csv-associative-array
28
     */
29
    public function toJson ()
30
    {
31
        $rows    = array_map("str_getcsv", explode("\n", trim($this->data)));
32
        $columns = array_shift($rows);
33
        $csv     = array();
34
35
        foreach ($rows as $row)
36
        {
37
            $csv[] = array_combine($columns, $row);
38
        }
39
40
        return json_encode($csv);
41
    }
42
}
43