|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* AnimeDb package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Peter Gribanov <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
|
7
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace AnimeDb\Bundle\CatalogBundle\Service\Item\Search\Driver; |
|
10
|
|
|
|
|
11
|
|
|
use AnimeDb\Bundle\CatalogBundle\Entity\Search; |
|
12
|
|
|
use AnimeDb\Bundle\CatalogBundle\Repository\Item; |
|
13
|
|
|
use AnimeDb\Bundle\CatalogBundle\Service\Item\Search\DriverInterface; |
|
14
|
|
|
use Doctrine\Bundle\DoctrineBundle\Registry; |
|
15
|
|
|
use AnimeDb\Bundle\CatalogBundle\Service\Item\Search\Selector; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Search driver use a SQL LIKE for select name. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Peter Gribanov <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class SqlLike implements DriverInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var Item |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $repository; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Selector |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $selector; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param Registry $doctrine |
|
36
|
|
|
* @param Selector $selector |
|
37
|
|
|
*/ |
|
38
|
7 |
|
public function __construct(Registry $doctrine, Selector $selector) |
|
39
|
|
|
{ |
|
40
|
7 |
|
$this->repository = $doctrine->getRepository('AnimeDbCatalogBundle:Item'); |
|
41
|
7 |
|
$this->selector = $selector; |
|
42
|
|
|
|
|
43
|
|
|
// register custom lower() |
|
44
|
7 |
|
$conn = $doctrine->getConnection()->getWrappedConnection(); |
|
45
|
7 |
|
if (method_exists($conn, 'sqliteCreateFunction')) { |
|
46
|
|
|
$conn->sqliteCreateFunction('lower', function ($str) { |
|
47
|
|
|
return mb_strtolower($str, 'UTF8'); |
|
48
|
|
|
}, 1); |
|
49
|
|
|
} |
|
50
|
7 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param Search $entity |
|
54
|
|
|
* @param int $limit |
|
55
|
|
|
* @param int $offset |
|
56
|
|
|
* @param string $sort_column |
|
57
|
|
|
* @param string $sort_direction |
|
58
|
|
|
* |
|
59
|
|
|
* @return array {list:[],total:0} |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function search(Search $entity, $limit, $offset, $sort_column, $sort_direction) |
|
62
|
|
|
{ |
|
63
|
1 |
|
$selector = $this->selector |
|
64
|
1 |
|
->create() |
|
65
|
1 |
|
->addCountry($entity) |
|
66
|
1 |
|
->addDateAdd($entity) |
|
67
|
1 |
|
->addDateEnd($entity) |
|
68
|
1 |
|
->addDatePremiere($entity) |
|
69
|
1 |
|
->addGenres($entity) |
|
70
|
1 |
|
->addLabels($entity) |
|
71
|
1 |
|
->addName($entity) |
|
72
|
1 |
|
->addStorage($entity) |
|
73
|
1 |
|
->addStudio($entity) |
|
74
|
1 |
|
->addType($entity) |
|
75
|
1 |
|
->sort($sort_column, $sort_direction) |
|
76
|
1 |
|
->limit($limit) |
|
77
|
1 |
|
->offset($offset); |
|
78
|
|
|
|
|
79
|
|
|
return [ |
|
80
|
|
|
'list' => $selector |
|
81
|
1 |
|
->getQuerySelect() |
|
82
|
1 |
|
->getQuery() |
|
83
|
1 |
|
->getResult(), |
|
84
|
|
|
'total' => $selector |
|
85
|
1 |
|
->getQueryTotal() |
|
86
|
1 |
|
->getQuery() |
|
87
|
1 |
|
->getSingleScalarResult(), |
|
88
|
1 |
|
]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $name |
|
93
|
|
|
* @param int $limit |
|
94
|
|
|
* |
|
95
|
|
|
* @return array |
|
96
|
|
|
*/ |
|
97
|
6 |
|
public function searchByName($name, $limit = 0) |
|
98
|
|
|
{ |
|
99
|
6 |
|
if (!$name) { |
|
100
|
1 |
|
return []; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
5 |
|
$selector = $this |
|
104
|
|
|
->repository |
|
105
|
5 |
|
->createQueryBuilder('i') |
|
106
|
5 |
|
->innerJoin('i.names', 'n') |
|
107
|
5 |
|
->where('LOWER(i.name) LIKE :name') |
|
108
|
5 |
|
->orWhere('LOWER(n.name) LIKE :name') |
|
109
|
5 |
|
->setParameter('name', preg_replace('/%+/', '%%', mb_strtolower($name, 'UTF-8')).'%') |
|
110
|
5 |
|
->groupBy('i'); |
|
111
|
|
|
|
|
112
|
5 |
|
if ($limit > 0) { |
|
113
|
3 |
|
$selector->setMaxResults($limit); |
|
114
|
3 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
// get items |
|
117
|
|
|
return $selector |
|
118
|
5 |
|
->getQuery() |
|
119
|
5 |
|
->getResult(); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|