Taskwarrior/src/Task.php

479 lines
7.8 KiB
PHP
Raw Normal View History

2015-02-05 13:41:03 -06:00
<?php
namespace DavidBadura\Taskwarrior;
2015-02-08 05:23:14 -06:00
use Carbon\Carbon;
2015-04-06 16:13:02 -05:00
use DavidBadura\Taskwarrior\Exception\DatetimeParseException;
2015-07-07 18:17:00 -05:00
use Doctrine\Common\Collections\ArrayCollection;
2015-02-05 13:41:03 -06:00
use JMS\Serializer\Annotation as JMS;
/**
* @author David Badura <d.a.badura@gmail.com>
*/
class Task
{
2015-02-05 17:09:29 -06:00
const STATUS_PENDING = 'pending';
2015-02-05 16:00:30 -06:00
const STATUS_COMPLETED = 'completed';
2015-02-05 17:09:29 -06:00
const STATUS_DELETED = 'deleted';
const STATUS_WAITING = 'waiting';
2015-02-08 06:13:53 -06:00
const STATUS_RECURRING = 'recurring';
2015-02-05 16:00:30 -06:00
2015-02-06 11:31:13 -06:00
const PRIORITY_LOW = 'L';
2015-02-06 11:05:29 -06:00
const PRIORITY_MEDIUM = 'M';
2015-02-06 11:31:13 -06:00
const PRIORITY_HIGH = 'H';
2015-02-06 11:05:29 -06:00
2015-02-05 13:41:03 -06:00
/**
* @var string
*
2015-02-08 06:13:53 -06:00
* @JMS\Type("string")
2015-02-05 13:41:03 -06:00
*/
private $uuid;
/**
* @var string
*
2015-02-08 06:13:53 -06:00
* @JMS\Type("string")
2015-02-05 13:41:03 -06:00
*/
private $description;
2015-02-06 11:05:29 -06:00
/**
* @var string
*
2015-02-08 06:13:53 -06:00
* @JMS\Type("string")
2015-02-06 11:05:29 -06:00
*/
private $priority;
2015-02-06 06:08:13 -06:00
/**
* @var string
*
2015-02-08 06:13:53 -06:00
* @JMS\Type("string")
2015-02-06 06:08:13 -06:00
*/
private $project;
2015-02-05 17:09:29 -06:00
/**
2015-02-08 05:23:14 -06:00
* @var Carbon
2015-02-05 17:09:29 -06:00
*
2015-02-08 05:23:14 -06:00
* @JMS\Type("Carbon")
2015-02-05 17:09:29 -06:00
*/
private $due;
2015-02-06 17:22:17 -06:00
/**
2015-02-08 05:23:14 -06:00
* @var Carbon
2015-02-06 17:22:17 -06:00
*
2015-02-08 05:23:14 -06:00
* @JMS\Type("Carbon")
2015-02-06 17:22:17 -06:00
*/
private $wait;
2015-02-06 11:31:13 -06:00
/**
* @var array
*
2015-02-08 06:13:53 -06:00
* @JMS\Type("array<string>")
2015-02-06 11:31:13 -06:00
*/
private $tags;
2015-02-05 17:09:29 -06:00
/**
* @var float
*
2015-02-08 06:13:53 -06:00
* @JMS\Type("float")
2015-02-05 17:09:29 -06:00
*/
private $urgency;
/**
2015-02-08 05:23:14 -06:00
* @var Carbon
2015-02-05 17:09:29 -06:00
*
2015-02-08 05:23:14 -06:00
* @JMS\Type("Carbon")
2015-02-05 17:09:29 -06:00
*/
private $entry;
2015-04-23 14:33:10 -05:00
/**
* @var Carbon
*
* @JMS\Type("Carbon")
*/
private $start;
2015-02-08 06:13:53 -06:00
/**
* @var string
*
2015-02-08 07:00:15 -06:00
* @JMS\Type("Recurring")
2015-02-08 06:13:53 -06:00
*/
private $recur;
/**
* @var Carbon
*
* @JMS\Type("Carbon")
*/
private $until;
2015-02-06 17:22:17 -06:00
/**
2015-02-08 05:23:14 -06:00
* @var Carbon
2015-02-06 17:22:17 -06:00
*
2015-02-08 05:23:14 -06:00
* @JMS\Type("Carbon")
2015-02-06 17:22:17 -06:00
*/
private $modified;
/**
2015-02-08 05:23:14 -06:00
* @var Carbon
2015-02-06 17:22:17 -06:00
*
2015-02-08 05:23:14 -06:00
* @JMS\Type("Carbon")
2015-02-06 17:22:17 -06:00
*/
private $end;
2015-02-05 16:00:30 -06:00
/**
* @var string
*
2015-02-08 06:13:53 -06:00
* @JMS\Type("string")
2015-02-05 16:00:30 -06:00
*/
private $status;
2015-07-07 18:17:00 -05:00
/**
* @var Task[]|ArrayCollection
*
* @JMS\Type("Depends")
*/
private $depends;
2015-02-05 16:00:30 -06:00
/**
*
*/
public function __construct()
{
2015-02-05 17:09:29 -06:00
$this->urgency = 0;
2015-04-22 07:00:47 -05:00
$this->entry = new Carbon('now');
2015-02-05 17:09:29 -06:00
$this->status = self::STATUS_PENDING;
2015-07-07 18:17:00 -05:00
$this->depends = new ArrayCollection();
2015-02-05 16:00:30 -06:00
}
2015-02-05 13:41:03 -06:00
/**
* @return string
*/
public function getUuid()
{
return $this->uuid;
}
/**
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* @param string $description
*/
public function setDescription($description)
{
$this->description = $description;
}
2015-02-05 16:00:30 -06:00
2015-02-06 11:05:29 -06:00
/**
* @return string
*/
public function getPriority()
{
return $this->priority;
}
/**
* @param string $priority
*/
public function setPriority($priority)
{
$this->priority = $priority;
}
2015-02-06 06:08:13 -06:00
/**
* @return string
*/
public function getProject()
{
return $this->project;
}
/**
* @param string $project
*/
public function setProject($project)
{
$this->project = $project;
}
2015-02-05 16:00:30 -06:00
/**
2015-04-23 14:33:10 -05:00
* @return Carbon
2015-02-05 16:00:30 -06:00
*/
2015-02-05 17:09:29 -06:00
public function getDue()
2015-02-05 16:00:30 -06:00
{
2015-02-05 17:09:29 -06:00
return $this->due;
}
/**
2015-02-06 17:22:17 -06:00
* @param \DateTime|string $due
*/
public function setDue($due = null)
{
$this->due = $this->parseDateTime($due);
}
/**
2015-04-23 14:33:10 -05:00
* @return Carbon
2015-02-06 17:22:17 -06:00
*/
public function getWait()
{
return $this->wait;
}
/**
* @param \DateTime|string $wait
2015-02-05 17:09:29 -06:00
*/
2015-02-06 17:22:17 -06:00
public function setWait($wait = null)
2015-02-05 17:09:29 -06:00
{
2015-02-06 17:22:17 -06:00
$this->wait = $this->parseDateTime($wait);
}
2015-02-06 11:31:13 -06:00
/**
* @return array
*/
public function getTags()
{
return (array)$this->tags;
}
/**
* @param array $tags
*/
public function setTags(array $tags = array())
{
$this->tags = $tags;
}
/**
* @param string $tag
*/
public function addTag($tag)
{
if (!$this->tags) {
$this->tags = [$tag];
}
2015-02-06 11:31:13 -06:00
if (!in_array($tag, $this->tags)) {
$this->tags[] = $tag;
}
}
/**
* @param string $tag
*/
public function removeTag($tag)
{
if (!$this->tags) {
return;
}
2015-02-06 11:31:13 -06:00
if (false !== $key = array_search($tag, $this->tags)) {
unset($this->tags[$key]);
2015-12-26 05:47:51 -06:00
$this->tags = array_values($this->tags);
2015-02-06 11:31:13 -06:00
}
}
2015-07-07 18:17:00 -05:00
/**
* @return Task[]|ArrayCollection
*/
public function getDependencies()
{
return $this->depends;
}
/**
* @param Task $task
*/
public function addDependency(Task $task)
{
$this->depends->add($task);
}
/**
* @param Task $task
*/
public function removeDependency(Task $task)
{
$this->depends->removeElement($task);
}
2015-07-08 04:55:42 -05:00
/**
* @param Task[] $tasks
*/
2015-07-08 18:29:50 -05:00
public function setDependencies(array $tasks = [])
2015-07-08 04:55:42 -05:00
{
$this->depends = new ArrayCollection();
foreach ($tasks as $task) {
$this->addDependency($task);
}
}
2015-02-08 06:13:53 -06:00
/**
2015-02-08 12:32:51 -06:00
* @return Recurring
2015-02-08 06:13:53 -06:00
*/
2015-02-08 07:29:44 -06:00
public function getRecurring()
2015-02-08 06:13:53 -06:00
{
return $this->recur;
}
/**
2015-02-08 07:00:15 -06:00
* @param string|Recurring $recur
2015-02-08 06:13:53 -06:00
*/
2015-02-08 07:29:44 -06:00
public function setRecurring($recur)
2015-02-08 06:13:53 -06:00
{
2015-04-03 09:36:58 -05:00
if ($recur instanceof Recurring) {
2015-02-08 07:00:15 -06:00
$this->recur = $recur;
2015-04-03 09:36:58 -05:00
} elseif ($recur) {
$this->recur = new Recurring($recur);
} else {
$this->recur = null;
2015-02-08 07:00:15 -06:00
}
2015-02-08 06:13:53 -06:00
}
/**
2015-04-23 14:33:10 -05:00
* @return Carbon
2015-02-08 06:13:53 -06:00
*/
public function getUntil()
{
return $this->until;
}
/**
* @param \DateTime|string $until
*/
public function setUntil($until = null)
{
$this->until = $this->parseDateTime($until);
}
2015-02-05 16:00:30 -06:00
/**
2015-04-23 14:33:10 -05:00
* @return Carbon
2015-02-05 16:00:30 -06:00
*/
2015-02-05 17:09:29 -06:00
public function getEntry()
2015-02-05 16:00:30 -06:00
{
2015-02-05 17:09:29 -06:00
return $this->entry;
}
2015-02-06 17:22:17 -06:00
/**
2015-04-23 14:33:10 -05:00
* @return Carbon
*/
public function getStart()
{
return $this->start;
}
/**
* @return Carbon
2015-02-06 17:22:17 -06:00
*/
public function getModified()
{
return $this->modified;
}
/**
* @return \DateTime
*/
public function getEnd()
{
return $this->end;
}
2015-02-05 17:09:29 -06:00
/**
* @return float
*/
public function getUrgency()
{
return $this->urgency;
}
/**
* @return string
*/
public function getStatus()
{
return $this->status;
2015-02-05 16:00:30 -06:00
}
/**
* @return bool
*/
public function isPending()
{
return $this->status == self::STATUS_PENDING;
}
/**
* @return bool
*/
public function isCompleted()
{
return $this->status == self::STATUS_COMPLETED;
}
/**
* @return bool
*/
public function isWaiting()
{
return $this->status == self::STATUS_WAITING;
}
/**
* @return bool
*/
public function isDeleted()
{
return $this->status == self::STATUS_DELETED;
}
2015-02-06 12:01:04 -06:00
2015-02-08 06:13:53 -06:00
/**
* @return bool
*/
2015-04-03 15:27:45 -05:00
public function isRecurring()
2015-02-08 06:13:53 -06:00
{
return $this->status == self::STATUS_RECURRING;
}
2015-02-06 17:22:17 -06:00
/**
* @param string|\DateTime|null $date
* @return \DateTime|null
2015-04-06 16:13:02 -05:00
* @throws DatetimeParseException
2015-02-06 17:22:17 -06:00
*/
private function parseDateTime($date)
{
if ($date instanceof \DateTime) {
2015-04-22 07:00:47 -05:00
return new Carbon($date->format('Y-m-d H:i:s'));
2015-02-08 05:23:14 -06:00
}
2015-02-06 17:22:17 -06:00
2015-02-08 05:23:14 -06:00
if ($date instanceof Carbon) {
2015-02-06 17:22:17 -06:00
return $date;
}
if (is_string($date)) {
2015-04-22 07:00:47 -05:00
return new Carbon($date);
2015-02-06 17:22:17 -06:00
}
if ($date === null) {
return null;
}
2015-04-06 16:13:02 -05:00
throw new DatetimeParseException($date);
2015-02-06 17:22:17 -06:00
}
2015-02-06 12:01:04 -06:00
/**
*
*/
public function __clone()
{
$this->uuid = null;
2015-04-22 07:00:47 -05:00
$this->entry = new Carbon('now');
2015-02-06 12:01:04 -06:00
$this->status = self::STATUS_PENDING;
}
2015-07-08 04:55:42 -05:00
}