|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file contains only the Model class. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
declare(strict_types = 1); |
|
7
|
|
|
|
|
8
|
|
|
namespace AppBundle\Model; |
|
9
|
|
|
|
|
10
|
|
|
use AppBundle\Repository\Repository; |
|
11
|
|
|
use Exception; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* A model is any domain-side entity to be represented in the application. |
|
15
|
|
|
* Models know nothing of persistence, transport, or presentation. |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class Model |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Below are the class properties. Some subclasses may not use all of these. |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
/** @var Repository The repository for this model. */ |
|
24
|
|
|
private $repository; |
|
25
|
|
|
|
|
26
|
|
|
/** @var Project The project. */ |
|
27
|
|
|
protected $project; |
|
28
|
|
|
|
|
29
|
|
|
/** @var User The user. */ |
|
30
|
|
|
protected $user; |
|
31
|
|
|
|
|
32
|
|
|
/** @var Page the page associated with this edit */ |
|
33
|
|
|
protected $page; |
|
34
|
|
|
|
|
35
|
|
|
/** @var int|string Which namespace we are querying for. 'all' for all namespaces. */ |
|
36
|
|
|
protected $namespace; |
|
37
|
|
|
|
|
38
|
|
|
/** @var false|int Start of time period as Unix timestamp. */ |
|
39
|
|
|
protected $start; |
|
40
|
|
|
|
|
41
|
|
|
/** @var false|int End of time period as Unix timestamp. */ |
|
42
|
|
|
protected $end; |
|
43
|
|
|
|
|
44
|
|
|
/** @var false|int Unix timestamp to offset results which acts as a substitute for $end */ |
|
45
|
|
|
protected $offset; |
|
46
|
|
|
|
|
47
|
|
|
/** @var int Number of rows to fetch. */ |
|
48
|
|
|
protected $limit; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Set this model's data repository. |
|
52
|
|
|
* @param Repository $repository |
|
53
|
|
|
*/ |
|
54
|
108 |
|
public function setRepository(Repository $repository): void |
|
55
|
|
|
{ |
|
56
|
108 |
|
$this->repository = $repository; |
|
57
|
108 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get this model's repository. |
|
61
|
|
|
* @return Repository A subclass of Repository. |
|
62
|
|
|
* @throws Exception If the repository hasn't been set yet. |
|
63
|
|
|
*/ |
|
64
|
97 |
|
public function getRepository(): Repository |
|
65
|
|
|
{ |
|
66
|
97 |
|
if (!$this->repository instanceof Repository) { |
|
|
|
|
|
|
67
|
2 |
|
$msg = sprintf('Repository for %s must be set before using.', static::class); |
|
68
|
2 |
|
throw new Exception($msg); |
|
69
|
|
|
} |
|
70
|
97 |
|
return $this->repository; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the associated Project. |
|
75
|
|
|
* @return Project |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function getProject(): Project |
|
78
|
|
|
{ |
|
79
|
1 |
|
return $this->project; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get the associated User. |
|
84
|
|
|
* @return User|null |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function getUser(): ?User |
|
87
|
|
|
{ |
|
88
|
1 |
|
return $this->user; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get the associated Page. |
|
93
|
|
|
* @return Page |
|
94
|
|
|
*/ |
|
95
|
13 |
|
public function getPage(): Page |
|
96
|
|
|
{ |
|
97
|
13 |
|
return $this->page; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get the associated namespace. |
|
102
|
|
|
* @return int|string Namespace ID or 'all' for all namespaces. |
|
103
|
|
|
*/ |
|
104
|
3 |
|
public function getNamespace() |
|
105
|
|
|
{ |
|
106
|
3 |
|
return $this->namespace; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Get date opening date range as Unix timestamp. |
|
111
|
|
|
* @return false|int |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public function getStart() |
|
114
|
|
|
{ |
|
115
|
1 |
|
return $this->start; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get date opening date range, formatted as this is used in the views. |
|
120
|
|
|
* @return string Blank if no value exists. |
|
121
|
|
|
*/ |
|
122
|
3 |
|
public function getStartDate(): string |
|
123
|
|
|
{ |
|
124
|
3 |
|
return is_int($this->start) ? date('Y-m-d', $this->start) : ''; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Get date closing date range as Unix timestamp. |
|
129
|
|
|
* @return false|int |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function getEnd() |
|
132
|
|
|
{ |
|
133
|
1 |
|
return $this->end; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get date closing date range, formatted as this is used in the views. |
|
138
|
|
|
* @return string Blank if no value exists. |
|
139
|
|
|
*/ |
|
140
|
3 |
|
public function getEndDate(): string |
|
141
|
|
|
{ |
|
142
|
3 |
|
return is_int($this->end) ? date('Y-m-d', $this->end) : ''; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Has date range? |
|
147
|
|
|
* @return bool |
|
148
|
|
|
*/ |
|
149
|
1 |
|
public function hasDateRange(): bool |
|
150
|
|
|
{ |
|
151
|
1 |
|
return $this->start || $this->end; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Get the limit set on number of rows to fetch. |
|
156
|
|
|
* @return int |
|
157
|
|
|
*/ |
|
158
|
1 |
|
public function getLimit(): int |
|
159
|
|
|
{ |
|
160
|
1 |
|
return (int)$this->limit; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Get the offset timestamp as Unix timestamp. Used for pagination. |
|
165
|
|
|
* @return false|int |
|
166
|
|
|
*/ |
|
167
|
3 |
|
public function getOffset() |
|
168
|
|
|
{ |
|
169
|
3 |
|
return $this->offset; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Get the offset timestamp as a formatted ISO timestamp. |
|
174
|
|
|
* @return null|string |
|
175
|
|
|
*/ |
|
176
|
|
|
public function getOffsetISO(): ?string |
|
177
|
|
|
{ |
|
178
|
|
|
return is_int($this->offset) ? date('Y-m-d\TH:i:s', $this->offset) : null; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|