Taskwarrior/src/Recurring.php

81 lines
1.5 KiB
PHP
Raw Normal View History

2015-02-08 07:00:15 -06:00
<?php
/**
*
*/
namespace DavidBadura\Taskwarrior;
/**
* @author David Badura <d.a.badura@gmail.com>
*/
class Recurring
{
const DAILY = 'daily';
const WEEKDAYS = 'weekdays';
const WEEKLY = 'weekly';
const BIWEEKLY = 'biweekly';
const QUARTERLY = 'quarterly';
const SEMIANNUAL = 'semiannual';
const ANNUAL = 'annual';
const YEARLY = 'yearly';
const BIANNUAL = 'biannual';
const BIYEARLY = 'biyearly';
/**
* @var string
*/
private $recurring;
/**
* @param string $recurring
* @throws TaskwarriorException
*/
public function __construct($recurring)
{
2015-02-08 07:29:44 -06:00
if (self::isValid($recurring)) {
2015-02-08 07:00:15 -06:00
$this->recurring = $recurring;
} else {
throw new TaskwarriorException();
}
}
/**
* @return string
*/
public function __toString()
{
return $this->recurring;
}
/**
* @param string $recur
* @return bool
*/
2015-02-08 07:29:44 -06:00
public static function isValid($recur)
2015-02-08 07:00:15 -06:00
{
$refClass = new \ReflectionClass(__CLASS__);
$constants = $refClass->getConstants();
if (in_array($recur, $constants)) {
return true;
}
if (preg_match('/^[0-9]+d$/', $recur)) {
return true;
}
if (preg_match('/^[0-9]+w$/', $recur)) {
return true;
}
if (preg_match('/^[0-9]+q$/', $recur)) {
return true;
}
if (preg_match('/^[0-9]+y$/', $recur)) {
return true;
}
return false;
}
}