AndyDune / SelectClosestNumberFromSet

It helps to narrow down lange set of input numbers to specified.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SelectClosestNumberFromSet

It helps to narrow down large set of input numbers to specified set.

Build Status Software License Packagist Version Total Downloads

It offers convenient interface for incapsulated array. Implaments strategy template for any number of filters.

Installation

Installation using composer:

composer require andydune/select-closest-number-from-set

Or if composer was not installed globally:

php composer.phar require andydune/select-closest-number-from-set

Or edit your composer.json:

"require" : {
     "andydune/select-closest-number-from-set": "^2"
}

And execute command:

php composer.phar update

Solve tasks

There is hard limited set numbers is allowed. But in the input can be any number. We need to select the closest number from allowed set to use next.

This class can help to solve this task.

By default script selects low close number:

use AndyDune\SelectClosestNumberFromSet;
$object = new SelectClosestNumberFromSet([0, 20, 40]);
$next = $object->select(-5); // = 0
$next = $object->select(5); // = 0
$next = $object->select(39); // = 20
$next = $object->select(100); // = 40

Use method selectHigh to select high allowed number:

use AndyDune\SelectClosestNumberFromSet;
$object = new SelectClosestNumberFromSet([0, 20, 40]);
$object->selectHigh();
$next = $object->select(-5); // = 0
$next = $object->select(5); // = 20
$next = $object->select(39); // = 40
$next = $object->select(100); // = 40

About

It helps to narrow down lange set of input numbers to specified.

License:MIT License


Languages

Language:PHP 100.0%