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 |
||
127 | |||
128 | /** |
||
129 | * Populates data of an answer derived from parent the parent-posting |
||
130 | * |
||
131 | * @param BasicPostingInterface $parent parent data |
||
132 | * @param array $data current posting data |
||
133 | * @return array populated $data |
||
134 | */ |
||
135 | public function prepareChildPosting(BasicPostingInterface $parent, array $data): array |
||
147 | |||
148 | /** |
||
149 | * Sets-up validator for the table |
||
150 | * |
||
151 | * @param CurrentUserInterface $CurrentUser current user |
||
152 | * @return void |
||
153 | */ |
||
154 | private function validatorSetup(CurrentUserInterface $CurrentUser): void |
||
164 | |||
165 | /** |
||
166 | * check that entries are only in existing and allowed categories |
||
167 | * |
||
168 | * @param mixed $categoryId value |
||
169 | * @param array $context context |
||
170 | * @return bool |
||
171 | */ |
||
172 | public function validateCategoryIsAllowed($categoryId, $context): bool |
||
180 | |||
181 | /** |
||
182 | * check editing allowed |
||
183 | * |
||
184 | * @param mixed $check value |
||
185 | * @param array $context context |
||
186 | * @return bool |
||
187 | */ |
||
188 | public function validateEditingAllowed($check, $context): bool |
||
194 | |||
195 | /** |
||
196 | * Get an array of postings for threads |
||
197 | * |
||
198 | * @param array $tids Thread-IDs |
||
199 | * @param array|null $order Thread sort order |
||
200 | * @param CurrentUserInterface $CU Current User |
||
201 | * @return array Array of postings found |
||
202 | * @throws RecordNotFoundException If no thread is found |
||
203 | */ |
||
204 | public function postingsForThreads(array $tids, ?array $order = null, CurrentUserInterface $CU = null): array |
||
218 | |||
219 | /** |
||
220 | * Get a posting for a thread |
||
221 | * |
||
222 | * @param int $tid Thread-ID |
||
223 | * @param bool $complete complete fieldset |
||
224 | * @param CurrentUserInterface|null $CurrentUser CurrentUser |
||
225 | * @return Posting |
||
226 | * @throws RecordNotFoundException If thread isn't found |
||
227 | */ |
||
228 | public function postingsForThread(int $tid, bool $complete = false, ?CurrentUserInterface $CurrentUser = null): Posting |
||
244 | |||
245 | /** |
||
246 | * Delete a node |
||
247 | * |
||
248 | * @param int $id the node id |
||
249 | * @return bool |
||
250 | */ |
||
251 | public function deletePosting(int $id): bool |
||
271 | |||
272 | /** |
||
273 | * Get recent postings |
||
274 | * |
||
275 | * ### Options: |
||
276 | * |
||
277 | * - `user_id` int|<null> If provided finds only postings of that user. |
||
278 | * - `limit` int <10> Number of postings to find. |
||
279 | * |
||
280 | * @param CurrentUserInterface $User User who has access to postings |
||
281 | * @param array $options find options |
||
282 | * |
||
283 | * @return array Array of Postings |
||
284 | */ |
||
285 | public function getRecentPostings(CurrentUserInterface $User, array $options = []): array |
||
334 | |||
335 | /** |
||
336 | * Convert array with Entry entities to array with Postings |
||
337 | * |
||
338 | * @param Traversable $entries Entry array |
||
339 | * @param CurrentUserInterface|null $CurrentUser The current user |
||
340 | * @return array |
||
341 | */ |
||
342 | protected function entriesToPostings(Traversable $entries, ?CurrentUserInterface $CurrentUser = null): array |
||
358 | |||
359 | /** |
||
360 | * tree of a single node and its subentries |
||
361 | * |
||
362 | * @param int $id id |
||
363 | * @return Posting|null tree or null if nothing found |
||
364 | */ |
||
365 | protected function postingsForNode(int $id) : ?Posting |
||
375 | |||
376 | /** |
||
377 | * Finder to get all entries for threads |
||
378 | * |
||
379 | * @param Query $query Query |
||
380 | * @param array $options Options |
||
381 | * - 'tids' array required thread-IDs |
||
382 | * - 'complete' fieldset |
||
383 | * - 'threadOrder' order |
||
384 | * @return Query |
||
385 | */ |
||
386 | public function findEntriesForThreads(Query $query, array $options): Query |
||
409 | } |
||
410 |
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.