CsvIterator::getSeparator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace FMUP\Import\Iterator;
3
4
use FMUP\Import\Exception;
5
6
/**
7
 * Permet de parcourir un csv ligne par ligne
8
 *
9
 * @author jyamin
10
 *
11
 */
12
class CsvIterator implements \Iterator
13
{
14
    const COMMA_SEPARATOR = ',';
15
    const SEMICOLON_SEPARATOR = ';';
16
    const TABULATION_SEPARATOR = "\t";
17
18
    protected $path;
19
    private $fHandle;
20
    private $current;
21
    private $line;
22
    private $separator;
23
24
    /**
25
     * @param string $path
26
     * @param string $separator optional (if null, autodetect)
27
     */
28 3
    public function __construct($path = "", $separator = self::SEMICOLON_SEPARATOR)
29
    {
30 3
        $this->setPath($path)->setSeparator($separator);
31 3
    }
32
33
    /**
34
     * @param string $path
35
     * @return $this
36
     */
37 3
    public function setPath($path)
38
    {
39 3
        $this->path = (string)$path;
40 3
        return $this;
41
    }
42
43 1
    public function getPath()
44
    {
45 1
        return $this->path;
46
    }
47
48
    /**
49
     * @throws Exception
50
     */
51 2 View Code Duplication
    public function rewind()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53 2
        if (!file_exists($this->path)) {
54 1
            throw new Exception("Le fichier specifie n'existe pas ou est introuvable");
55
        }
56 1
        $this->fHandle = fopen($this->path, "r");
57 1
        rewind($this->fHandle);
58 1
        $this->next();
59 1
        $this->line = 0;
60 1
    }
61
62
    /**
63
     * @return mixed
64
     */
65 1
    public function current()
66
    {
67 1
        return $this->current;
68
    }
69
70 1
    public function next()
71
    {
72 1
        if (feof($this->fHandle)) {
73 1
            fclose($this->fHandle);
74 1
            $this->current = null;
75
        } else {
76 1
            $this->current = fgetcsv($this->fHandle, 0, $this->getSeparator());
77
        }
78 1
        $this->line++;
79 1
    }
80
81
    /**
82
     * @return bool
83
     */
84 1
    public function valid()
85
    {
86 1
        return !is_null($this->current());
87
    }
88
89
    /**
90
     * Get separator
91
     * @return string
92
     */
93 2
    public function getSeparator()
94
    {
95 2
        return $this->separator;
96
    }
97
98
    /**
99
     * Set separator
100
     * @param string $separator
101
     * @return self
102
     */
103 3
    public function setSeparator($separator)
104
    {
105 3
        $this->separator = (string)$separator;
106 3
        return $this;
107
    }
108
109 1
    public function key()
110
    {
111 1
        return $this->line;
112
    }
113
}
114