first commit
This commit is contained in:
103
Classes/System/Meilisearch/Parser/SchemaParser.php
Normal file
103
Classes/System/Meilisearch/Parser/SchemaParser.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace WapplerSystems\Meilisearch\System\Meilisearch\Parser;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2010-2016 Timo Hund <timo.hund@dkd.de
|
||||
* All rights reserved
|
||||
*
|
||||
* This script is part of the TYPO3 project. The TYPO3 project 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The GNU General Public License can be found at
|
||||
* http://www.gnu.org/copyleft/gpl.html.
|
||||
*
|
||||
* This script 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.
|
||||
*
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\Schema\Schema;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* Class to parse the schema from a meilisearch response.
|
||||
*
|
||||
* @author Timo Hund <timo.hund@dkd.de>
|
||||
*/
|
||||
class SchemaParser
|
||||
{
|
||||
|
||||
/**
|
||||
* Parse the meilisearch stopwords response from an json string to an array.
|
||||
*
|
||||
* @param string $jsonString
|
||||
* @return Schema
|
||||
*/
|
||||
public function parseJson($jsonString)
|
||||
{
|
||||
$decodedResponse = json_decode($jsonString);
|
||||
$schemaResponse = $decodedResponse->schema;
|
||||
|
||||
$schema = GeneralUtility::makeInstance(Schema::class);
|
||||
|
||||
if ($schemaResponse === null) {
|
||||
return $schema;
|
||||
}
|
||||
|
||||
$language = $this->parseLanguage($schemaResponse);
|
||||
$schema->setLanguage($language);
|
||||
|
||||
$name = $this->parseName($schemaResponse);
|
||||
$schema->setName($name);
|
||||
|
||||
return $schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the language from a meilisearch schema response.
|
||||
*
|
||||
* @param \stdClass $schema
|
||||
* @return string
|
||||
*/
|
||||
protected function parseLanguage(\stdClass $schema)
|
||||
{
|
||||
$language = 'english';
|
||||
if (!is_object($schema) || !isset($schema->fieldTypes)) {
|
||||
return $language;
|
||||
}
|
||||
|
||||
foreach ($schema->fieldTypes as $fieldType) {
|
||||
if ($fieldType->name !== 'text') {
|
||||
continue;
|
||||
}
|
||||
// we have a text field
|
||||
foreach ($fieldType->queryAnalyzer->filters as $filter) {
|
||||
if ($filter->class === 'meilisearch.ManagedSynonymGraphFilterFactory') {
|
||||
$language = $filter->managed;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $language;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the schema name from the response.
|
||||
*
|
||||
* @param \stdClass $schemaResponse
|
||||
* @return string
|
||||
*/
|
||||
protected function parseName(\stdClass $schemaResponse)
|
||||
{
|
||||
return isset($schemaResponse->name) ? $schemaResponse->name : '';
|
||||
}
|
||||
}
|
74
Classes/System/Meilisearch/Parser/StopWordParser.php
Normal file
74
Classes/System/Meilisearch/Parser/StopWordParser.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace WapplerSystems\Meilisearch\System\Meilisearch\Parser;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2010-2016 Timo Hund <timo.hund@dkd.de
|
||||
* All rights reserved
|
||||
*
|
||||
* This script is part of the TYPO3 project. The TYPO3 project 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The GNU General Public License can be found at
|
||||
* http://www.gnu.org/copyleft/gpl.html.
|
||||
*
|
||||
* This script 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.
|
||||
*
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
|
||||
/**
|
||||
* Class to parse the stopwords from a meilisearch response.
|
||||
*
|
||||
* @author Timo Hund <timo.hund@dkd.de>
|
||||
*/
|
||||
class StopWordParser
|
||||
{
|
||||
|
||||
/**
|
||||
* Parse the meilisearch stopwords response from an json string to an array.
|
||||
*
|
||||
* @param string $jsonString
|
||||
* @return array
|
||||
*/
|
||||
public function parseJson($jsonString)
|
||||
{
|
||||
$stopWords = [];
|
||||
|
||||
$decodedResponse = json_decode($jsonString);
|
||||
|
||||
if (isset($decodedResponse->wordSet->managedList)) {
|
||||
$stopWords = (array)$decodedResponse->wordSet->managedList;
|
||||
}
|
||||
|
||||
return $stopWords;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|array $stopWords
|
||||
* @return string
|
||||
* @throws \Apache_Meilisearch_InvalidArgumentException
|
||||
*/
|
||||
public function toJson($stopWords)
|
||||
{
|
||||
if (empty($stopWords)) {
|
||||
throw new \Apache_Meilisearch_InvalidArgumentException('Must provide stop word.');
|
||||
}
|
||||
|
||||
if (is_string($stopWords)) {
|
||||
$stopWords = [$stopWords];
|
||||
}
|
||||
|
||||
$stopWords = array_values($stopWords);
|
||||
return json_encode($stopWords);
|
||||
}
|
||||
}
|
75
Classes/System/Meilisearch/Parser/SynonymParser.php
Normal file
75
Classes/System/Meilisearch/Parser/SynonymParser.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace WapplerSystems\Meilisearch\System\Meilisearch\Parser;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2010-2016 Timo Hund <timo.hund@dkd.de
|
||||
* All rights reserved
|
||||
*
|
||||
* This script is part of the TYPO3 project. The TYPO3 project 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The GNU General Public License can be found at
|
||||
* http://www.gnu.org/copyleft/gpl.html.
|
||||
*
|
||||
* This script 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.
|
||||
*
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
|
||||
/**
|
||||
* Class to parse the synonyms from a meilisearch response.
|
||||
*
|
||||
* @author Timo Hund <timo.hund@dkd.de>
|
||||
*/
|
||||
class SynonymParser
|
||||
{
|
||||
|
||||
/**
|
||||
* Parse the meilisearch synonyms response from an json string to an array.
|
||||
*
|
||||
* @param string $baseWord
|
||||
* @param string $jsonString
|
||||
* @return array
|
||||
*/
|
||||
public function parseJson($baseWord, $jsonString)
|
||||
{
|
||||
$decodedResponse = json_decode($jsonString);
|
||||
$synonyms = [];
|
||||
if (!empty($baseWord)) {
|
||||
if (is_array($decodedResponse->{$baseWord})) {
|
||||
$synonyms = $decodedResponse->{$baseWord};
|
||||
}
|
||||
} else {
|
||||
if (isset($decodedResponse->synonymMappings->managedMap)) {
|
||||
$synonyms = (array)$decodedResponse->synonymMappings->managedMap;
|
||||
}
|
||||
}
|
||||
|
||||
return $synonyms;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $baseWord
|
||||
* @param array $synonyms
|
||||
* @return string
|
||||
* @throws \Apache_Meilisearch_InvalidArgumentException
|
||||
*/
|
||||
public function toJson($baseWord, $synonyms)
|
||||
{
|
||||
if (empty($baseWord) || empty($synonyms)) {
|
||||
throw new \Apache_Meilisearch_InvalidArgumentException('Must provide base word and synonyms.');
|
||||
}
|
||||
|
||||
return json_encode([$baseWord => $synonyms]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user