You are here

class GetWidthPos in Length Indicator 8

Class GetWidthPos.

@package Drupal\length_indicator

Hierarchy

Expanded class hierarchy of GetWidthPos

1 file declares its use of GetWidthPos
LengthIndicatorTest.php in tests/src/Unit/LengthIndicatorTest.php
1 string reference to 'GetWidthPos'
length_indicator.services.yml in ./length_indicator.services.yml
length_indicator.services.yml
1 service uses GetWidthPos
length_indicator.get_width_pos in ./length_indicator.services.yml
Drupal\length_indicator\GetWidthPos

File

src/GetWidthPos.php, line 10

Namespace

Drupal\length_indicator
View source
class GetWidthPos {

  /**
   * Gets the widths and positions for the indicator template.
   *
   * @param int $optimin
   *   The optimum minimum.
   * @param int $optimax
   *   The optimum maximum. This must be larger than $optimin. This is validated
   *   on the widget settings form.
   * @param int $tolerance
   *   The tolerance. This must be smaller than $optimin. This is validated on
   *   the widget settings form.
   *
   * @return array
   *   The widths and positions of the indicators.
   */
  public function getWidthAndPosition($optimin, $optimax, $tolerance) {
    $indicators = [];
    $min = $optimin - $tolerance;
    $max = $optimax + $tolerance;
    $total = $max + $min;
    $width = $min / $total * 100;
    $indicators[0]['width'] = $width;
    $indicators[0]['pos'] = 0;
    $indicators[0]['class'] = 'length-indicator__indicator--bad';

    // Adding +1 to make max inclusive.
    $indicators[4]['width'] = $width;
    $indicators[4]['pos'] = $max + 1;
    $indicators[4]['class'] = 'length-indicator__indicator--bad';
    $last = $width;
    $width = $optimin / $total * 100;
    $indicators[1]['width'] = $width - $last;
    $indicators[1]['pos'] = $min;
    $indicators[1]['class'] = 'length-indicator__indicator--ok';
    $last = $width;
    $width = $optimax / $total * 100;
    $indicators[2]['width'] = $width - $last;
    $indicators[2]['pos'] = $optimin;
    $indicators[2]['class'] = 'length-indicator__indicator--good';
    $last = $width;
    $width = $max / $total * 100;

    // Adding +1 to make optimax inclusive.
    $indicators[3]['width'] = $width - $last;
    $indicators[3]['pos'] = $optimax + 1;
    $indicators[3]['class'] = 'length-indicator__indicator--ok';
    ksort($indicators);
    return $indicators;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GetWidthPos::getWidthAndPosition public function Gets the widths and positions for the indicator template.