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: * Abstract import adapter
29: *
30: * @category Mage
31: * @package Mage_ImportExport
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: abstract class Mage_ImportExport_Model_Import_Adapter_Abstract implements SeekableIterator
35: {
36: /**
37: * Column names array.
38: *
39: * @var array
40: */
41: protected $_colNames;
42:
43: /**
44: * Quantity of columns in first (column names) row.
45: *
46: * @var int
47: */
48: protected $_colQuantity;
49:
50: /**
51: * Current row.
52: *
53: * @var array
54: */
55: protected $_currentRow = null;
56:
57: /**
58: * Current row number.
59: *
60: * @var int
61: */
62: protected $_currentKey = null;
63:
64: /**
65: * Source file path.
66: *
67: * @var string
68: */
69: protected $_source;
70:
71: /**
72: * Adapter object constructor.
73: *
74: * @param string $source Source file path.
75: * @throws Mage_Core_Exception
76: * @return void
77: */
78: final public function __construct($source)
79: {
80: if (!is_string($source)) {
81: Mage::throwException(Mage::helper('importexport')->__('Source file path must be a string'));
82: }
83: if (!is_readable($source)) {
84: Mage::throwException(Mage::helper('importexport')->__("%s file does not exists or is not readable", $source));
85: }
86: $this->_source = $source;
87:
88: $this->_init();
89:
90: // validate column names consistency
91: if (is_array($this->_colNames) && !empty($this->_colNames)) {
92: $this->_colQuantity = count($this->_colNames);
93:
94: if (count(array_unique($this->_colNames)) != $this->_colQuantity) {
95: Mage::throwException(Mage::helper('importexport')->__('Column names have duplicates'));
96: }
97: } else {
98: Mage::throwException(Mage::helper('importexport')->__('Column names is empty or is not an array'));
99: }
100: }
101:
102: /**
103: * Method called as last step of object instance creation. Can be overridden in child classes.
104: *
105: * @return Mage_ImportExport_Model_Import_Adapter_Abstract
106: */
107: protected function _init()
108: {
109: return $this;
110: }
111:
112: /**
113: * Return the current element.
114: *
115: * @return mixed
116: */
117: public function current()
118: {
119: return array_combine(
120: $this->_colNames,
121: count($this->_currentRow) != $this->_colQuantity
122: ? array_pad($this->_currentRow, $this->_colQuantity, '')
123: : $this->_currentRow
124: );
125: }
126:
127: /**
128: * Column names getter.
129: *
130: * @return array
131: */
132: public function getColNames()
133: {
134: return $this->_colNames;
135: }
136:
137: /**
138: * Return the key of the current element.
139: *
140: * @return int More than 0 integer on success, integer 0 on failure.
141: */
142: public function key()
143: {
144: return $this->_currentKey;
145: }
146:
147: /**
148: * Seeks to a position.
149: *
150: * @param int $position The position to seek to.
151: * @return void
152: */
153: public function seek($position)
154: {
155: Mage::throwException(Mage::helper('importexport')->__('Not implemented yet'));
156: }
157:
158: /**
159: * Checks if current position is valid.
160: *
161: * @return boolean Returns true on success or false on failure.
162: */
163: public function valid()
164: {
165: return !empty($this->_currentRow);
166: }
167:
168: /**
169: * Check source file for validity.
170: *
171: * @return Mage_ImportExport_Model_Import_Adapter_Abstract
172: */
173: public function validateSource()
174: {
175: return $this;
176: }
177: }
178: