1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_ImportExport
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * CSV import adapter
29: *
30: * @category Mage
31: * @package Mage_ImportExport
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_ImportExport_Model_Import_Adapter_Csv extends Mage_ImportExport_Model_Import_Adapter_Abstract
35: {
36: /**
37: * Field delimiter.
38: *
39: * @var string
40: */
41: protected $_delimiter = ',';
42:
43: /**
44: * Field enclosure character.
45: *
46: * @var string
47: */
48: protected $_enclosure = '"';
49:
50: /**
51: * Source file handler.
52: *
53: * @var resource
54: */
55: protected $_fileHandler;
56:
57: /**
58: * Object destructor.
59: *
60: * @return void
61: */
62: public function __destruct()
63: {
64: if (is_resource($this->_fileHandler)) {
65: fclose($this->_fileHandler);
66: }
67: }
68:
69: /**
70: * Method called as last step of object instance creation. Can be overrided in child classes.
71: *
72: * @return Mage_ImportExport_Model_Import_Adapter_Abstract
73: */
74: protected function _init()
75: {
76: $this->_fileHandler = fopen($this->_source, 'r');
77: $this->rewind();
78: return $this;
79: }
80:
81: /**
82: * Move forward to next element
83: *
84: * @return void Any returned value is ignored.
85: */
86: public function next()
87: {
88: $this->_currentRow = fgetcsv($this->_fileHandler, null, $this->_delimiter, $this->_enclosure);
89: $this->_currentKey = $this->_currentRow ? $this->_currentKey + 1 : null;
90: }
91:
92: /**
93: * Rewind the Iterator to the first element.
94: *
95: * @return void Any returned value is ignored.
96: */
97: public function rewind()
98: {
99: // rewind resource, reset column names, read first row as current
100: rewind($this->_fileHandler);
101: $this->_colNames = fgetcsv($this->_fileHandler, null, $this->_delimiter, $this->_enclosure);
102: $this->_currentRow = fgetcsv($this->_fileHandler, null, $this->_delimiter, $this->_enclosure);
103:
104: if ($this->_currentRow) {
105: $this->_currentKey = 0;
106: }
107: }
108:
109: /**
110: * Seeks to a position.
111: *
112: * @param int $position The position to seek to.
113: * @throws OutOfBoundsException
114: * @return void
115: */
116: public function seek($position)
117: {
118: if ($position != $this->_currentKey) {
119: if (0 == $position) {
120: return $this->rewind();
121: } elseif ($position > 0) {
122: if ($position < $this->_currentKey) {
123: $this->rewind();
124: }
125: while ($this->_currentRow = fgetcsv($this->_fileHandler, null, $this->_delimiter, $this->_enclosure)) {
126: if (++ $this->_currentKey == $position) {
127: return;
128: }
129: }
130: }
131: throw new OutOfBoundsException(Mage::helper('importexport')->__('Invalid seek position'));
132: }
133: }
134: }
135: