[ticket/14944] Add possibility to search for template loop indexes by key

Adds a new function to the template interface, and implements it in the
context class.  The function returns the ordinal index for a specified key,
with the same structure that the key for alter_block_array.
Reuses same code. Remove unneeded references, do nothing for int keys.
Check out of bounds or wrong blockname errors.  Added tests.
Remove default parameter value.

PHPBB3-14944
This commit is contained in:
javiexin 2017-01-28 21:34:08 +01:00
parent 20c03cccdd
commit 849fd9df7d
4 changed files with 5 additions and 5 deletions

View file

@ -135,7 +135,7 @@ abstract class base implements template
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function find_key_index($blockname, $key = false) public function find_key_index($blockname, $key)
{ {
return $this->context->find_key_index($blockname, $key); return $this->context->find_key_index($blockname, $key);
} }

View file

@ -278,7 +278,7 @@ class context
* *
* @return mixed false if not found, index position otherwise; be sure to test with === * @return mixed false if not found, index position otherwise; be sure to test with ===
*/ */
public function find_key_index($blockname, $key = false) public function find_key_index($blockname, $key)
{ {
// For nested block, $blockcount > 0, for top-level block, $blockcount == 0 // For nested block, $blockcount > 0, for top-level block, $blockcount == 0
$blocks = explode('.', $blockname); $blocks = explode('.', $blockname);

View file

@ -187,7 +187,7 @@ interface template
* *
* @return mixed false if not found, index position otherwise; be sure to test with === * @return mixed false if not found, index position otherwise; be sure to test with ===
*/ */
public function find_key_index($blockname, $key = false); public function find_key_index($blockname, $key);
/** /**
* Get path to template for handle (required for BBCode parser) * Get path to template for handle (required for BBCode parser)

View file

@ -637,8 +637,8 @@ EOT
$this->assertEquals(1, $this->template->find_key_index('outer[2].middle', array('VARIABLE' => '2B')), 'Find index by key in middle loop'); $this->assertEquals(1, $this->template->find_key_index('outer[2].middle', array('VARIABLE' => '2B')), 'Find index by key in middle loop');
$this->assertEquals(2, $this->template->find_key_index('outer.middle', true), 'Find index at the end of middle loop'); $this->assertEquals(2, $this->template->find_key_index('outer.middle', true), 'Find index at the end of middle loop');
$this->assertEquals(false, $this->template->find_key_index('outer.wrong'), 'Wrong middle block name'); $this->assertEquals(false, $this->template->find_key_index('outer.wrong', true), 'Wrong middle block name');
$this->assertEquals(false, $this->template->find_key_index('wrong.middle'), 'Wrong outer block name'); $this->assertEquals(false, $this->template->find_key_index('wrong.middle', false), 'Wrong outer block name');
} }
public function assign_block_vars_array_data() public function assign_block_vars_array_data()
{ {