Taskwarrior/src/Recurring.php

122 lines
2.6 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';
2015-05-15 06:38:38 -05:00
const MONTHLY = 'monthly';
const BIMONTHLY = 'bimonthly';
2015-04-06 16:13:02 -05:00
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;
}
2015-05-15 06:38:38 -05:00
// seconds
if (preg_match('/^[0-9]*\s?se?c?o?n?d?s?$/', $recur)) {
2015-02-08 07:00:15 -06:00
return true;
}
2015-05-15 06:38:38 -05:00
// minutes
if (preg_match('/^[0-9]*\s?mi?n?u?t?e?s?$/', $recur)) {
2015-02-08 07:00:15 -06:00
return true;
}
2015-05-15 06:38:38 -05:00
// hours
if (preg_match('/^[0-9]*\s?ho?u?r?s?$/', $recur)) {
2015-02-08 07:00:15 -06:00
return true;
}
2015-05-15 06:38:38 -05:00
// days
if (preg_match('/^[0-9]*\s?da?y?s?$/', $recur)) {
return true;
}
// weeks
if (preg_match('/^[0-9]*\s?we?e?k?s?$/', $recur)) {
return true;
}
// months
if (preg_match('/^[0-9]*\s?mo?n?t?h?s?$/', $recur)) {
return true;
}
// quarters
if (preg_match('/^[0-9]*\s?qu?a?r?t?e?r?(s|ly)?/', $recur)) {
return true;
}
// years
if (preg_match('/^[0-9]*\s?ye?a?r?s?$/', $recur)) {
return true;
}
// fortnight | sennight
if (preg_match('/^[0-9]*\s?(fortnight|sennight)$/', $recur)) {
2015-02-08 07:00:15 -06:00
return true;
}
return false;
}
}