celestial/lexicology

PHP Lexicology library.

0.0.3 2017-08-22 05:38 UTC

This package is not auto-updated.

Last update: 2024-10-27 03:16:16 UTC


README

Build Status

Lexicology

PHP lexicology library.

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.

  • 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

Install

Composer

composer require celestial\lexicology

or composer.json

{
   "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.

<?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.

<?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:

<?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

<?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.

<?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
//)