mcordingley/HashBin

View on GitHub
src/BinStrategies/Multiply.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace mcordingley\HashBin\BinStrategies;

use mcordingley\HashBin\BinStrategy;

final class Multiply implements BinStrategy
{
    private $coefficient;

    public function __construct($coefficient = 0.61803398874989)
    {
        $this->coefficient = $coefficient;
    }

    public function bin(int $code, int $min, int $max): int
    {
        $product = $this->coefficient * $code;
        $fractional = $product - floor($product);

        return floor($fractional * ($max - $min + 1)) + $min;
    }
}