Skip to content

Commit

Permalink
Convert iterators into arrays before mucking about with them.
Browse files Browse the repository at this point in the history
$data['options'] might be an iterator. Convert it to an array before
adding empty options or converting to option elements.
  • Loading branch information
markstory committed Jan 8, 2014
1 parent 9a8f6db commit 462be3e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cake/View/Input/SelectBox.php
Expand Up @@ -99,6 +99,10 @@ protected function _renderContent($data) {
$out = [];
$options = $data['options'];

if ($options instanceof Traversable) {
$options = iterator_to_array($options);
}

if (!empty($data['empty'])) {
$value = $data['empty'] === true ? '' : $data['empty'];
$options = ['' => $value] + $options;
Expand Down
24 changes: 24 additions & 0 deletions Test/TestCase/View/Input/SelectBoxTest.php
Expand Up @@ -76,6 +76,30 @@ public function testRenderSimple() {
$this->assertTags($result, $expected);
}

/**
* test simple iterator rendering
*
* @return void
*/
public function testRenderSimpleIterator() {
$select = new SelectBox($this->templates);
$options = new \ArrayObject(['a' => 'Albatross', 'b' => 'Budgie']);
$data = [
'name' => 'Birds[name]',
'options' => $options,
'empty' => true
];
$result = $select->render($data);
$expected = [
'select' => ['name' => 'Birds[name]'],
['option' => ['value' => '']], '/option',
['option' => ['value' => 'a']], 'Albatross', '/option',
['option' => ['value' => 'b']], 'Budgie', '/option',
'/select'
];
$this->assertTags($result, $expected);
}

/**
* test complex option rendering
*
Expand Down

0 comments on commit 462be3e

Please sign in to comment.