Skip to content

Instantly share code, notes, and snippets.

@simensen
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simensen/67139c0521c5495a799c to your computer and use it in GitHub Desktop.
Save simensen/67139c0521c5495a799c to your computer and use it in GitHub Desktop.
<?php
//
// Create a monthly task that happens every five months
// and needs to be completed within 5 days.
//
// uses the 'by' "constructor" for Completed
$taskRecurrence = TaskRecurrence::forRecurrence(Recurrence::MONTHLY())
->withInterval(Interval::fromNumber(5))
->mustBe(Completed::by(Designator::DAY(), 5));
//
// 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.
//
// uses the 'onDays' "constructor" for Completed
$taskRecurrence = TaskRecurrence::forRecurrence(Recurrence::MONTHLY())
->mustBe(Completed::onDays([1, 3, 5]));
<?php
class Completed implements JsonSerializable, JsonDeserializable
{
/**
* @var string
*/
private $type;
/**
* @var Designator
*/
private $byDesignator;
/**
* @var int
*/
private $byNumber;
/**
* @var int[]
*/
private $onDays = [];
private function __construct($type)
{
$this->type = $type;
}
/**
* @param Designator $designator
* @param $number
* @return static
*/
public static function by(Designator $designator, $number)
{
$instance = new static('by');
$instance->byDesignator = $designator;
$instance->byNumber = $number;
return $instance;
}
/**
* @param int[] $days
* @return static
*/
public static function onDays(array $days)
{
$instance = new static('on');
$instance->onDays = $days;
return $instance;
}
/**
* @return static
*/
public static function anytime()
{
return new static('anytime');
}
/**
* @return bool
*/
public function isBy()
{
return 'by' === $this->type;
}
/**
* @return bool
*/
public function isOnDays()
{
return 'on' === $this->type;
}
/**
* @return bool
*/
public function isAnytime()
{
return 'anytime' === $this->type;
}
/**
* @return Designator
*/
public function getByDesignator()
{
return $this->byDesignator;
}
/**
* @return int
*/
public function getByNumber()
{
return $this->byNumber;
}
/**
* @return \int[]
*/
public function getOnDays()
{
return $this->onDays;
}
/**
* @return array|mixed
*/
public function jsonSerialize()
{
return [
'type' => $this->type,
'byNumber' => $this->byNumber,
'byDesignator' => $this->byDesignator,
'onDays' => $this->onDays,
];
}
/**
* @param $data
* @return static
*/
public static function jsonDeserialize($data)
{
$instance = new static($data['type']);
$instance->byNumber = $data['byNumber'];
$instance->byDesignator = Designator::jsonDeserialize($data['byDesignator']);
$instance->onDays = $data['onDays'];
return $instance;
}
}
<?php
class Designator implements JsonSerializable, JsonDeserializable
{
private $name;
private function __construct($name)
{
$this->name = $name;
}
public function __toString()
{
return (string) $this->name;
}
public static function fromString($string)
{
return new static(strtoupper($string));
}
public static function DAY()
{
return static::fromString('DAY');
}
public static function WEEK()
{
return static::fromString('WEEK');
}
public static function YEAR()
{
return static::fromString('YEAR');
}
public function equals(Designator $other)
{
return $this->name === $other->name;
}
public function jsonSerialize()
{
return $this->name;
}
public static function jsonDeserialize($data)
{
return new static($data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment