2011-07-20 19 views
1

ループのコードは次のとおりです。クラスでは、私がここでIteratorインターフェイス を実装に問題がある

class User_Model_Users implements Iterator, Countable 
{ 

    protected $_count; 
    protected $_gateway; 
    protected $_resultSet; 

    public function __construct($results, $gateway) 
    { 
     $this->setGateway($gateway); 
     $this->_resultSet = $results; 
    } 

    public function setGateway(User_Model_UserGateway $gateway) 
    { 
     $this->_gateway = $gateway; 
     return $this; 
    } 

    public function getGateway() 
    { 
     return $this->_gateway; 
    } 

    public function count() 
    { 
     if (null === $this->_count) { 
      $this->_count = count($this->_resultSet); 
     } 
     return $this->_count; 
    } 

    public function current() 
    { 
     if ($this->_resultSet instanceof Iterator) { 
      $key = $this->_resultSet->key(); 
     } else { 
      $key = key($this->_resultSet); 
     } 
     $result = $this->_resultSet [$key]; 
     if (!$result instanceof User_Model_User) { 
      $gateway = $this->getGateway(); 
      $result = $gateway->createUser($result); 
      $this->_resultSet [$key] = $result; 
     } 
     return $result; 
    } 

    public function key() 
    { 
     return key($this->_resultSet); 
    } 

    public function next() 
    { 
     return next($this->_resultSet); 
    } 

    public function rewind() 
    { 
     return reset($this->_resultSet); 
    } 

    public function valid() 
    { 
     return (bool) $this->current(); 
    } 

} 

$結果として、私ははZend_Db_Table_Rowsetを提供しますが、それはまた、他のオブジェクトや配列にすることができます。 foreachループで動作するようにこのコードを修正するにはどうすればよいですか? 無限ループなのでエラーはありません。

+1

「はまた、他のオブジェクトまたは配列とすることができる」 - この場合の例スクリプト+データと期待される結果はいいだろう。 – VolkerK

+0

for this Zend_Db_Table_Rowsetでは動作しません。私はこの場合を解決したいと思います – greg606

+0

具体的にあなたの質問は何ですか?修正が必要だと思いますか?どのエラーが出ますか? 「うまくいかない」とは何がうまくいかないとは言いません。 – hakre

答えて

1

ワイルドな推測(実際にコードに掘り下げていない):の実装以来

()

public function valid() 
{ 
    return (bool) $this->current(); 
} 

あなたは何がある場合)(現在はfalseを返していないことを確認する必要がありますが有効より多くの要素

public function current() 
{ 
    if ($this->_resultSet instanceof Iterator) { 
     $key = $this->_resultSet->key(); 
    } else { 
     $key = key($this->_resultSet); 
    } 

    if (is_null($key) ) { // could also be is_null($key)||false===$key, not sure... 
     return false; 
    } 

    $result = $this->_resultSet[$key]; 
    if (!$result instanceof User_Model_User) { 
     $gateway = $this->getGateway(); 
     $result = $gateway->createUser($result); 
     $this->_resultSet[$key] = $result; 
    } 
    return $result; 
} 

とところで:あなたのファンクションキー()(現在のようなinstanceof Iteratorケースを実装していない)ん。


テストスクリプト:

<?php 
class User_Model_Users implements Iterator, Countable 
{ 
    protected $_count; 
    protected $_gateway; 
    protected $_resultSet; 

    public function __construct($results, $gateway) 
    { 
     $this->setGateway($gateway); 
     $this->_resultSet = $results; 
     $this->_count = null; 
    } 

    public function setGateway(User_Model_UserGateway $gateway) 
    { 
     $this->_gateway = $gateway; 
     return $this; 
    } 

    public function getGateway() 
    { 
     return $this->_gateway; 
    } 

    public function count() 
    { 
     if (is_null($this->_count)) { 
      $this->_count = count($this->_resultSet); 
     } 
     return $this->_count; 
    } 

    public function current() 
    { 
     if ($this->_resultSet instanceof Iterator) { 
      $key = $this->_resultSet->key(); 
     } else { 
      $key = key($this->_resultSet); 
     } 
     if (is_null($key)) { 
      return false; 
     } 

     $result = $this->_resultSet[$key]; 
     if (!$result instanceof User_Model_User) { 
      $gateway = $this->getGateway(); 
      $result = $gateway->createUser($result); 
      $this->_resultSet[$key] = $result; 
     } 
     return $result; 
    } 

    public function key() 
    { 
     return key($this->_resultSet); 
    } 

    public function next() 
    { 
     return next($this->_resultSet); 
    } 

    public function rewind() 
    { 
     return reset($this->_resultSet); 
    } 

    public function valid() 
    { 
     return (bool) $this->current(); 
    } 
} 

class User_Model_User { 
    protected $x, $y; 

    public function __construct($x, $y) { 
     $this->x = $x; 
     $this->y = $y; 
    } 
} 

class User_Model_UserGateway { 
    protected $id; 
    public function __construct() { 
     $this->id = time(); 
    } 

    public function createUser($x) { 
     echo "User_Model_UserGateway::createUser($x)\n"; 
     return new User_Model_User($x, $this->id); 
    } 
} 

$a = array('a', 'b', 'c', 'd'); 
$users = new User_Model_Users($a, new User_Model_UserGateway); 
foreach($users as $u) { 
    print_r($u); 
} 

プリント

User_Model_UserGateway::createUser(a) 
User_Model_User Object 
(
    [x:protected] => a 
    [y:protected] => 1311167311 
) 
User_Model_UserGateway::createUser(b) 
User_Model_User Object 
(
    [x:protected] => b 
    [y:protected] => 1311167311 
) 
User_Model_UserGateway::createUser(c) 
User_Model_User Object 
(
    [x:protected] => c 
    [y:protected] => 1311167311 
) 
User_Model_UserGateway::createUser(d) 
User_Model_User Object 
(
    [x:protected] => d 
    [y:protected] => 1311167311 
) 
+0

残念ながら、これは動作しません。 – greg606

+0

@ greg606その後、単純なテストスクリプトがそうでないと言うので、例のスクリプトが本当に必要です;-) – VolkerK

+0

見てください:https://github.com/greg606/zf-core – greg606

関連する問題