1 | <?php |
||
37 | class PostController |
||
38 | { |
||
39 | /** |
||
40 | * @var PostManagerInterface |
||
41 | */ |
||
42 | protected $postManager; |
||
43 | |||
44 | /** |
||
45 | * @var CommentManagerInterface |
||
46 | */ |
||
47 | protected $commentManager; |
||
48 | |||
49 | /** |
||
50 | * @var MailerInterface |
||
51 | */ |
||
52 | protected $mailer; |
||
53 | |||
54 | /** |
||
55 | * @var FormFactoryInterface |
||
56 | */ |
||
57 | protected $formFactory; |
||
58 | |||
59 | /** |
||
60 | * @var FormatterPool |
||
61 | */ |
||
62 | protected $formatterPool; |
||
63 | |||
64 | public function __construct(PostManagerInterface $postManager, CommentManagerInterface $commentManager, MailerInterface $mailer, FormFactoryInterface $formFactory, FormatterPool $formatterPool) |
||
72 | |||
73 | /** |
||
74 | * Retrieves the list of posts (paginated) based on criteria. |
||
75 | * |
||
76 | * @ApiDoc( |
||
77 | * resource=true, |
||
78 | * output={"class"="Sonata\DatagridBundle\Pager\PagerInterface", "groups"={"sonata_api_read"}} |
||
79 | * ) |
||
80 | * |
||
81 | * @REST\QueryParam(name="page", requirements="\d+", default="1", description="Page for posts list pagination") |
||
82 | * @REST\QueryParam(name="count", requirements="\d+", default="10", description="Number of posts by page") |
||
83 | * @REST\QueryParam(name="enabled", requirements="0|1", nullable=true, strict=true, description="Enabled/Disabled posts filter") |
||
84 | * @REST\QueryParam(name="dateQuery", requirements=">|<|=", default=">", description="Date filter orientation (>, < or =)") |
||
85 | * @REST\QueryParam(name="dateValue", requirements="[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]([+-][0-9]{2}(:)?[0-9]{2})?", nullable=true, strict=true, description="Date filter value") |
||
86 | * @REST\QueryParam(name="tag", requirements="\S+", nullable=true, strict=true, description="Tag name filter") |
||
87 | * @REST\QueryParam(name="author", requirements="\S+", nullable=true, strict=true, description="Author filter") |
||
88 | * @REST\QueryParam(name="mode", requirements="public|admin", default="public", description="'public' mode filters posts having enabled tags and author") |
||
89 | * |
||
90 | * @REST\View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true) |
||
91 | * |
||
92 | * @return PagerInterface |
||
93 | */ |
||
94 | public function getPostsAction(ParamFetcherInterface $paramFetcher) |
||
103 | |||
104 | /** |
||
105 | * Retrieves a specific post. |
||
106 | * |
||
107 | * @ApiDoc( |
||
108 | * requirements={ |
||
109 | * {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="Post identifier"} |
||
110 | * }, |
||
111 | * output={"class"="sonata_news_api_form_post", "groups"={"sonata_api_read"}}, |
||
112 | * statusCodes={ |
||
113 | * 200="Returned when successful", |
||
114 | * 404="Returned when post is not found" |
||
115 | * } |
||
116 | * ) |
||
117 | * |
||
118 | * @REST\View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true) |
||
119 | * |
||
120 | * @param int $id A post identifier |
||
121 | * |
||
122 | * @return Post |
||
123 | */ |
||
124 | public function getPostAction($id) |
||
128 | |||
129 | /** |
||
130 | * Adds a post. |
||
131 | * |
||
132 | * @ApiDoc( |
||
133 | * input={"class"="sonata_news_api_form_post", "name"="", "groups"={"sonata_api_write"}}, |
||
134 | * output={"class"="sonata_news_api_form_post", "groups"={"sonata_api_read"}}, |
||
135 | * statusCodes={ |
||
136 | * 200="Returned when successful", |
||
137 | * 400="Returned when an error has occurred while post creation", |
||
138 | * } |
||
139 | * ) |
||
140 | * |
||
141 | * @param Request $request Symfony request |
||
142 | * |
||
143 | * @throws NotFoundHttpException |
||
144 | * |
||
145 | * @return Post |
||
146 | */ |
||
147 | public function postPostAction(Request $request) |
||
151 | |||
152 | /** |
||
153 | * Updates a post. |
||
154 | * |
||
155 | * @ApiDoc( |
||
156 | * requirements={ |
||
157 | * {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="Post identifier"} |
||
158 | * }, |
||
159 | * input={"class"="sonata_news_api_form_post", "name"="", "groups"={"sonata_api_write"}}, |
||
160 | * output={"class"="sonata_news_api_form_post", "groups"={"sonata_api_read"}}, |
||
161 | * statusCodes={ |
||
162 | * 200="Returned when successful", |
||
163 | * 400="Returned when an error has occurred while post update", |
||
164 | * 404="Returned when unable to find post" |
||
165 | * } |
||
166 | * ) |
||
167 | * |
||
168 | * @param int $id Post identifier |
||
169 | * @param Request $request Symfony request |
||
170 | * |
||
171 | * @throws NotFoundHttpException |
||
172 | * |
||
173 | * @return Post |
||
174 | */ |
||
175 | public function putPostAction($id, Request $request) |
||
179 | |||
180 | /** |
||
181 | * Deletes a post. |
||
182 | * |
||
183 | * @ApiDoc( |
||
184 | * requirements={ |
||
185 | * {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="Post identifier"} |
||
186 | * }, |
||
187 | * statusCodes={ |
||
188 | * 200="Returned when post is successfully deleted", |
||
189 | * 400="Returned when an error has occurred while post deletion", |
||
190 | * 404="Returned when unable to find post" |
||
191 | * } |
||
192 | * ) |
||
193 | * |
||
194 | * @param int $id Post identifier |
||
195 | * |
||
196 | * @throws NotFoundHttpException |
||
197 | * |
||
198 | * @return View |
||
199 | */ |
||
200 | public function deletePostAction($id) |
||
212 | |||
213 | /** |
||
214 | * Retrieves the comments of specified post. |
||
215 | * |
||
216 | * @ApiDoc( |
||
217 | * requirements={ |
||
218 | * {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="Post identifier"} |
||
219 | * }, |
||
220 | * output={"class"="Sonata\DatagridBundle\Pager\PagerInterface", "groups"={"sonata_api_read"}}, |
||
221 | * statusCodes={ |
||
222 | * 200="Returned when successful", |
||
223 | * 404="Returned when post is not found" |
||
224 | * } |
||
225 | * ) |
||
226 | * |
||
227 | * @REST\QueryParam(name="page", requirements="\d+", default="1", description="Page for comments list pagination") |
||
228 | * @REST\QueryParam(name="count", requirements="\d+", default="10", description="Number of comments by page") |
||
229 | * |
||
230 | * @REST\View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true) |
||
231 | * |
||
232 | * @param int $id Post identifier |
||
233 | * |
||
234 | * @return PagerInterface |
||
235 | */ |
||
236 | public function getPostCommentsAction($id, ParamFetcherInterface $paramFetcher) |
||
251 | |||
252 | /** |
||
253 | * Adds a comment to a post. |
||
254 | * |
||
255 | * @ApiDoc( |
||
256 | * requirements={ |
||
257 | * {"name"="id", "dataType"="integer", "requirement"="\d+", "description"="Post identifier"} |
||
258 | * }, |
||
259 | * input={"class"="sonata_news_api_form_comment", "name"="", "groups"={"sonata_api_write"}}, |
||
260 | * output={"class"="Sonata\NewsBundle\Model\Comment", "groups"={"sonata_api_read"}}, |
||
261 | * statusCodes={ |
||
262 | * 200="Returned when successful", |
||
263 | * 400="Returned when an error has occurred while comment creation", |
||
264 | * 403="Returned when commenting is not enabled on the related post", |
||
265 | * 404="Returned when post is not found" |
||
266 | * } |
||
267 | * ) |
||
268 | * |
||
269 | * @REST\View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true) |
||
270 | * |
||
271 | * @param int $id Post identifier |
||
272 | * |
||
273 | * @throws HttpException |
||
274 | * |
||
275 | * @return Comment|FormInterface |
||
276 | */ |
||
277 | public function postPostCommentsAction($id, Request $request) |
||
307 | |||
308 | /** |
||
309 | * Updates a comment. |
||
310 | * |
||
311 | * @ApiDoc( |
||
312 | * requirements={ |
||
313 | * {"name"="postId", "dataType"="integer", "requirement"="\d+", "description"="Post identifier"}, |
||
314 | * {"name"="commentId", "dataType"="integer", "requirement"="\d+", "description"="Comment identifier"} |
||
315 | * }, |
||
316 | * input={"class"="sonata_news_api_form_comment", "name"="", "groups"={"sonata_api_write"}}, |
||
317 | * output={"class"="Sonata\NewsBundle\Model\Comment", "groups"={"sonata_api_read"}}, |
||
318 | * statusCodes={ |
||
319 | * 200="Returned when successful", |
||
320 | * 400="Returned when an error has occurred while comment update", |
||
321 | * 404="Returned when unable to find comment" |
||
322 | * } |
||
323 | * ) |
||
324 | * |
||
325 | * @REST\View(serializerGroups={"sonata_api_read"}, serializerEnableMaxDepthChecks=true) |
||
326 | * |
||
327 | * @param int $postId Post identifier |
||
328 | * @param int $commentId Comment identifier |
||
329 | * @param Request $request Symfony request |
||
330 | * |
||
331 | * @throws NotFoundHttpException |
||
332 | * @throws HttpException |
||
333 | * |
||
334 | * @return Comment |
||
335 | */ |
||
336 | public function putPostCommentsAction($postId, $commentId, Request $request) |
||
367 | |||
368 | /** |
||
369 | * Filters criteria from $paramFetcher to be compatible with the Pager criteria. |
||
370 | * |
||
371 | * @return array The filtered criteria |
||
372 | */ |
||
373 | protected function filterCriteria(ParamFetcherInterface $paramFetcher) |
||
398 | |||
399 | /** |
||
400 | * Retrieves post with id $id or throws an exception if it doesn't exist. |
||
401 | * |
||
402 | * @param int $id Post identifier |
||
403 | * |
||
404 | * @throws NotFoundHttpException |
||
405 | * |
||
406 | * @return Post |
||
407 | */ |
||
408 | protected function getPost($id) |
||
418 | |||
419 | /** |
||
420 | * Write a post, this method is used by both POST and PUT action methods. |
||
421 | * |
||
422 | * @param Request $request Symfony request |
||
423 | * @param int|null $id Post identifier |
||
424 | * |
||
425 | * @return View|FormInterface |
||
426 | */ |
||
427 | protected function handleWritePost($request, $id = null) |
||
460 | } |
||
461 |
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.