Skip to content

Commit 6f700c1

Browse files
committed
Refactor to use in the post filtering feature
1 parent a7a131e commit 6f700c1

File tree

4 files changed

+48
-23
lines changed

4 files changed

+48
-23
lines changed

app/config/services.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ services:
77
markdown:
88
class: AppBundle\Utils\Markdown
99

10+
term_splitter:
11+
class: AppBundle\Utils\TermSplitter
12+
1013
# These are the Twig extensions that create new filters and functions for
1114
# using them in the templates
1215
app.twig.app_extension:

src/AppBundle/Controller/BlogController.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,26 +128,21 @@ public function commentFormAction(Post $post)
128128
*/
129129
public function searchAction(Request $request)
130130
{
131-
$query = $request->query->get('q', '');
132-
133-
// Sanitizing the query: removes all non-alphanumeric characters except whitespaces
134-
$query = preg_replace('/[^[:alnum:] ]/', '', trim(preg_replace('/[[:space:]]+/', ' ', $query)));
131+
$posts = array();
135132

136-
// Splits the query into terms and removes all terms which
137-
// length is less than 2
138-
$terms = array_unique(explode(' ', strtolower($query)));
139-
$terms = array_filter($terms, function($term) {
140-
return 2 <= strlen($term);
141-
});
133+
$query = $request->query->get('q', '');
142134

143-
$posts = array();
135+
$terms = $this->get('term_splitter')->splitIntoTerms($query, 2);
144136

145137
if (!empty($terms)) {
146138
$posts = $this
147139
->getDoctrine()
148-
->getManager()
149140
->getRepository('AppBundle:Post')
150-
->findByTerms($terms)
141+
->createQueryBuilderForTerms($terms, 'p')
142+
->orderBy('p.publishedAt', 'DESC')
143+
->setMaxResults(10)
144+
->getQuery()
145+
->getResult()
151146
;
152147
}
153148

@@ -156,7 +151,7 @@ public function searchAction(Request $request)
156151
foreach ($posts as $post) {
157152
array_push($results, array(
158153
'result' => htmlspecialchars($post->getTitle()),
159-
'url' => $this->generateUrl('blog_post', array('slug' => $post->getSlug())),
154+
'url' => $this->generateUrl('blog_post', array('slug' => $post->getSlug())),
160155
));
161156
}
162157

src/AppBundle/Repository/PostRepository.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace AppBundle\Repository;
1313

1414
use Doctrine\ORM\EntityRepository;
15-
use AppBundle\Entity\Post;
1615

1716
/**
1817
* This custom Doctrine repository contains some methods which are useful when
@@ -42,21 +41,17 @@ public function findLatest()
4241
$this->queryLatest()->getResult();
4342
}
4443

45-
public function findByTerms(array $terms, $limit = Post::NUM_ITEMS)
44+
public function createQueryBuilderForTerms(array $terms, $alias = 'p')
4645
{
47-
$queryBuilder = $this->createQueryBuilder('p');
46+
$queryBuilder = $this->createQueryBuilder($alias);
4847

4948
foreach ($terms as $key => $term) {
5049
$queryBuilder
51-
->orWhere('p.title LIKE :t_' . $key)
50+
->orWhere(sprintf('%s.title LIKE :t_%s', $alias, $key))
5251
->setParameter('t_' . $key , '%' . $term . '%')
5352
;
5453
}
5554

56-
return $queryBuilder
57-
->orderBy('p.publishedAt', 'DESC')
58-
->setMaxResults($limit)
59-
->getQuery()
60-
->getResult();
55+
return $queryBuilder;
6156
}
6257
}

src/AppBundle/Utils/TermSplitter.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace AppBundle\Utils;
4+
5+
/**
6+
* @author Oleg Voronkovich <[email protected]>
7+
*/
8+
class TermSplitter
9+
{
10+
/**
11+
* Splits a given string into terms.
12+
*
13+
* @param string $string
14+
* @param int $minTermLength
15+
*
16+
* @return array
17+
*/
18+
public function splitIntoTerms($string, $minTermLength = 2)
19+
{
20+
// Sanitizing the string: removes all non-alphanumeric characters except whitespaces
21+
$string = preg_replace('/[^[:alnum:] ]/', '', trim(preg_replace('/[[:space:]]+/', ' ', $string)));
22+
23+
// Splits the string into terms and removes all terms which
24+
// length is less than $minTermLength
25+
$terms = array_unique(explode(' ', strtolower($string)));
26+
$terms = array_filter($terms, function($term) use ($minTermLength) {
27+
return $minTermLength <= strlen($term);
28+
});
29+
30+
return $terms;
31+
}
32+
}

0 commit comments

Comments
 (0)