andydune/select-closest-number-from-set

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

v2.0.0 2018-09-12 07:40 UTC

This package is auto-updated.

Last update: 2024-11-13 19:25:37 UTC


README

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