/
KnpResultSet.php
80 lines (70 loc) · 2.15 KB
/
KnpResultSet.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/*
* This file is part of the Stinger Entity Search package.
*
* (c) Oliver Kotte <oliver.kotte@stinger-soft.net>
* (c) Florian Meyer <florian.meyer@stinger-soft.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace StingerSoft\DoctrineEntitySearchBundle\Model;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use StingerSoft\EntitySearchBundle\Model\PaginatableResultSet;
use StingerSoft\EntitySearchBundle\Model\ResultSetAdapter;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use StingerSoft\PhpCommons\String\Utils;
use StingerSoft\EntitySearchBundle\Model\Document as BaseDocument;
class KnpResultSet extends ResultSetAdapter implements PaginatableResultSet {
use ContainerAwareTrait;
protected $query = null;
protected $term = null;
/**
*
* @param Query|QueryBuilder $items
*/
public function __construct($items, $term) {
$this->query = $items;
$this->term = $term;
}
/**
*
* {@inheritDoc}
*
* @see \StingerSoft\EntitySearchBundle\Model\PaginatableResultSet::paginate()
*/
public function paginate($page = 1, $limit = 10, array $options = array()) {
$paginator = $this->container->get('knp_paginator');
return $paginator->paginate($this->query, $page, $limit, $options);
}
/**
*
* {@inheritDoc}
*
* @see \StingerSoft\EntitySearchBundle\Model\ResultSetAdapter::getResults()
*/
public function getResults($offset = 0, $limit = null) {
$query = null;
if($this->query instanceof QueryBuilder) {
$query = $this->query->getQuery();
} else {
$query = clone $this->query;
}
$query->setFirstResult($offset);
$query->setMaxResults($limit);
return $query->getResult();
}
/**
*
* {@inheritDoc}
*
* @see \StingerSoft\EntitySearchBundle\Model\ResultSet::getExcerpt()
*/
public function getExcerpt(BaseDocument $document) {
$content = $document->getFieldValue(BaseDocument::FIELD_CONTENT);
$content = !is_array($content) ? $content : implode(' ', $content);
if($content === null) return null;
return Utils::highlight(Utils::excerpt($content, $this->term), $this->term);
}
}