1
|
|
|
<?php |
2
|
|
|
namespace FMUP\Import\Iterator; |
3
|
|
|
|
4
|
|
|
use FMUP\Import\Config; |
5
|
|
|
use FMUP\Import\Config\ConfigObjet; |
6
|
|
|
use FMUP\Import\Exception; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Valide une ligne et compte le nombre de ligne MAJ ou CRÉÉ |
10
|
|
|
* |
11
|
|
|
* @author csanz |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
class ValidatorIterator extends \IteratorIterator |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Si la ligne est validée |
18
|
|
|
* |
19
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
private $valid; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Si l'import va réaliser uniquement des insert, le type sera "insert" sinon "update" |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $lineType; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @var integer |
33
|
|
|
*/ |
34
|
|
|
private $totalInsert = 0; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @var Integer |
39
|
|
|
*/ |
40
|
|
|
private $totalUpdate = 0; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* |
44
|
|
|
* @var integer |
45
|
|
|
*/ |
46
|
|
|
private $totalErrors = 0; |
47
|
|
|
|
48
|
|
|
/* |
49
|
|
|
* *************************** |
50
|
|
|
* GETTERS |
51
|
|
|
* *************************** |
52
|
|
|
*/ |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
1 |
|
public function getValid() |
59
|
|
|
{ |
60
|
1 |
|
return (bool)$this->valid; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
1 |
|
public function getType() |
68
|
|
|
{ |
69
|
1 |
|
return $this->lineType; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* |
74
|
|
|
* @return int |
75
|
|
|
*/ |
76
|
1 |
|
public function getTotalUpdate() |
77
|
|
|
{ |
78
|
1 |
|
return (int)$this->totalUpdate; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* |
83
|
|
|
* @return int |
84
|
|
|
*/ |
85
|
1 |
|
public function getTotalInsert() |
86
|
|
|
{ |
87
|
1 |
|
return (int)$this->totalInsert; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* |
92
|
|
|
* @return int |
93
|
|
|
*/ |
94
|
1 |
|
public function getTotalErrors() |
95
|
|
|
{ |
96
|
1 |
|
return (int)$this->totalErrors; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return mixed |
101
|
|
|
* @throws Exception |
102
|
|
|
*/ |
103
|
2 |
|
public function current() |
104
|
|
|
{ |
105
|
2 |
|
$current = $this->getInnerIterator()->current(); |
106
|
2 |
|
if (!$current instanceof Config) { |
107
|
1 |
|
throw new Exception('Iterator can only validate Config'); |
108
|
|
|
} |
109
|
1 |
|
$this->valid = $current->validateLine(); |
110
|
1 |
|
$type = ""; |
111
|
1 |
|
foreach ($current->getListeConfigObjet() as $configObject) { |
112
|
1 |
|
$status = $configObject->getStatut(); |
113
|
1 |
|
if ($status == ConfigObjet::INSERT) { |
114
|
1 |
|
$type = ($type == ConfigObjet::UPDATE ? ConfigObjet::UPDATE : ConfigObjet::INSERT); |
115
|
1 |
|
} elseif ($status == ConfigObjet::UPDATE) { |
116
|
1 |
|
$type = ConfigObjet::UPDATE; |
117
|
|
|
} |
118
|
|
|
} |
119
|
1 |
|
if ($this->valid && !$current->getDoublonLigne()) { |
120
|
1 |
|
$this->increaseCounter($type); |
121
|
|
|
} else { |
122
|
1 |
|
$this->totalErrors++; |
123
|
|
|
} |
124
|
1 |
|
$this->lineType = $type; |
125
|
1 |
|
return $current; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Increase update/insert counter depending on type |
130
|
|
|
* @param string $type |
131
|
|
|
* @return $this |
132
|
|
|
*/ |
133
|
1 |
|
protected function increaseCounter($type) |
134
|
|
|
{ |
135
|
1 |
|
if ($type == ConfigObjet::INSERT) { |
136
|
1 |
|
$this->totalInsert++; |
137
|
1 |
|
} elseif ($type == ConfigObjet::UPDATE) { |
138
|
1 |
|
$this->totalUpdate++; |
139
|
|
|
} |
140
|
1 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|