-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseReader.php
76 lines (67 loc) · 1.33 KB
/
BaseReader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace boundstate\importexport;
/**
* Reads files.
* @package boundstate\importexport
*/
abstract class BaseReader extends \yii\base\Object
{
/**
* @var ImportInterface|string importer instance or class name
*/
public $destination;
/**
* @var array data
*/
public $rows = [];
/**
* @var array import errors
*/
private $_errors = [];
/**
* @inheritdoc
*/
public function init()
{
if (!$this->destination) {
throw new \yii\base\InvalidConfigException('The "destination" property must be set.');
}
if (is_string($this->destination)) {
$this->destination = new $this->destination;
}
}
/**
* Adds an error.
* @param integer $row
* @param mixed $message
*/
public function addError($row, $message) {
$this->_errors[] = ['row'=>$row, 'message'=>$message];
}
/**
* @return array row errors
*/
public function getErrors() {
return $this->_errors;
}
/**
* Imports data via the configured importer.
* @param string $filename
* @return bool
*/
public function import($filename) {
$this->_errors = [];
$this->read($filename);
foreach ($this->rows as $i => $row) {
if (!$this->destination->import($this, $i, $row)) {
return false;
}
}
return true;
}
/**
* Reads from a file.
* @param string $filename
*/
protected abstract function read($filename);
}