1
0
mirror of https://github.com/rlanvin/php-rrule.git synced 2025-02-20 09:54:16 +01:00

Add explicit_infinite and dtstart options to humanReadable

This commit is contained in:
rlanvin 2017-04-11 11:20:20 +01:00
parent 4ca0c62e0a
commit 9b4681793d
3 changed files with 53 additions and 20 deletions

View File

@ -2,6 +2,8 @@
## [Unreleased]
- Add `explicit_infinite` and `dtstart` options to `humanReadable` to respectivity omit "forever" and the start date from the sentence.
## [1.4.2] - 2017-03-29
### Fixed

View File

@ -2375,6 +2375,17 @@ class RRule implements RRuleInterface
* Format a rule in a human readable string
* intl extension is required.
*
* Available options
*
* | Name | Type | Description
* |-------------------|---------|------------
* | `use_intl` | bool | Use the intl extension or not (autodetect)
* | `locale` | string | The locale to use (autodetect)
* | `fallback` | string | Fallback locale if main locale is not found (default en)
* | `date_formatter` | callable| Function used to format the date (takes date, returns formatted)
* | `explicit_inifite`| bool | Mention "forever" if the rule is infinite (true)
* | `dtstart` | bool | Mention the start date (true)
*
* @param array $opt
*
* @return string
@ -2390,6 +2401,8 @@ class RRule implements RRuleInterface
'locale' => null,
'date_formatter' => null,
'fallback' => 'en',
'explicit_infinite' => true,
'dtstart' => true
);
// attempt to detect default locale
@ -2633,14 +2646,18 @@ class RRule implements RRuleInterface
$parts['bysetpos'] = $tmp;
}
// from X
$parts['start'] = strtr($i18n['dtstart'], array(
'%{date}' => $opt['date_formatter']($this->dtstart)
));
if ( $opt['dtstart'] ) {
// from X
$parts['start'] = strtr($i18n['dtstart'], array(
'%{date}' => $opt['date_formatter']($this->dtstart)
));
}
// to X, or N times, or indefinitely
if ( ! $this->until && ! $this->count ) {
$parts['end'] = $i18n['infinite'];
if ( $opt['explicit_infinite'] ) {
$parts['end'] = $i18n['infinite'];
}
}
elseif ( $this->until ) {
$parts['end'] = strtr($i18n['until'], array(
@ -2656,12 +2673,6 @@ class RRule implements RRuleInterface
);
}
// $str = strtr('%{frequency}%{byday}%{start}%{end}', array(
// '%{frequency}' => $parts['frequency'],
// '%{start}' => $parts['start'],
// '%{end}' => $parts['end'],
// '%{byday}' => $parts['byday'],
// ));
$parts = array_filter($parts);
$str = implode('',$parts);
return $str;

View File

@ -2574,29 +2574,49 @@ class RRuleTest extends PHPUnit_Framework_TestCase
{
return array(
array(
"DTSTART:20170202T000000Z\nFREQ=DAILY;UNTIL=20170205T000000Z",
"en",
"DTSTART:20170202T000000Z\nRRULE:FREQ=DAILY;UNTIL=20170205T000000Z",
array('locale' => "en"),
"daily, starting from 2/2/17, until 2/5/17"
),
array(
"DTSTART:20170202T000000Z\nFREQ=DAILY;UNTIL=20170205T000000Z",
"en_IE",
"DTSTART:20170202T000000Z\nRRULE:FREQ=DAILY;UNTIL=20170205T000000Z",
array('locale' => "en_IE"),
"daily, starting from 02/02/2017, until 05/02/2017"
),
array(
"DTSTART;TZID=America/New_York:19970901T090000\nFREQ=DAILY;UNTIL=20170205T000000Z",
"en_IE",
"DTSTART;TZID=America/New_York:19970901T090000\nRRULE:FREQ=DAILY;UNTIL=20170205T000000Z",
array('locale' => "en_IE"),
"daily, starting from 01/09/1997, until 04/02/2017"
)
),
array(
"DTSTART;TZID=America/New_York:19970901T090000\nRRULE:FREQ=DAILY;UNTIL=20170205T000000Z",
array('locale' => "en_IE", 'dtstart' => false),
"daily, until 04/02/2017"
),
array(
"DTSTART;TZID=America/New_York:19970901T090000\nRRULE:FREQ=DAILY",
array('locale' => "en_IE", 'explicit_infinite' => false),
"daily, starting from 01/09/1997"
),
array(
"DTSTART;TZID=America/New_York:19970901T090000\nRRULE:FREQ=YEARLY;INTERVAL=2",
array('locale' => "en_IE", 'explicit_infinite' => false),
"every 2 years, starting from 01/09/1997"
),
array(
"FREQ=DAILY",
array('locale' => "en_IE", 'dtstart' => false, 'explicit_infinite' => false),
"daily"
),
);
}
/**
* @dataProvider humanReadableStrings
*/
public function testHumanReadable($rrule,$locale, $string)
public function testHumanReadable($rrule,$options, $string)
{
$rrule = new RRule($rrule);
$this->assertEquals($string, $rrule->humanReadable(array('locale' => $locale)));
$this->assertEquals($string, $rrule->humanReadable($options));
}
}