Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BYSETPOS to RecurrenceRule #113

Merged
merged 4 commits into from
Oct 10, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions src/Property/Event/RecurrenceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class RecurrenceRule implements ValueInterface
*/
protected $freq = self::FREQ_YEARLY;

/**
* BYSETPOS must require use of other BY*.
*
* @var bool
*/
protected $canUseBySetPos = false;

/**
* @var null|int
*/
Expand All @@ -65,6 +72,11 @@ class RecurrenceRule implements ValueInterface
*/
protected $wkst;

/**
* @var null|array
*/
protected $bySetPos = null;

/**
* @var null|string
*/
Expand Down Expand Up @@ -135,6 +147,10 @@ protected function buildParameterBag()
$parameterBag->setParam('WKST', $this->wkst);
}

if (null !== $this->bySetPos && $this->canUseBySetPos) {
$parameterBag->setParam('BYSETPOS', $this->bySetPos);
}

if (null !== $this->byMonth) {
$parameterBag->setParam('BYMONTH', explode(',', $this->byMonth));
}
Expand Down Expand Up @@ -286,6 +302,69 @@ public function setWkst($value)
return $this;
}

/**
* The BYSETPOS filters one interval of events by the specified position.
* A positive position will start from the beginning and go forward while
* a negative position will start at the end and move backward.
*
* Valid values are a comma separated string or an array of integers
* from 1 to 366 or negative integers from -1 to -366.
*
* @param null|int|string|array $value
*
* @throws InvalidArgumentException
*
* @return $this
*/
public function setBySetPos($value)
{
if (null === $value) {
$this->bySetPos = $value;

return $this;
}

if (!(is_string($value) || is_array($value) || is_int($value))) {
throw new InvalidArgumentException('Invalid value for BYSETPOS');
}

$list = $value;

if (is_int($value)) {
if ($value === 0 || $value < -366 || $value > 366) {
throw new InvalidArgumentException('Invalid value for BYSETPOS');
}
$this->bySetPos = [$value];

return $this;
}

if (is_string($value)) {
$list = explode(',', $value);
}

$output = [];

foreach ($list as $item) {
if (is_string($item)) {
if (!preg_match('/^ *-?[0-9]* *$/', $item)) {
throw new InvalidArgumentException('Invalid value for BYSETPOS');
}
$item = intval($item);
}

if (!is_int($item) || $item === 0 || $item < -366 || $item > 366) {
throw new InvalidArgumentException('Invalid value for BYSETPOS');
}

$output[] = $item;
}

$this->bySetPos = $output;

return $this;
}

/**
* The BYMONTH rule part specifies a COMMA-separated list of months of the year.
* Valid values are 1 to 12.
Expand All @@ -304,6 +383,8 @@ public function setByMonth($month)

$this->byMonth = $month;

$this->canUseBySetPos = true;

return $this;
}

Expand All @@ -319,6 +400,8 @@ public function setByWeekNo($value)
{
$this->byWeekNo = $value;

$this->canUseBySetPos = true;

return $this;
}

Expand All @@ -334,6 +417,8 @@ public function setByYearDay($day)
{
$this->byYearDay = $day;

$this->canUseBySetPos = true;

return $this;
}

Expand All @@ -349,6 +434,8 @@ public function setByMonthDay($day)
{
$this->byMonthDay = $day;

$this->canUseBySetPos = true;

return $this;
}

Expand All @@ -369,6 +456,8 @@ public function setByDay(string $day)
{
$this->byDay = $day;

$this->canUseBySetPos = true;

return $this;
}

Expand All @@ -390,6 +479,8 @@ public function setByHour($value)

$this->byHour = $value;

$this->canUseBySetPos = true;

return $this;
}

Expand All @@ -411,6 +502,8 @@ public function setByMinute($value)

$this->byMinute = $value;

$this->canUseBySetPos = true;

return $this;
}

Expand All @@ -432,6 +525,8 @@ public function setBySecond($value)

$this->bySecond = $value;

$this->canUseBySetPos = true;

return $this;
}
}