src/Repository/PostRepository.php line 54

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace App\Repository;
  11. use App\Entity\Post;
  12. use App\Entity\Tag;
  13. use App\Pagination\Paginator;
  14. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  15. use Doctrine\Persistence\ManagerRegistry;
  16. use function Symfony\Component\String\u;
  17. /**
  18.  * This custom Doctrine repository contains some methods which are useful when
  19.  * querying for blog post information.
  20.  *
  21.  * See https://symfony.com/doc/current/doctrine.html#querying-for-objects-the-repository
  22.  *
  23.  * @author Ryan Weaver <weaverryan@gmail.com>
  24.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  25.  * @author Yonel Ceruto <yonelceruto@gmail.com>
  26.  */
  27. class PostRepository extends ServiceEntityRepository
  28. {
  29.     public function __construct(ManagerRegistry $registry)
  30.     {
  31.         parent::__construct($registryPost::class);
  32.     }
  33.     public function findLatest(int $page 1Tag $tag null): Paginator
  34.     {
  35.         $qb $this->createQueryBuilder('p')
  36.             ->addSelect('a''t')
  37.             ->innerJoin('p.author''a')
  38.             ->leftJoin('p.tags''t')
  39.             ->where('p.publishedAt <= :now')
  40.             ->orderBy('p.publishedAt''DESC')
  41.             ->setParameter('now', new \DateTime())
  42.         ;
  43.         if (null !== $tag) {
  44.             $qb->andWhere(':tag MEMBER OF p.tags')
  45.                 ->setParameter('tag'$tag);
  46.         }
  47.         return (new Paginator($qb))->paginate($page);
  48.     }
  49.     /**
  50.      * @return Post[]
  51.      */
  52.     public function findBySearchQuery(string $queryint $limit Paginator::PAGE_SIZE): array
  53.     {
  54.         $searchTerms $this->extractSearchTerms($query);
  55.         if (=== \count($searchTerms)) {
  56.             return [];
  57.         }
  58.         $queryBuilder $this->createQueryBuilder('p');
  59.         foreach ($searchTerms as $key => $term) {
  60.             $queryBuilder
  61.                 ->orWhere('p.title LIKE :t_'.$key)
  62.                 ->setParameter('t_'.$key'%'.$term.'%')
  63.             ;
  64.         }
  65.         return $queryBuilder
  66.             ->orderBy('p.publishedAt''DESC')
  67.             ->setMaxResults($limit)
  68.             ->getQuery()
  69.             ->getResult();
  70.     }
  71.     /**
  72.      * Transforms the search string into an array of search terms.
  73.      */
  74.     private function extractSearchTerms(string $searchQuery): array
  75.     {
  76.         $searchQuery u($searchQuery)->replaceMatches('/[[:space:]]+/'' ')->trim();
  77.         $terms array_unique($searchQuery->split(' '));
  78.         // ignore the search terms that are too short
  79.         return array_filter($terms, static function ($term) {
  80.             return <= $term->length();
  81.         });
  82.     }
  83. }