This repository has been archived on 2024-08-28. You can view files and clone it, but cannot push or open issues or pull requests.
satori-services/src/Dictionary/DictionaryRoutes.php

65 lines
1.9 KiB
PHP

<?php
namespace Satori\Dictionary;
use Index\Routing\Route;
use Index\Routing\RouteHandler;
class DictionaryRoutes extends RouteHandler {
public function __construct(
private DictionaryContext $context
) {}
#[Route('GET', '/dictionary/define')]
public function getDefine($response, $request) {
$word = trim((string)$request->getParam('word'));
if($word === '')
return ['error' => 'word'];
$results = $this->context->defineWord($word);
$response = [];
foreach($results as $result) {
if(!$result->hasText())
continue;
$response[] = [
'word' => $result->getWord(),
'part_of_speech' => $result->getPartOfSpeech(),
'attribution_text' => $result->getAttributionText(),
'attribution_url' => $result->getAttributionUrl(),
'source_dictionary' => $result->getSourceDictionary(),
'text' => $result->getText(),
'wordnik_url' => $result->getWordnikUrl(),
];
}
return $response;
}
#[Route('GET', '/word-define.php')]
public function getPHP($response, $request) {
$word = trim((string)$request->getParam('word'));
if($word === '')
return ['error' => 'word'];
$results = $this->context->defineWord($word);
$response = [];
foreach($results as $result) {
if(!$result->hasText())
continue;
$response[] = [
'word' => $result->getWord(),
'pos' => $result->getPartOfSpeech(),
'attr' => $result->getAttributionText(),
'attr_url' => $result->getAttributionUrl(),
'dict' => $result->getSourceDictionary(),
'text' => $result->getText(),
'url' => $result->getWordnikUrl(),
];
}
return $response;
}
}