Skip to content

Commit

Permalink
Convert the counter() method to use StringTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Sep 28, 2013
1 parent c9f8b06 commit 8819b85
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 36 deletions.
16 changes: 4 additions & 12 deletions Cake/Test/TestCase/View/Helper/PaginatorHelperTest.php
Expand Up @@ -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);
Expand All @@ -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);
}

Expand Down
43 changes: 19 additions & 24 deletions Cake/View/Helper/PaginatorHelper.php
Expand Up @@ -76,6 +76,8 @@ class PaginatorHelper extends Helper {
'nextDisabled' => '<li class="next disabled"><span>{{text}}</span></li>',
'prevActive' => '<li class="prev"><a rel="prev" href="{{url}}">{{text}}</a></li>',
'prevDisabled' => '<li class="prev disabled"><span>{{text}}</span></li>',
'counterRange' => '{{start}} - {{end}} of {{count}}',
'counterPages' => '{{page}} of {{pages}}',
];

/**
Expand Down Expand Up @@ -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']);
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit 8819b85

Please sign in to comment.