Completed
Push — master ( 7bef58...5c053f )
by Julito
25:30
created

Import::xlsToArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nop 1
dl 0
loc 10
rs 9.4285
nc 2
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Ddeboer\DataImport\Reader\ExcelReader;
5
use League\Csv\Reader;
6
7
/**
8
 * Class Import
9
 * This class provides some functions which can be used when importing data from
10
 * external files into Chamilo.
11
 *
12
 * @package chamilo.library
13
 */
14
class Import
15
{
16
    /**
17
     * @param string $path
18
     * @param bool   $setFirstRowAsHeader
19
     *
20
     * @return array
21
     */
22
    public static function csv_reader($path, $setFirstRowAsHeader = true)
0 ignored issues
show
Unused Code introduced by
The parameter $setFirstRowAsHeader is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

22
    public static function csv_reader($path, /** @scrutinizer ignore-unused */ $setFirstRowAsHeader = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
23
    {
24
        return self::csvToArray($path);
25
    }
26
27
    /**
28
     * Reads a CSV-file into an array. The first line of the CSV-file should contain the array-keys.
29
     * The encoding of the input file is tried to be detected.
30
     * The elements of the returned array are encoded in the system encoding.
31
     * Example:
32
     *   FirstName;LastName;Email
33
     *   John;Doe;[email protected]
34
     *   Adam;Adams;[email protected]
35
     *  returns
36
     *   $result [0]['FirstName'] = 'John';
37
     *   $result [0]['LastName'] = 'Doe';
38
     *   $result [0]['Email'] = 'john.doe@mail. com';
39
     *   $result [1]['FirstName'] = 'Adam';
40
     *   ...
41
     *
42
     * @param string $filename the path to the CSV-file which should be imported
43
     *
44
     * @return array returns an array (in the system encoding) that contains all data from the CSV-file
45
     */
46
    public static function csvToArray($filename)
47
    {
48
        if (empty($filename)) {
49
            return [];
50
        }
51
52
        $reader = Reader::createFromPath($filename, 'r');
53
        if ($reader) {
54
            $reader->setDelimiter(';');
55
            $reader->stripBom(true);
56
            /*$contents = $reader->__toString();
57
            if (!Utf8::isUtf8($contents)) {
58
                // If file is not in utf8 try converting to ISO-8859-15
59
                if ($reader->getStreamFilterMode() == 1) {
60
                    $reader->appendStreamFilter('convert.iconv.ISO-8859-15/UTF-8');
61
                }
62
            }*/
63
64
            $iterator = $reader->fetchAssoc(0);
65
66
            return iterator_to_array($iterator);
67
        }
68
69
        return [];
70
    }
71
72
    /**
73
     * @param string $filename
74
     *
75
     * @return array
76
     */
77
    public static function xlsToArray($filename)
78
    {
79
        if (empty($filename)) {
80
            return [];
81
        }
82
83
        $file = new \SplFileObject($filename);
84
        $reader = new ExcelReader($file, 0);
85
86
        return $reader;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $reader returns the type Ddeboer\DataImport\Reader\ExcelReader which is incompatible with the documented return type array.
Loading history...
87
    }
88
}
89