, (*1)
Lexicology
PHP lexicology library., (*2)
While we only test PHP 7+, this library does work with PHP 5.x. PHP Sorting methods changed how they deal with equal sort values in 7+ - so suggestion arrays will differ., (*3)
- Suggests values from an array based on a lexical comparison
- Return sorted array based on lexical comparison
- Pick best match from an array
- Pick best match associations between arrays
Core Lexical Comparisons
Also allows custom lexical comparison by extending Celestial\Lexicology\Method\AbstractMethod
and implementing Celestial\Lexicology\Method\MethodInterface
. See Custom Method, (*4)
Install
Composer, (*5)
composer require celestial\lexicology
or composer.json, (*6)
{
"require": {
"celestial/lexicology": "^0.1"
}
}
Use
Suggestion
The Suggestion
class will suggest an array or value that match closely to a needle.
The default method is PregGrep but that can be changed to one of the other methods or a custom method., (*7)
<?php
use Celestial\Lexicology\Suggestion;
$suggestionOptions = [
'string',
'new string',
'value',
'variable'
];
$suggestion = new Suggestion();
$suggestions = $suggestion->getSuggestions('string', $suggestionOptions);
print_r($suggestions);
//Array
//(
// [0] => string
// [1] => new string
//)
Attempting to get a single 'best' suggestion value will return a string or throw an exception. If you need to suppress the exception and return a non-standard or shared value (such as a meta field or constant) use the fourth parameter to override the result., (*8)
<?php
use Celestial\Lexicology\Suggestion;
$suggestionOptions = [
'string',
'new string',
'value',
'variable'
];
$suggestion = new Suggestion();
$suggestions = $suggestion->getSingleSuggestion('string', $suggestionOptions);
print_r($suggestions);
// ['string']
Supressing an exception:, (*9)
<?php
use Celestial\Lexicology\Suggestion;
$suggestion = new Suggestion();
$suggestions = $suggestion->getSingleSuggestion('string',[], null, 'meta');
print_r($suggestions);
// ['meta']
Custom Method
A custom Method definition must implement either a FilterInterface
or RateInterface
, (*10)
<?php
use Celestial\Lexicology\Method\AbstractMethod;
use Celestial\Lexicology\Method\Interfaces\FilterInterface;
use Celestial\Lexicology\Method\Interfaces\SortInterface;
class CustomMethod extends AbstractMethod implements SortInterface, FilterInterface
{
use \Celestial\Lexicology\Method\Traits\SortTrait;
/**
* Return a sort value if either a or b match.
*
* @inheritdoc
*/
public function sortPair($a, $b) {
if ($a === $b) {
return 0;
} elseif ($a === $this->getField()) {
return 1;
} elseif ($b === $this->getField()) {
return -1;
}
return null;
}
/**
* Return a filter array of string that have more than 5 characters
*
* @inheritdoc
*/
public function filter($possibleValues) {
return array_values(array_filter($possibleValues, function($value){
return (strlen($value) > 5);
}));
}
}
While this custom method doesn't do anything extraordinary, it's a basic example of the interfaces for a lexical method., (*11)
<?php
use Celestial\Lexicology\Suggestion;
use Lexicology\Test\Method\CustomMethod;
$suggestionOptions = [
'string',
'strings',
'new string',
'value',
'variable'
];
$suggestion = new Suggestion();
$suggestions = $suggestion->getSuggestions('string', $suggestionOptions, CustomMethod::class);
print_r($suggestions);
//Array
//(
// [0] => new string
// [1] => strings
// [2] => variable
// [3] => string
//)