You are here

divide.inc in Units of Measurement 7.2

File

plugins/operator/divide.inc
View source
<?php

/**
 * @file
 * Declaration of divide cTools "operator" plugin.
 */
$plugin = array(
  'title' => t('Divide'),
  'description' => t('Divide 2 values.'),
  'sign' => '/',
  'transparent operand1' => 1,
  'transparent operand2' => 1,
  'precedence' => 2,
  'dimension check' => FALSE,
  'evaluate callback' => 'units_operator_divide_evaluate',
  'dimension callback' => 'units_operator_divide_dimension',
  'split quantity callback' => 'units_operator_divide_split_quantity',
  'isolate operand callback' => 'units_operator_divide_isolate_operand',
  'sql evaluate fragment' => 'SET result = token1 / token2;',
  'operator class' => 'UnitsMathematicalOperatorLinear',
);
function units_operator_divide_evaluate($operand1, $operand2) {
  if (floatval($operand2) == 0) {
    throw new UnitsMathematicalExpressionMalformedException(t('Division by zero.'));
  }
  return $operand1 / $operand2;
}
function units_operator_divide_dimension($dimension1, $dimension2, $operator1, $operator2) {
  return units_dimension_subtract($dimension1, $dimension2);
}
function units_operator_divide_split_quantity($total_quantity, $quantity1, $quantity2, $plugin) {

  // Dead easy: we'll just divide by 1 to keep it consistent.
  return array(
    $total_quantity,
    $plugin['transparent operand2'],
  );
}
function units_operator_divide_isolate_operand(UnitsMathematicalExpression $operand1, UnitsMathematicalExpression $operand2, UnitsMathematicalExpression $equal_operand, $operand_to_isolate) {
  if ($operand_to_isolate == 1) {
    return units_mathematical_expression_operator_construct(units_get_operator('multiply'), $equal_operand, $operand2);
  }
  else {
    return units_mathematical_expression_operator_construct(units_get_operator('divide'), $operand1, $equal_operand);
  }
}