From 8819b85679cf6580805e8bfcd046a4527b1db2e9 Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 26 Sep 2013 22:08:28 -0400 Subject: [PATCH] Convert the counter() method to use StringTemplate --- .../View/Helper/PaginatorHelperTest.php | 16 ++----- Cake/View/Helper/PaginatorHelper.php | 43 ++++++++----------- 2 files changed, 23 insertions(+), 36 deletions(-) diff --git a/Cake/Test/TestCase/View/Helper/PaginatorHelperTest.php b/Cake/Test/TestCase/View/Helper/PaginatorHelperTest.php index 0a4f83bbf08..9685a5174ab 100644 --- a/Cake/Test/TestCase/View/Helper/PaginatorHelperTest.php +++ b/Cake/Test/TestCase/View/Helper/PaginatorHelperTest.php @@ -2011,22 +2011,14 @@ public function testCounter() { 'order' => 'DESC', ) ); - $input = 'Page {:page} of {:pages}, showing {:current} records out of {:count} total, '; - $input .= 'starting on record {:start}, ending on {:end}'; + $input = 'Page {{page}} of {{pages}}, showing {{current}} records out of {{count}} total, '; + $input .= 'starting on record {{start}}, ending on {{end}}'; + $expected = 'Page 1 of 5, showing 3 records out of 13 total, starting on record 1, '; $expected .= 'ending on 3'; $result = $this->Paginator->counter($input); $this->assertEquals($expected, $result); - $input = 'Page {:page} of {:pages}'; - $result = $this->Paginator->counter($input); - $expected = 'Page 1 of 5'; - $this->assertEquals($expected, $result); - - $result = $this->Paginator->counter(array('format' => $input)); - $expected = 'Page 1 of 5'; - $this->assertEquals($expected, $result); - $result = $this->Paginator->counter(array('format' => 'pages')); $expected = '1 of 5'; $this->assertEquals($expected, $result); @@ -2035,7 +2027,7 @@ public function testCounter() { $expected = '1 - 3 of 13'; $this->assertEquals($expected, $result); - $result = $this->Paginator->counter('Showing {:page} of {:pages} {:model}'); + $result = $this->Paginator->counter('Showing {{page}} of {{pages}} {{model}}'); $this->assertEquals('Showing 1 of 5 clients', $result); } diff --git a/Cake/View/Helper/PaginatorHelper.php b/Cake/View/Helper/PaginatorHelper.php index 0cba66ea18f..82233ed1151 100644 --- a/Cake/View/Helper/PaginatorHelper.php +++ b/Cake/View/Helper/PaginatorHelper.php @@ -76,6 +76,8 @@ class PaginatorHelper extends Helper { 'nextDisabled' => '', 'prevActive' => '', 'prevDisabled' => '', + 'counterRange' => '{{start}} - {{end}} of {{count}}', + 'counterPages' => '{{page}} of {{pages}}', ]; /** @@ -554,25 +556,23 @@ public function defaultModel() { * - `model` The model to use, defaults to PaginatorHelper::defaultModel(); * - `format` The format string you want to use, defaults to 'pages' Which generates output like '1 of 5' * set to 'range' to generate output like '1 - 3 of 13'. Can also be set to a custom string, containing - * the following placeholders `{:page}`, `{:pages}`, `{:current}`, `{:count}`, `{:model}`, `{:start}`, `{:end}` and any + * the following placeholders `{{page}}`, `{{pages}}`, `{{current}}`, `{{count}}`, `{{model}}`, `{{start}}`, `{{end}}` and any * custom content you would like. - * - `separator` The separator string to use, default to ' of ' * * @param array $options Options for the counter string. See #options for list of keys. * @return string Counter string. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::counter */ - public function counter($options = array()) { + public function counter($options = []) { if (is_string($options)) { $options = array('format' => $options); } $options = array_merge( - array( + [ 'model' => $this->defaultModel(), 'format' => 'pages', - 'separator' => __d('cake', ' of ') - ), + ], $options); $paging = $this->params($options['model']); @@ -590,28 +590,23 @@ public function counter($options = array()) { switch ($options['format']) { case 'range': - if (!is_array($options['separator'])) { - $options['separator'] = array(' - ', $options['separator']); - } - $out = $start . $options['separator'][0] . $end . $options['separator'][1]; - $out .= $paging['count']; - break; case 'pages': - $out = $paging['page'] . $options['separator'] . $paging['pageCount']; + $template = 'counter' . ucfirst($options['format']); break; default: - $map = array( - '{:page}' => $paging['page'], - '{:pages}' => $paging['pageCount'], - '{:current}' => $paging['current'], - '{:count}' => $paging['count'], - '{:start}' => $start, - '{:end}' => $end, - '{:model}' => strtolower(Inflector::humanize(Inflector::tableize($options['model']))) - ); - $out = str_replace(array_keys($map), array_values($map), $options['format']); + $template = 'counterCustom'; + $this->_templater->add([$template => $options['format']]); } - return $out; + $map = [ + 'page' => $paging['page'], + 'pages' => $paging['pageCount'], + 'current' => $paging['current'], + 'count' => $paging['count'], + 'start' => $start, + 'end' => $end, + 'model' => strtolower(Inflector::humanize(Inflector::tableize($options['model']))) + ]; + return $this->_templater->format($template, $map); } /**