Standardizing format string in PaginatorHelper::counter(), %page% becomes {:page}, old keys still work, but will be deprecated

This commit is contained in:
nate 2009-02-07 08:41:15 -05:00
parent 23174203e3
commit c2f111a45a
2 changed files with 25 additions and 17 deletions

View file

@ -390,6 +390,7 @@ class PaginatorHelper extends AppHelper {
* Returns a counter string for the paged result set * Returns a counter string for the paged result set
* *
* @param mixed $options Options for the counter string. See #options for list of keys. * @param mixed $options Options for the counter string. See #options for list of keys.
* @todo See about deprecating the keys in $map for formatting
* @return string Counter string. * @return string Counter string.
*/ */
function counter($options = array()) { function counter($options = array()) {
@ -429,7 +430,7 @@ class PaginatorHelper extends AppHelper {
$out = $paging['page'] . $options['separator'] . $paging['pageCount']; $out = $paging['page'] . $options['separator'] . $paging['pageCount'];
break; break;
default: default:
$replace = array( $map = array(
'%page%' => $paging['page'], '%page%' => $paging['page'],
'%pages%' => $paging['pageCount'], '%pages%' => $paging['pageCount'],
'%current%' => $paging['current'], '%current%' => $paging['current'],
@ -437,7 +438,12 @@ class PaginatorHelper extends AppHelper {
'%start%' => $start, '%start%' => $start,
'%end%' => $end '%end%' => $end
); );
$out = str_replace(array_keys($replace), array_values($replace), $options['format']); $out = str_replace(array_keys($map), array_values($map), $options['format']);
$newKeys = array(
'{:page}', '{:pages}', '{:current}', '{:count}', '{:start}', '{:end}'
);
$out = str_replace($newKeys, array_values($map), $out);
break; break;
} }
return $this->output($out); return $this->output($out);
@ -452,22 +458,17 @@ class PaginatorHelper extends AppHelper {
function numbers($options = array()) { function numbers($options = array()) {
if ($options === true) { if ($options === true) {
$options = array( $options = array(
'before' => ' | ', 'after' => ' | ', 'before' => ' | ', 'after' => ' | ', 'first' => 'first', 'last' => 'last'
'first' => 'first', 'last' => 'last', );
);
} }
$options = array_merge( $defaults = array(
array( 'tag' => 'span', 'before' => null, 'after' => null, 'model' => $this->defaultModel(),
'tag' => 'span', 'modulus' => '8', 'separator' => ' | ', 'first' => null, 'last' => null,
'before'=> null, 'after'=> null, );
'model' => $this->defaultModel(), $options += $defaults;
'modulus' => '8', 'separator' => ' | ',
'first' => null, 'last' => null,
),
(array)$options);
$params = array_merge(array('page'=> 1), (array)$this->params($options['model'])); $params = (array)$this->params($options['model']) + array('page'=> 1);
unset($options['model']); unset($options['model']);
if ($params['pageCount'] <= 1) { if ($params['pageCount'] <= 1) {

View file

@ -739,9 +739,16 @@ class PaginatorTest extends CakeTestCase {
), ),
) )
); );
$input = 'Page %page% of %pages%, showing %current% records out of %count% total, 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%';
$result = $this->Paginator->counter($input);
$expected = 'Page 1 of 5, showing 3 records out of 13 total, starting on record 1, ';
$expected .= 'ending on 3';
$this->assertEqual($result, $expected);
$input = 'Page {:page} of {:pages}, showing {:current} records out of {:count} total, ';
$input .= 'starting on record {:start}, ending on {:end}';
$result = $this->Paginator->counter($input); $result = $this->Paginator->counter($input);
$expected = 'Page 1 of 5, showing 3 records out of 13 total, starting on record 1, ending on 3';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$input = 'Page %page% of %pages%'; $input = 'Page %page% of %pages%';