Re-factor element generation.

Add the ability for arbitrary attributes.
Use built-in methods for attribute generation.
Add tests for element generation.
This commit is contained in:
mark_story 2012-05-12 21:16:57 -04:00
parent 4f12f254f4
commit a80a7dadf8
3 changed files with 35 additions and 20 deletions

View file

@ -119,10 +119,10 @@ class TimeHelperTest extends CakeTestCase {
public function testTimeAgoInWords() { public function testTimeAgoInWords() {
$Time = new TimeHelper($this->View); $Time = new TimeHelper($this->View);
$timestamp = strtotime('+8 years, +4 months +2 weeks +3 days'); $timestamp = strtotime('+8 years, +4 months +2 weeks +3 days');
$result = $Time->timeAgoInWords( $result = $Time->timeAgoInWords($timestamp, array(
$timestamp, 'end' => '1 years',
array('end' => '1 years', 'element' => 'span') 'element' => 'span'
); ));
$expected = array( $expected = array(
'span' => array( 'span' => array(
'title' => $timestamp, 'title' => $timestamp,
@ -133,6 +133,24 @@ class TimeHelperTest extends CakeTestCase {
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
$result = $Time->timeAgoInWords($timestamp, array(
'end' => '1 years',
'element' => array(
'title' => 'testing',
'rel' => 'test'
)
));
$expected = array(
'span' => array(
'title' => 'testing',
'class' => 'time-ago-in-words',
'rel' => 'test'
),
'on ' . date('j/n/y', $timestamp),
'/span'
);
$this->assertTags($result, $expected);
$timestamp = strtotime('+2 weeks'); $timestamp = strtotime('+2 weeks');
$result = $Time->timeAgoInWords( $result = $Time->timeAgoInWords(
$timestamp, $timestamp,

View file

@ -353,34 +353,31 @@ class TimeHelper extends AppHelper {
$stringDate = ''; $stringDate = '';
if (isset($options['element'])) { if (!empty($options['element'])) {
$element_options = array( $element = array(
'tag' => 'span', 'tag' => 'span',
'class' => 'time-ago-in-words', 'class' => 'time-ago-in-words',
'title' => $dateTime 'title' => $dateTime
); );
if (is_array($options['element'])) { if (is_array($options['element'])) {
$element = array_merge($element_options, $options['element']); $element = array_merge($element, $options['element']);
} else { } else {
if ($options['element']) {
$element = $element_options;
$element['tag'] = $options['element']; $element['tag'] = $options['element'];
} else {
$element = null;
} }
unset($options['element']);
} }
}
$relativeDate = $this->_engine->timeAgoInWords($dateTime, $options); $relativeDate = $this->_engine->timeAgoInWords($dateTime, $options);
// Apply HTML element
if ($element) { if ($element) {
$title = isset($element['title']) ? ' title="'.$element['title'].'"' : ''; $relativeDate = sprintf(
$class = isset($element['class']) ? ' class="'.$element['class'].'"' : ''; '<%s%s>%s</%s>',
$relativeDate = '<'.$element['tag'].$title.$class.'>'.$relativeDate.'</'.$element['tag'].'>'; $element['tag'],
$this->_parseAttributes($element, array('tag')),
$relativeDate,
$element['tag']
);
} }
return $relativeDate; return $relativeDate;
} }