Passed
Pull Request — 1.11.x (#4360)
by Angel Fernando Quiroz
08:20 queued 16s
created

Import::csvColumnToArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 18
rs 9.9666
cc 3
nop 2
nc 3
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Ddeboer\DataImport\Reader\ExcelReader;
6
use League\Csv\Reader;
7
use Symfony\Component\DomCrawler\Crawler;
8
9
/**
10
 * Class Import
11
 * This class provides some functions which can be used when importing data from
12
 * external files into Chamilo.
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)
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
    public static function csvColumnToArray($filename, $columnIndex = 0): array
73
    {
74
        if (empty($filename)) {
75
            return [];
76
        }
77
78
        $reader = Reader::createFromPath($filename, 'r');
79
80
        if (!$reader) {
81
            return [];
82
        }
83
84
        $reader->setDelimiter(';');
85
        $reader->stripBom(true);
86
87
        $iterator = $reader->fetchColumn($columnIndex);
88
89
        return iterator_to_array($iterator);
90
    }
91
92
    /**
93
     * @param string $filename
94
     *
95
     * @return array
96
     */
97
    public static function xlsToArray($filename)
98
    {
99
        if (empty($filename)) {
100
            return [];
101
        }
102
103
        $file = new \SplFileObject($filename);
104
105
        return new ExcelReader($file, 0);
106
    }
107
108
    /**
109
     * @param string $file
110
     *
111
     * @return Crawler
112
     */
113
    public static function xml($file)
114
    {
115
        return self::xmlFromString(file_get_contents($file));
116
    }
117
118
    /**
119
     * @param string $contents
120
     *
121
     * @return Crawler
122
     */
123
    public static function xmlFromString($contents)
124
    {
125
        @libxml_disable_entity_loader(true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for libxml_disable_entity_loader(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

125
        /** @scrutinizer ignore-unhandled */ @libxml_disable_entity_loader(true);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
126
127
        $crawler = new Crawler();
128
        $crawler->addXmlContent($contents);
129
130
        return $crawler;
131
    }
132
}
133