mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +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() {
|
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,
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue