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_Core
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: * Active record implementation
29: *
30: * @author Magento Core Team <core@magentocommerce.com>
31: */
32: class Mage_Core_Model_Resource_Iterator extends Varien_Object
33: {
34: /**
35: * Walk over records fetched from query one by one using callback function
36: *
37: * @param Zend_Db_Statement_Interface|Zend_Db_Select|string $query
38: * @param array|string $callbacks
39: * @param array $args
40: * @param Varien_Db_Adapter_Interface $adapter
41: * @return Mage_Core_Model_Resource_Iterator
42: */
43: public function walk($query, array $callbacks, array $args=array(), $adapter = null)
44: {
45: $stmt = $this->_getStatement($query, $adapter);
46: $args['idx'] = 0;
47: while ($row = $stmt->fetch()) {
48: $args['row'] = $row;
49: foreach ($callbacks as $callback) {
50: $result = call_user_func($callback, $args);
51: if (!empty($result)) {
52: $args = array_merge($args, $result);
53: }
54: }
55: $args['idx']++;
56: }
57:
58: return $this;
59: }
60:
61: /**
62: * Fetch Zend statement instance
63: *
64: * @param Zend_Db_Statement_Interface|Zend_Db_Select|string $query
65: * @param Zend_Db_Adapter_Abstract $conn
66: * @return Zend_Db_Statement_Interface
67: * @throws Mage_Core_Exception
68: */
69: protected function _getStatement($query, $conn = null)
70: {
71: if ($query instanceof Zend_Db_Statement_Interface) {
72: return $query;
73: }
74:
75: if ($query instanceof Zend_Db_Select) {
76: return $query->query();
77: }
78:
79: if (is_string($query)) {
80: if (!$conn instanceof Zend_Db_Adapter_Abstract) {
81: Mage::throwException(Mage::helper('core')->__('Invalid connection'));
82: }
83: return $conn->query($query);
84: }
85:
86: Mage::throwException(Mage::helper('core')->__('Invalid query'));
87: }
88: }
89: