Completed
Push — master ( a3f369...f825b4 )
by Tim
14s
created

Lexer::parse()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 0
cts 29
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 24
nc 6
nop 2
crap 56
1
<?php
2
3
/**
4
 * TechDivision\Import\Adapter\Goodby\Lexer
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Adapter\Goodby;
22
23
use Goodby\CSV\Import\Protocol\LexerInterface;
24
use Goodby\CSV\Import\Protocol\InterpreterInterface;
25
use Goodby\CSV\Import\Standard\LexerConfig;
26
use Goodby\CSV\Import\Standard\StreamFilter\ConvertMbstringEncoding;
27
28
/**
29
 * Custom exporter implementation which resets row consistency on every import.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2016 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/techdivision/import
35
 * @link      http://www.techdivision.com
36
 */
37
class Lexer implements LexerInterface
38
{
39
40
    /**
41
     * The exporter configuration.
42
     *
43
     * @var \Goodby\CSV\Import\Standard\LexerConfig
44
     */
45
    private $config;
46
47
    /**
48
     * Initialize the instance with the passed configuration.
49
     *
50
     * @param \Goodby\CSV\Import\Standard\LexerConfig $config The lexer configuration
51
     */
52
    public function __construct(LexerConfig $config = null)
53
    {
54
55
        // query whether or not a configuration has been passed
56
        if ($config instanceof LexerConfig) {
57
            $this->config = $config;
58
        } else {
59
            $this->config = new LexerConfig();
60
        }
61
62
        // register the encoding filter
63
        ConvertMbstringEncoding::register();
64
    }
65
66
    /**
67
     * Parse the passed CSV file.
68
     *
69
     * @param string                                           $filename    The filename to parse
70
     * @param \Goodby\CSV\Import\Protocol\InterpreterInterface $interpreter The interpreter instance
71
     *
72
     * @return void
73
     */
74
    public function parse($filename, InterpreterInterface $interpreter)
75
    {
76
77
        // for mac's office excel csv
78
        ini_set('auto_detect_line_endings', true);
79
80
        // initialize the configuration
81
        $delimiter      = $this->config->getDelimiter();
82
        $enclosure      = $this->config->getEnclosure();
83
        $escape         = $this->config->getEscape();
84
        $fromCharset    = $this->config->getFromCharset();
85
        $toCharset      = $this->config->getToCharset();
86
        $flags          = $this->config->getFlags();
87
        $ignoreHeader   = $this->config->getIgnoreHeaderLine();
88
89
        // query whether or not the charset has to be converted
90
        if ($fromCharset === null) {
91
            $url = $filename;
92
        } else {
93
            $url = ConvertMbstringEncoding::getFilterURL($filename, $fromCharset, $toCharset);
94
        }
95
96
        // initialize the CSV file object
97
        $csv = new \SplFileObject($url);
98
        $csv->setCsvControl($delimiter, $enclosure, $escape);
99
        $csv->setFlags($flags);
100
101
        // backup current locale
102
        $originalLocale = setlocale(LC_ALL, '0');
103
        setlocale(LC_ALL, 'en_US.UTF-8');
104
105
        // process each line of the CSV file
106
        foreach ($csv as $lineNumber => $line) {
107
            if ($ignoreHeader && $lineNumber == 0 || (count($line) === 1 && trim($line[0]) === '')) {
108
                continue;
109
            }
110
            $interpreter->interpret($line);
111
        }
112
113
        // reset locale
114
        parse_str(str_replace(';', '&', $originalLocale), $locale_array);
115
        setlocale(LC_ALL, $locale_array);
116
    }
117
}
118