1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-26 12:23:06 +08:00
xiaochong0302 43adee9ceb 1.重写view provider,数组赋值自动转对象
2.重写response provider,优化json输出格式
2020-04-25 18:36:32 +08:00

89 lines
1.7 KiB
PHP

<?php
namespace App\Services\Search;
use Phalcon\Mvc\User\Component;
class CourseHandler extends Component
{
/**
* @var \XS
*/
protected $xs;
public function __construct()
{
$fileName = config_path() . '/xs.course.ini';
$this->xs = new \XS($fileName);
}
/**
* 获取XS
*
* @return \XS
*/
public function getXS()
{
return $this->xs;
}
/**
* 搜索课程
*
* @param string $query
* @param int $limit
* @param int $offset
* @return array
* @throws \XSException
*/
public function search($query, $limit = 15, $offset = 0)
{
$search = $this->xs->getSearch();
$docs = $search->setQuery($query)->setLimit($limit, $offset)->search();
$total = $search->getLastCount();
$fields = array_keys($this->xs->getAllFields());
$items = [];
foreach ($docs as $doc) {
$item = [];
foreach ($fields as $field) {
if (in_array($field, ['title', 'summary'])) {
$item[$field] = $search->highlight($doc->{$field});
} else {
$item[$field] = $doc->{$field};
}
}
$items[] = $item;
}
return [
'total' => $total,
'items' => $items,
];
}
/**
* 获取相关搜索
*
* @param string $query
* @param int $limit
* @return array
* @throws \XSException
*/
public function getRelatedQuery($query, $limit = 10)
{
$search = $this->xs->getSearch();
$search->setQuery($query);
return $search->getRelatedQuery($query, $limit);
}
}