| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of the Sonata Project package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * (c) Thomas Rabaix <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Sonata\NewsBundle\Document; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Sonata\Doctrine\Document\BaseDocumentManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Sonata\DoctrineMongoDBAdminBundle\Datagrid\Pager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Sonata\NewsBundle\Model\CommentInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Sonata\NewsBundle\Model\CommentManagerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Sonata\NewsBundle\Model\PostInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class CommentManager extends BaseDocumentManager implements CommentManagerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param int $page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param int $limit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @return Pager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function getPager(array $criteria, $page, $limit = 10, array $sort = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $qb = $this->getDocumentManager()->getRepository($this->class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             ->createQueryBuilder() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             ->sort('createdAt', 'desc'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $criteria['status'] = $criteria['status'] ?? CommentInterface::STATUS_VALID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $qb->field('status')->equals($criteria['status']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         if (isset($criteria['postId'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             $qb->field('post')->equals($criteria['postId']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $pager = new Pager(500); // no limit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $pager->setQuery(new ProxyQuery($qb)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $pager->setPage($page); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $pager->init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         return $pager; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Update the comments count. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param PostInterface $post | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |     public function updateCommentsCount(?PostInterface $post = null): void | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |         $post->setCommentsCount($post->getCommentsCount() + 1); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |         $this->getDocumentManager()->persist($post); | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         $this->getDocumentManager()->flush(); | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 63 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |  | 
            
                        
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.