1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Purple - Run tasks on collections |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* Copyright (C) 2016 Jake Johns |
8
|
|
|
* |
9
|
|
|
* This software may be modified and distributed under the terms |
10
|
|
|
* of the MIT license. See the LICENSE file for details. |
11
|
|
|
* |
12
|
|
|
* @category Task |
13
|
|
|
* @package Jnjxp\Purple |
14
|
|
|
* @author Jake Johns <[email protected]> |
15
|
|
|
* @copyright 2016 Jake Johns |
16
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
17
|
|
|
* @link https://github.com/jnjxp/jnjxp.purple |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Jnjxp\Purple; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Task |
24
|
|
|
* |
25
|
|
|
* @category Task |
26
|
|
|
* @package Jnjxp\Purple |
27
|
|
|
* @author Jake Johns <[email protected]> |
28
|
|
|
* @license http://jnj.mit-license.org/ MIT License |
29
|
|
|
* @link https://github.com/jnjxp/jnjxp.purple |
30
|
|
|
* |
31
|
|
|
* @see TaskInterface |
32
|
|
|
*/ |
33
|
|
|
class Task implements TaskInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Collection |
37
|
|
|
* |
38
|
|
|
* @var mixed |
39
|
|
|
* |
40
|
|
|
* @access protected |
41
|
|
|
*/ |
42
|
|
|
protected $collection; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Item tasks |
46
|
|
|
* |
47
|
|
|
* @var QueueInterface |
48
|
|
|
* |
49
|
|
|
* @access protected |
50
|
|
|
*/ |
51
|
|
|
protected $itemTasks; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Collection tasks |
55
|
|
|
* |
56
|
|
|
* @var QueueInterface |
57
|
|
|
* |
58
|
|
|
* @access protected |
59
|
|
|
*/ |
60
|
|
|
protected $collectionTasks; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* __construct |
64
|
|
|
* |
65
|
|
|
* @param mixed $collection DESCRIPTION |
66
|
|
|
* @param QueueInterface $itemTaskQueue DESCRIPTION |
67
|
|
|
* @param QueueInterface $collectionTaskQueue DESCRIPTION |
68
|
|
|
* |
69
|
|
|
* @access public |
70
|
|
|
*/ |
71
|
3 |
|
public function __construct( |
72
|
|
|
$collection, |
73
|
1 |
|
QueueInterface $itemTaskQueue = null, |
74
|
1 |
|
QueueInterface $collectionTaskQueue = null |
75
|
|
|
) { |
76
|
3 |
|
$this->collection = $collection; |
77
|
2 |
|
$this->itemTasks = $itemTaskQueue ?: new Queue; |
78
|
2 |
|
$this->collectionTasks = $collectionTaskQueue ?: new Queue; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Add an item task |
83
|
|
|
* |
84
|
|
|
* @param mixed $task task or task spec |
85
|
|
|
* @param int $priority priority of task |
86
|
|
|
* |
87
|
|
|
* @return $this |
88
|
|
|
* |
89
|
|
|
* @access public |
90
|
|
|
*/ |
91
|
2 |
|
public function each($task, $priority = 1000) |
92
|
|
|
{ |
93
|
|
|
$this->itemTasks->insert($task, $priority); |
94
|
2 |
|
return $this; |
95
|
2 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Add Item Tasks |
99
|
|
|
* |
100
|
|
|
* @param mixed $tasks tasks to add |
101
|
|
|
* @param int $priority task priority |
102
|
|
|
* |
103
|
|
|
* @return $this |
104
|
|
|
* |
105
|
|
|
* @access public |
106
|
|
|
*/ |
107
|
1 |
|
public function addItemTasks($tasks, $priority = 1000) |
108
|
|
|
{ |
109
|
|
|
foreach ($tasks as $task) { |
110
|
|
|
$this->each($task, $priority); |
111
|
|
|
} |
112
|
|
|
return $this; |
113
|
1 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set Item Tasks |
117
|
|
|
* |
118
|
|
|
* @param mixed $tasks Tasks to set |
119
|
|
|
* @param int $priority Task priority |
120
|
|
|
* |
121
|
|
|
* @return $this |
122
|
|
|
* |
123
|
|
|
* @access public |
124
|
|
|
*/ |
125
|
1 |
|
public function setItemTasks($tasks, $priority = 1000) |
126
|
|
|
{ |
127
|
|
|
$this->itemTasks->clear(); |
128
|
|
|
return $this->addItemTasks($tasks, $priority); |
129
|
1 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Add a collection task |
133
|
|
|
* |
134
|
|
|
* @param mixed $task task |
135
|
|
|
* @param int $priority order priority |
136
|
|
|
* |
137
|
|
|
* @return $this |
138
|
|
|
* |
139
|
|
|
* @access public |
140
|
|
|
*/ |
141
|
2 |
|
public function all($task, $priority = 1000) |
142
|
|
|
{ |
143
|
|
|
$this->collectionTasks->insert($task, $priority); |
144
|
2 |
|
return $this; |
145
|
2 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Add Collection Tasks |
149
|
|
|
* |
150
|
|
|
* @param mixed $tasks tasks to add |
151
|
|
|
* @param int $priority task priority |
152
|
|
|
* |
153
|
|
|
* @return $this |
154
|
|
|
* |
155
|
|
|
* @access public |
156
|
|
|
*/ |
157
|
1 |
|
public function addCollectionTasks($tasks, $priority = 1000) |
158
|
|
|
{ |
159
|
|
|
foreach ($tasks as $task) { |
160
|
|
|
$this->all($task, $priority); |
161
|
|
|
} |
162
|
|
|
return $this; |
163
|
1 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Set Collection Tasks |
167
|
|
|
* |
168
|
|
|
* @param mixed $tasks tasks to set |
169
|
|
|
* @param int $priority priority of tasks |
170
|
|
|
* |
171
|
|
|
* @return $this |
172
|
|
|
* |
173
|
|
|
* @access public |
174
|
|
|
*/ |
175
|
1 |
|
public function setCollectionTasks($tasks, $priority = 1000) |
176
|
|
|
{ |
177
|
|
|
$this->collectionTasks->clear(); |
178
|
|
|
return $this->addCollectionTasks($tasks, $priority); |
179
|
1 |
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get collection |
183
|
|
|
* |
184
|
|
|
* @return mixed |
185
|
|
|
* |
186
|
|
|
* @access public |
187
|
|
|
*/ |
188
|
2 |
|
public function getCollection() |
189
|
|
|
{ |
190
|
2 |
|
return $this->collection; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get item tasks |
195
|
|
|
* |
196
|
|
|
* @return QueueInterface |
197
|
|
|
* |
198
|
|
|
* @access public |
199
|
|
|
*/ |
200
|
2 |
|
public function getItemTasks() |
201
|
|
|
{ |
202
|
2 |
|
return $this->itemTasks; |
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get collection tasks |
207
|
|
|
* |
208
|
|
|
* @return QueueInterface |
209
|
|
|
* |
210
|
|
|
* @access public |
211
|
|
|
*/ |
212
|
2 |
|
public function getCollectionTasks() |
213
|
|
|
{ |
214
|
2 |
|
return $this->collectionTasks; |
|
|
|
|
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.