2019-10-02 17:28:40 +02:00
|
|
|
<?php
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-10-02 17:28:40 +02:00
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
2019-10-02 17:28:40 +02:00
|
|
|
*
|
2019-11-01 13:40:30 +01:00
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
|
2019-10-02 17:28:40 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
2019-10-19 23:29:51 +02:00
|
|
|
use App\Services\Attachments\BuiltinAttachmentsFinder;
|
2019-11-01 23:07:31 +01:00
|
|
|
use App\Services\TagFinder;
|
2019-10-02 17:28:40 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
|
|
|
use Symfony\Component\Serializer\Serializer;
|
|
|
|
|
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* In this controller the endpoints for the typeaheads are collected.
|
|
|
|
*
|
2019-10-02 17:28:40 +02:00
|
|
|
* @Route("/typeahead")
|
|
|
|
*/
|
|
|
|
class TypeaheadController extends AbstractController
|
|
|
|
{
|
|
|
|
/**
|
2019-11-10 14:00:56 +01:00
|
|
|
* @Route("/builtInResources/search/{query}", name="typeahead_builtInRessources", requirements={"query"= ".+"})
|
2020-02-02 14:05:36 +01:00
|
|
|
* @param Request $request
|
|
|
|
* @param string $query
|
|
|
|
* @param BuiltinAttachmentsFinder $finder
|
|
|
|
* @return JsonResponse
|
2019-10-02 17:28:40 +02:00
|
|
|
*/
|
2019-11-10 14:00:56 +01:00
|
|
|
public function builtInResources(Request $request, string $query, BuiltinAttachmentsFinder $finder)
|
2019-10-02 17:28:40 +02:00
|
|
|
{
|
|
|
|
$array = $finder->find($query);
|
|
|
|
|
2019-11-01 23:07:31 +01:00
|
|
|
$normalizers = [
|
2019-11-09 00:47:20 +01:00
|
|
|
new ObjectNormalizer(),
|
2019-11-01 23:07:31 +01:00
|
|
|
];
|
2019-11-09 00:47:20 +01:00
|
|
|
$encoders = [
|
|
|
|
new JsonEncoder(),
|
2019-11-01 23:07:31 +01:00
|
|
|
];
|
|
|
|
$serializer = new Serializer($normalizers, $encoders);
|
|
|
|
$data = $serializer->serialize($array, 'json');
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-11-01 23:07:31 +01:00
|
|
|
return new JsonResponse($data, 200, [], true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/tags/search/{query}", name="typeahead_tags", requirements={"query"= ".+"})
|
2020-02-02 14:05:36 +01:00
|
|
|
* @param string $query
|
|
|
|
* @param TagFinder $finder
|
|
|
|
* @return JsonResponse
|
2019-11-01 23:07:31 +01:00
|
|
|
*/
|
|
|
|
public function tags(string $query, TagFinder $finder)
|
|
|
|
{
|
|
|
|
$array = $finder->searchTags($query);
|
|
|
|
|
2019-10-02 17:28:40 +02:00
|
|
|
$normalizers = [
|
2019-11-09 00:47:20 +01:00
|
|
|
new ObjectNormalizer(),
|
2019-10-02 17:28:40 +02:00
|
|
|
];
|
2019-11-09 00:47:20 +01:00
|
|
|
$encoders = [
|
|
|
|
new JsonEncoder(),
|
2019-10-02 17:28:40 +02:00
|
|
|
];
|
|
|
|
$serializer = new Serializer($normalizers, $encoders);
|
|
|
|
$data = $serializer->serialize($array, 'json');
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-10-02 17:28:40 +02:00
|
|
|
return new JsonResponse($data, 200, [], true);
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|