1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chriscreates\Projects\Traits; |
4
|
|
|
|
5
|
|
|
trait IsMeasurable |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Has the task got an expected target. |
9
|
|
|
* |
10
|
|
|
* @return bool |
11
|
|
|
*/ |
12
|
|
|
public function hasDateTarget() : bool |
13
|
|
|
{ |
14
|
|
|
return $this->started_at !== null || $this->expected_at !== null; |
|
|
|
|
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Is the task still in process. |
19
|
|
|
* |
20
|
|
|
* @return bool |
21
|
|
|
*/ |
22
|
|
|
public function isInProcess() : bool |
23
|
|
|
{ |
24
|
|
|
return $this->delivered_at === null; |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Is the task not due yet. |
29
|
|
|
* |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
|
|
public function notDueYet() : bool |
33
|
|
|
{ |
34
|
|
|
return $this->expected_at->greaterThan(now()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Is the task complete. |
39
|
|
|
* |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
public function completed() : bool |
43
|
|
|
{ |
44
|
|
|
if ( ! $this->hasDateTarget() || $this->isInProcess()) { |
45
|
|
|
return false; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return ($this->completedOnSchedule() |
49
|
|
|
|| $this->completedAfterSchedule()) |
50
|
|
|
&& $this->completedAllTasks(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Was the task completed after the given deadline. |
55
|
|
|
* |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public function completedAfterSchedule() : bool |
59
|
|
|
{ |
60
|
|
|
return $this->delivered_at->greaterThan($this->expected_at); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Was the task completed before the given deadline. |
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public function completedBeforeSchedule() : bool |
69
|
|
|
{ |
70
|
|
|
return $this->delivered_at->lessThan($this->expected_at); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Was the task completed before or on the given deadline. |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function completedOnSchedule() : bool |
79
|
|
|
{ |
80
|
|
|
return $this->delivered_at->lessThanOrEqualTo($this->expected_at); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Is the task overdue. |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
public function isOverdue() : bool |
89
|
|
|
{ |
90
|
|
|
return $this->isInProcess() && ! $this->notDueYet(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Have all tasks been completed. |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
public function completedAllTasks() : bool |
99
|
|
|
{ |
100
|
|
|
$this->load('tasks'); |
|
|
|
|
101
|
|
|
|
102
|
|
|
if ($this->tasks->isEmpty()) { |
|
|
|
|
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->tasks->count() === $this->tasks->sum->completed(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: