You are here

protected function FractionTarget::prepareValue in Fraction 8

Same name and namespace in other branches
  1. 2.x src/Feeds/Target/FractionTarget.php \Drupal\fraction\Feeds\Target\FractionTarget::prepareValue()

Prepares a single value.

Parameters

int $delta: The field delta.

array $values: The values.

Overrides FieldTargetBase::prepareValue

File

src/Feeds/Target/FractionTarget.php, line 67

Class

FractionTarget
Defines a fraction field mapper.

Namespace

Drupal\fraction\Feeds\Target

Code

protected function prepareValue($delta, array &$values) {
  $item = trim($values['value']);
  unset($values['value']);
  switch ($this->configuration['type']) {
    case 'fraction':

      // Pull out the numerator and denominator.
      $parts = explode('/', $item);
      if (!empty($parts[0]) && is_numeric($parts[0]) && !empty($parts[1]) && is_numeric($parts[1]) && $parts[1] >= 0) {
        $values['numerator'] = $parts[0];
        $values['denominator'] = $parts[1];
      }
      else {
        $values['numerator'] = '';
        $values['denominator'] = '';
      }
      break;
    case 'decimal':

      // If item is non-numeric or an empty string, create an empty fraction.
      // Otherwise, pass the value to createFromDecimal().
      if (!is_numeric($item) || $item === '') {
        $values['numerator'] = '';
        $values['denominator'] = '';
      }
      else {
        $fraction = Fraction::createFromDecimal($item);
        $values['numerator'] = $fraction
          ->getNumerator();
        $values['denominator'] = $fraction
          ->getDenominator();
      }
      break;
  }
}