Taskwarrior/src/Recurring.php

91 lines
1.8 KiB
PHP
Raw Normal View History

2015-02-08 07:00:15 -06:00
<?php
/**
*
*/
namespace DavidBadura\Taskwarrior;
2015-04-06 16:13:02 -05:00
use DavidBadura\Taskwarrior\Exception\RecurringParseException;
2015-02-08 07:00:15 -06:00
/**
* @author David Badura <d.a.badura@gmail.com>
*/
class Recurring
{
2015-04-06 16:13:02 -05:00
const DAILY = 'daily';
const WEEKDAYS = 'weekdays';
const WEEKLY = 'weekly';
const BIWEEKLY = 'biweekly';
const QUARTERLY = 'quarterly';
2015-02-08 07:00:15 -06:00
const SEMIANNUAL = 'semiannual';
2015-04-06 16:13:02 -05:00
const ANNUAL = 'annual';
const YEARLY = 'yearly';
const BIANNUAL = 'biannual';
const BIYEARLY = 'biyearly';
2015-02-08 07:00:15 -06:00
/**
* @var string
*/
private $recurring;
/**
* @param string $recurring
2015-04-06 16:13:02 -05:00
* @throws RecurringParseException
2015-02-08 07:00:15 -06:00
*/
public function __construct($recurring)
{
2015-04-06 16:13:02 -05:00
if (!self::isValid($recurring)) {
throw new RecurringParseException(sprintf('recurring "%s" is not valid', $recurring));
2015-02-08 07:00:15 -06:00
}
2015-04-06 16:13:02 -05:00
$this->recurring = $recurring;
2015-02-08 07:00:15 -06:00
}
2015-02-08 09:40:37 -06:00
/**
* @return string
*/
public function getValue()
{
return $this->recurring;
}
2015-02-08 07:00:15 -06:00
/**
* @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
{
2015-04-06 16:13:02 -05:00
$refClass = new \ReflectionClass(__CLASS__);
2015-02-08 07:00:15 -06:00
$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;
}
}