1 | <?php |
||
29 | class PostingBehavior extends Behavior |
||
30 | { |
||
31 | /** @var CurrentUserInterface */ |
||
32 | private $CurrentUser; |
||
33 | |||
34 | /** @var FieldFilter */ |
||
35 | private $fieldFilter; |
||
36 | |||
37 | /** |
||
38 | * {@inheritDoc} |
||
39 | */ |
||
40 | public function initialize(array $config) |
||
46 | |||
47 | /** |
||
48 | * Creates a new posting from user |
||
49 | * |
||
50 | * @param array $data raw posting data |
||
51 | * @param CurrentUserInterface $CurrentUser the current user |
||
52 | * @return Entry|null on success, null otherwise |
||
53 | */ |
||
54 | public function createPosting(array $data, CurrentUserInterface $CurrentUser): ?Entry |
||
86 | |||
87 | /** |
||
88 | * Updates an existing posting |
||
89 | * |
||
90 | * @param Entry $posting the posting to update |
||
91 | * @param array $data data the posting should be updated with |
||
92 | * @param CurrentUserInterface $CurrentUser the current-user |
||
93 | * @return Entry|null the posting which was asked to update |
||
94 | */ |
||
95 | public function updatePosting(Entry $posting, array $data, CurrentUserInterface $CurrentUser): ?Entry |
||
128 | |||
129 | /** |
||
130 | * Populates data of an answer derived from parent the parent-posting |
||
131 | * |
||
132 | * @param BasicPostingInterface $parent parent data |
||
133 | * @param array $data current posting data |
||
134 | * @return array populated $data |
||
135 | */ |
||
136 | public function prepareChildPosting(BasicPostingInterface $parent, array $data): array |
||
148 | |||
149 | /** |
||
150 | * Sets-up validator for the table |
||
151 | * |
||
152 | * @param CurrentUserInterface $CurrentUser current user |
||
153 | * @return void |
||
154 | */ |
||
155 | private function validatorSetup(CurrentUserInterface $CurrentUser): void |
||
165 | |||
166 | /** |
||
167 | * check that entries are only in existing and allowed categories |
||
168 | * |
||
169 | * @param mixed $categoryId value |
||
170 | * @param array $context context |
||
171 | * @return bool |
||
172 | */ |
||
173 | public function validateCategoryIsAllowed($categoryId, $context): bool |
||
181 | |||
182 | /** |
||
183 | * check editing allowed |
||
184 | * |
||
185 | * @param mixed $check value |
||
186 | * @param array $context context |
||
187 | * @return bool |
||
188 | */ |
||
189 | public function validateEditingAllowed($check, $context): bool |
||
195 | |||
196 | /** |
||
197 | * Get an array of postings for threads |
||
198 | * |
||
199 | * @param array $tids Thread-IDs |
||
200 | * @param array|null $order Thread sort order |
||
201 | * @param CurrentUserInterface $CU Current User |
||
202 | * @return array Array of postings found |
||
203 | * @throws RecordNotFoundException If no thread is found |
||
204 | */ |
||
205 | public function postingsForThreads(array $tids, ?array $order = null, CurrentUserInterface $CU = null): array |
||
219 | |||
220 | /** |
||
221 | * Get a posting for a thread |
||
222 | * |
||
223 | * @param int $tid Thread-ID |
||
224 | * @param bool $complete complete fieldset |
||
225 | * @param CurrentUserInterface|null $CurrentUser CurrentUser |
||
226 | * @return Posting |
||
227 | * @throws RecordNotFoundException If thread isn't found |
||
228 | */ |
||
229 | public function postingsForThread(int $tid, bool $complete = false, ?CurrentUserInterface $CurrentUser = null): Posting |
||
245 | |||
246 | /** |
||
247 | * Delete a node |
||
248 | * |
||
249 | * @param int $id the node id |
||
250 | * @return bool |
||
251 | */ |
||
252 | public function deletePosting(int $id): bool |
||
272 | |||
273 | /** |
||
274 | * Get recent postings |
||
275 | * |
||
276 | * ### Options: |
||
277 | * |
||
278 | * - `user_id` int|<null> If provided finds only postings of that user. |
||
279 | * - `limit` int <10> Number of postings to find. |
||
280 | * |
||
281 | * @param CurrentUserInterface $User User who has access to postings |
||
282 | * @param array $options find options |
||
283 | * |
||
284 | * @return array Array of Postings |
||
285 | */ |
||
286 | public function getRecentPostings(CurrentUserInterface $User, array $options = []): array |
||
335 | |||
336 | /** |
||
337 | * Convert array with Entry entities to array with Postings |
||
338 | * |
||
339 | * @param Traversable $entries Entry array |
||
340 | * @param CurrentUserInterface|null $CurrentUser The current user |
||
341 | * @return array |
||
342 | */ |
||
343 | protected function entriesToPostings(Traversable $entries, ?CurrentUserInterface $CurrentUser = null): array |
||
359 | |||
360 | /** |
||
361 | * tree of a single node and its subentries |
||
362 | * |
||
363 | * @param int $id id |
||
364 | * @return Posting|null tree or null if nothing found |
||
365 | */ |
||
366 | protected function postingsForNode(int $id) : ?Posting |
||
376 | |||
377 | /** |
||
378 | * Finder to get all entries for threads |
||
379 | * |
||
380 | * @param Query $query Query |
||
381 | * @param array $options Options |
||
382 | * - 'tids' array required thread-IDs |
||
383 | * - 'complete' fieldset |
||
384 | * - 'threadOrder' order |
||
385 | * @return Query |
||
386 | */ |
||
387 | public function findEntriesForThreads(Query $query, array $options): Query |
||
410 | } |
||
411 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.