mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
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:
parent
4f12f254f4
commit
a80a7dadf8
3 changed files with 35 additions and 20 deletions
|
@ -119,10 +119,10 @@ class TimeHelperTest extends CakeTestCase {
|
|||
public function testTimeAgoInWords() {
|
||||
$Time = new TimeHelper($this->View);
|
||||
$timestamp = strtotime('+8 years, +4 months +2 weeks +3 days');
|
||||
$result = $Time->timeAgoInWords(
|
||||
$timestamp,
|
||||
array('end' => '1 years', 'element' => 'span')
|
||||
);
|
||||
$result = $Time->timeAgoInWords($timestamp, array(
|
||||
'end' => '1 years',
|
||||
'element' => 'span'
|
||||
));
|
||||
$expected = array(
|
||||
'span' => array(
|
||||
'title' => $timestamp,
|
||||
|
@ -133,6 +133,24 @@ class TimeHelperTest extends CakeTestCase {
|
|||
);
|
||||
$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');
|
||||
$result = $Time->timeAgoInWords(
|
||||
$timestamp,
|
||||
|
|
|
@ -353,34 +353,31 @@ class TimeHelper extends AppHelper {
|
|||
|
||||
$stringDate = '';
|
||||
|
||||
if (isset($options['element'])) {
|
||||
$element_options = array(
|
||||
if (!empty($options['element'])) {
|
||||
$element = array(
|
||||
'tag' => 'span',
|
||||
'class' => 'time-ago-in-words',
|
||||
'title' => $dateTime
|
||||
);
|
||||
|
||||
if (is_array($options['element'])) {
|
||||
$element = array_merge($element_options, $options['element']);
|
||||
$element = array_merge($element, $options['element']);
|
||||
} else {
|
||||
if ($options['element']) {
|
||||
$element = $element_options;
|
||||
$element['tag'] = $options['element'];
|
||||
} else {
|
||||
$element = null;
|
||||
}
|
||||
unset($options['element']);
|
||||
}
|
||||
}
|
||||
|
||||
$relativeDate = $this->_engine->timeAgoInWords($dateTime, $options);
|
||||
|
||||
// Apply HTML element
|
||||
if ($element) {
|
||||
$title = isset($element['title']) ? ' title="'.$element['title'].'"' : '';
|
||||
$class = isset($element['class']) ? ' class="'.$element['class'].'"' : '';
|
||||
$relativeDate = '<'.$element['tag'].$title.$class.'>'.$relativeDate.'</'.$element['tag'].'>';
|
||||
$relativeDate = sprintf(
|
||||
'<%s%s>%s</%s>',
|
||||
$element['tag'],
|
||||
$this->_parseAttributes($element, array('tag')),
|
||||
$relativeDate,
|
||||
$element['tag']
|
||||
);
|
||||
}
|
||||
|
||||
return $relativeDate;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue