You are here

public function FractionDecimalProperty::getValue in Fraction 8

Same name and namespace in other branches
  1. 2.x src/FractionDecimalProperty.php \Drupal\fraction\FractionDecimalProperty::getValue()

Implements \Drupal\Core\TypedData\TypedDataInterface::getValue().

Overrides TypedData::getValue

File

src/FractionDecimalProperty.php, line 22

Class

FractionDecimalProperty
A computed property for storing the fraction decimal value for validation.

Namespace

Drupal\fraction

Code

public function getValue($langcode = NULL) {

  // If a value is already available, return it.
  if ($this->decimal !== NULL) {
    return $this->decimal;
  }

  // Load the parent item.
  $item = $this
    ->getParent();

  // Otherwise, create a Fraction object.
  $fraction = new Fraction($item->numerator, $item->denominator);

  // Fallback to precision 9 to ensure validation.
  $this->decimal = $fraction
    ->toDecimal(9, TRUE);
  return $this->decimal;
}