Skip to content

Instantly share code, notes, and snippets.

@simensen
Created August 1, 2014 02:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save simensen/7b8f1d7feb7f803b7d62 to your computer and use it in GitHub Desktop.
<?php
namespace FooBar\Domain\Model\TaskTemplate;
use JsonSerializable;
use FooBar\Shared\JsonDeserializable;
use FooBar\Domain\Model\Recurrence\Interval;
use FooBar\Domain\Model\Recurrence\Recurrence;
class TaskRecurrence implements JsonSerializable, JsonDeserializable
{
private $recurrence;
private $splitIntervals = [];
private $timeBox;
private $interval;
private $completed;
private $onMonday;
private $onTuesday;
private $onWednesday;
private $onThursday;
private $onFriday;
private $onSaturday;
private $onSunday;
private function __construct(Recurrence $recurrence)
{
$this->recurrence = $recurrence;
}
public static function forRecurrence(Recurrence $recurrence)
{
return new static($recurrence);
}
public function getRecurrence()
{
return $this->recurrence;
}
public function withSplitIntervals(array $splitIntervals = [])
{
$clone = clone($this);
$clone->splitIntervals = $splitIntervals;
return $clone;
}
public function getSplitIntervals()
{
return $this->splitIntervals;
}
public function withTimeBox(TimeBox $timeBox)
{
$clone = clone($this);
$clone->timeBox = $timeBox;
return $clone;
}
public function getTimeBox()
{
return $this->timeBox;
}
public function withInterval(Interval $interval)
{
$clone = clone($this);
$clone->interval = $interval;
return $clone;
}
public function getInterval()
{
return $this->interval;
}
public function mustBe(Completed $completed)
{
$clone = clone($this);
$clone->completed = $completed;
return $clone;
}
public function getCompleted()
{
return $this->completed;
}
public function recurOnMonday()
{
$clone = clone($this);
$clone->onMonday = true;
return $clone;
}
public function doNotRecurOnMonday()
{
$clone = clone($this);
$clone->onMonday = false;
return $clone;
}
public function getOnMonday()
{
return $this->onMonday;
}
public function recurOnTuesday()
{
$clone = clone($this);
$clone->onTuesday = true;
return $clone;
}
public function doNotRecurOnTuesday()
{
$clone = clone($this);
$clone->onTuesday = false;
return $clone;
}
public function getOnTuesday()
{
return $this->onTuesday;
}
public function recurOnWednesday()
{
$clone = clone($this);
$clone->onWednesday = true;
return $clone;
}
public function doNotRecurOnWednesday()
{
$clone = clone($this);
$clone->onWednesday = false;
return $clone;
}
public function getOnWednesday()
{
return $this->onWednesday;
}
public function recurOnThursday()
{
$clone = clone($this);
$clone->onThursday = true;
return $clone;
}
public function doNotRecurOnThursday()
{
$clone = clone($this);
$clone->onThursday = false;
return $clone;
}
public function getOnThursday()
{
return $this->onThursday;
}
public function recurOnFriday()
{
$clone = clone($this);
$clone->onFriday = true;
return $clone;
}
public function doNotRecurOnFriday()
{
$clone = clone($this);
$clone->onFriday = false;
return $clone;
}
public function getOnFriday()
{
return $this->onFriday;
}
public function recurOnSaturday()
{
$clone = clone($this);
$clone->onSaturday = true;
return $clone;
}
public function doNotRecurOnSaturday()
{
$clone = clone($this);
$clone->onSaturday = false;
return $clone;
}
public function getOnSaturday()
{
return $this->onSaturday;
}
public function recurOnSunday()
{
$clone = clone($this);
$clone->onSunday = true;
return $clone;
}
public function doNotRecurOnSunday()
{
$clone = clone($this);
$clone->onSunday = false;
return $clone;
}
public function getOnSunday()
{
return $this->onSunday;
}
public function jsonSerialize()
{
return [
'recurrence' => $this->recurrence,
'splitIntervals' => $this->splitIntervals,
'timeBox' => $this->timeBox,
'interval' => $this->interval,
'completed' => $this->completed,
'onMonday' => $this->onMonday,
'onTuesday' => $this->onTuesday,
'onWednesday' => $this->onWednesday,
'onThursday' => $this->onThursday,
'onFriday' => $this->onFriday,
'onSaturday' => $this->onSaturday,
'onSunday' => $this->onSunday,
];
}
public static function jsonDeserialize($data)
{
$instance = new static(
Recurrence::jsonDeserialize($data['recurrence'])
);
$instance->splitIntervals = array_map(function ($splitInterval) {
return SplitInterval::jsonDeserialize($splitInterval);
}, $data['splitIntervals']);
$instance->timeBox = $data['timeBox'] ? TimeBox::jsonDeserialize($data['timeBox']) : null;
$instance->interval = $data['interval'] ? Interval::jsonDeserialize($data['interval']) : null;
$instance->completed = $data['completed'] ? Completed::jsonDeserialize($data['completed']) : null;
$instance->onMonday = $data['onMonday'];
$instance->onTuesday = $data['onTuesday'];
$instance->onWednesday = $data['onWednesday'];
$instance->onThursday = $data['onThursday'];
$instance->onFriday = $data['onFriday'];
$instance->onSaturday = $data['onSaturday'];
$instance->onSunday = $data['onSunday'];
return $instance;
}
}
<?php
$hourlyRecurrence = Recurrence::HOURLY();T
$dailyRecurrence = Recurrence::DAILY();
$weeklyRecurrence = Recurrence::WEEKLY();
$monthlyRecurrence = Recurrence::MONTHLY();
//
// Create an hourly task that happens on Monday and Friday
// between 3:00pm and 5:00pm and 7:00pm and 9:00pm.
//
$hourlyTaskRecurrence = TaskRecurrence::forRecurrence($hourlyRecurrence)
->withSplitIntervals([
SplitInterval::createFromStrings("3:00pm", "5:00pm"),
SplitInterval::createFromStrings("7:00pm", "9:00pm"),
])
->recurOnMonday()
->recurOnFriday();
$taskTemplate->taskRecurs($hourlyTaskRecurrence);
//
// Create a daily task that happens on Monday and Friday
// that is time boxed to 10am-11am.
//
$dailyTaskRecurrence = TaskRecurrence::forRecurrence($dailyRecurrence)
->withTimeBox(TimeBox::createFromStrings("10am", "11am"))
->recurOnMonday()
->recurOnFriday();
$taskTemplate->taskRecurs($dailyTaskRecurrence);
//
// Create a weekly task that happens every three weeks from
// 10am-11am.
//
$weeklyTaskRecurrence = TaskRecurrence::forRecurrence($weeklyRecurrence)
->withTimeBox(TimeBox::createFromStrings("10am", "11am"))
->withInterval(Interval::fromNumber(3));
$taskTemplate->taskRecurs($weeklyTaskRecurrence);
//
// Create a monthly task that happens every five months
// between 10am-11am that needs to be completed within
// 5 days.
//
$monthlyTaskRecurrenceOne = TaskRecurrence::forRecurrence($monthlyRecurrence)
->withTimeBox(TimeBox::createFromStrings("10am", "11am"))
->withInterval(Interval::fromNumber(5))
->mustBe(Completed::by(Designator::DAY(), 5));
$taskTemplate->taskRecurs($monthlyTaskRecurrenceOne);
//
// Create a monthly task that happens every month that
// needs to be completed on each of the 1st, 3rd, and
// 5th days of the month.
//
$monthlyTaskRecurrenceTwo = TaskRecurrence::forRecurrence($monthlyRecurrence)
->mustBe(Completed::onDays([1, 3, 5]));
$taskTemplate->taskRecurs($monthlyTaskRecurrenceTwo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment