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