1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: namespace Avisota\RecipientSource;
16:
17: use Avisota\Recipient\MutableRecipient;
18:
19: 20: 21: 22: 23:
24: class CSVFile implements RecipientSourceInterface
25: {
26: private $file;
27:
28: private $columnAssignment;
29:
30: private $delimiter;
31:
32: private $enclosure;
33:
34: private $escape;
35:
36: 37: 38:
39: public function __construct($file, array $columnAssignment, $delimiter = ',', $enclosure = '"', $escape = '\\')
40: {
41: $this->file = (string) $file;
42: $this->columnAssignment = $columnAssignment;
43: $this->delimiter = $delimiter;
44: $this->enclosure = $enclosure;
45: $this->escape = $escape;
46: }
47:
48: 49: 50: 51: 52:
53: public function countRecipients()
54: {
55: $in = fopen($this->file, 'r');
56:
57: if (!$in) {
58: return 0;
59: }
60:
61: $recipients = 0;
62:
63: $index = array_search('email', $this->columnAssignment);
64:
65: while ($row = fgetcsv($in, 0, $this->delimiter, $this->enclosure, $this->escape)) {
66: if (!empty($row[$index])) {
67: $recipients ++;
68: }
69: }
70:
71: fclose($in);
72:
73: return $recipients;
74: }
75:
76: 77: 78:
79: public function getRecipients($limit = null, $offset = null)
80: {
81: $in = fopen($this->file, 'r');
82:
83: if (!$in) {
84: return null;
85: }
86:
87: $recipients = array();
88:
89: while ($row = fgetcsv($in, 0, $this->delimiter, $this->enclosure, $this->escape)) {
90: $details = array();
91:
92: foreach ($this->columnAssignment as $index => $field) {
93: if (isset($row[$index])) {
94: $details[$field] = $row[$index];
95: }
96: }
97:
98: if (empty($details['email'])) {
99: continue;
100: }
101:
102: $recipients[] = new MutableRecipient($details['email'], $details);
103: }
104:
105: fclose($in);
106:
107: return $recipients;
108: }
109: }
110: