first commit
This commit is contained in:
111
Classes/Domain/Index/Classification/Classification.php
Normal file
111
Classes/Domain/Index/Classification/Classification.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php declare(strict_types = 1);
|
||||
namespace WapplerSystems\Meilisearch\Domain\Index\Classification;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2010-2017 dkd Internet Service GmbH <solr-support@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 Classification
|
||||
*/
|
||||
class Classification {
|
||||
|
||||
/**
|
||||
* Array of regular expressions
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $matchPatterns = [];
|
||||
|
||||
/**
|
||||
* Array of regular expressions
|
||||
* @var array
|
||||
*/
|
||||
protected $unMatchPatterns = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $mappedClass = '';
|
||||
|
||||
/**
|
||||
* Classification constructor.
|
||||
* @param array $matchPatterns
|
||||
* @param array $unMatchPatterns
|
||||
* @param string $mappedClass
|
||||
*/
|
||||
public function __construct(array $matchPatterns = [], array $unMatchPatterns = [],string $mappedClass = '')
|
||||
{
|
||||
$this->matchPatterns = $matchPatterns;
|
||||
$this->unMatchPatterns = $unMatchPatterns;
|
||||
$this->mappedClass = $mappedClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getUnMatchPatterns(): array
|
||||
{
|
||||
return $this->unMatchPatterns;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $unMatchPatterns
|
||||
*/
|
||||
public function setUnMatchPatterns(array $unMatchPatterns)
|
||||
{
|
||||
$this->unMatchPatterns = $unMatchPatterns;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getMatchPatterns(): array
|
||||
{
|
||||
return $this->matchPatterns;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $matchPatterns
|
||||
*/
|
||||
public function setMatchPatterns(array $matchPatterns)
|
||||
{
|
||||
$this->matchPatterns = $matchPatterns;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMappedClass(): string
|
||||
{
|
||||
return $this->mappedClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $mappedClass
|
||||
*/
|
||||
public function setMappedClass(string $mappedClass)
|
||||
{
|
||||
$this->mappedClass = $mappedClass;
|
||||
}
|
||||
}
|
@@ -0,0 +1,88 @@
|
||||
<?php declare(strict_types = 1);
|
||||
namespace WapplerSystems\Meilisearch\Domain\Index\Classification;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2010-2017 dkd Internet Service GmbH <solr-support@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 ClassificationService
|
||||
*/
|
||||
class ClassificationService {
|
||||
|
||||
/**
|
||||
* @param string $stringToMatch
|
||||
* @param Classification[] $classifications
|
||||
* @return array
|
||||
*/
|
||||
public function getMatchingClassNames(string $stringToMatch, $classifications) : array
|
||||
{
|
||||
$matchingClassification = [];
|
||||
foreach ($classifications as $classification) {
|
||||
$matchingClassification = $this->applyMatchPatterns($stringToMatch, $classification, $matchingClassification);
|
||||
$matchingClassification = $this->applyUnMatchPatterns($stringToMatch, $classification, $matchingClassification);
|
||||
}
|
||||
|
||||
return array_values($matchingClassification);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $stringToMatch
|
||||
* @param Classification $classification
|
||||
* @param $matchingClassification
|
||||
* @return array
|
||||
*/
|
||||
protected function applyMatchPatterns(string $stringToMatch, $classification, $matchingClassification): array
|
||||
{
|
||||
/** @var $classification Classification */
|
||||
foreach ($classification->getMatchPatterns() as $matchPattern) {
|
||||
if (preg_match_all('~' . $matchPattern . '~ims', $stringToMatch) > 0) {
|
||||
$matchingClassification[] = $classification->getMappedClass();
|
||||
// if we found one match, we do not need to check the other patterns
|
||||
break;
|
||||
}
|
||||
}
|
||||
return array_unique($matchingClassification);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $stringToMatch
|
||||
* @param Classification $classification
|
||||
* @param $matchingClassification
|
||||
* @param $messages
|
||||
* @return array
|
||||
*/
|
||||
protected function applyUnMatchPatterns(string $stringToMatch, $classification, $matchingClassification): array
|
||||
{
|
||||
foreach ($classification->getUnMatchPatterns() as $unMatchPattern) {
|
||||
if (preg_match_all('~' . $unMatchPattern . '~ims', $stringToMatch) > 0) {
|
||||
// if we found one match, we do not need to check the other patterns
|
||||
$position = array_search($classification->getMappedClass(), $matchingClassification);
|
||||
if ($position !== false) {
|
||||
unset($matchingClassification[$position]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $matchingClassification;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user