You are here

subtract.inc in Units of Measurement 7.2

File

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

/**
 * @file
 * Declaration of subtraction cTools "operator" plugin.
 */
$plugin = array(
  'title' => t('Subtraction'),
  'description' => t('Subtract 2 values.'),
  'sign' => '-',
  'transparent operand1' => 0,
  'transparent operand2' => 0,
  'precedence' => 1,
  'dimension check' => TRUE,
  'evaluate callback' => 'units_operator_subtract_evaluate',
  'dimension callback' => 'units_operator_subtract_dimension',
  'split quantity callback' => 'units_operator_subtract_split_quantity',
  'isolate operand callback' => 'units_operator_subtract_isolate_operand',
  'sql evaluate fragment' => 'SET result = token1 - token2;',
  'operator class' => 'UnitsMathematicalOperatorLinear',
);
function units_operator_subtract_evaluate($operand1, $operand2) {
  return $operand1 - $operand2;
}
function units_operator_subtract_dimension($dimension1, $dimension2, $operator1, $operator2) {

  // We require 'dimension check', so both provided dimensions must be the same,
  // and since subtraction does not actually alter resulting dimension, we just
  // return the first dimension.
  return $dimension1;
}
function units_operator_subtract_split_quantity($total_quantity, $quantity1, $quantity2, $plugin) {

  // The easiest way to split quantity within a subtraction is to give 100% of
  // the quantity to minuend and 0 to subtrahend.
  return array(
    $total_quantity,
    $plugin['transparent operand2'],
  );
}
function units_operator_subtract_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('add'), $equal_operand, $operand2);
  }
  else {
    return units_mathematical_expression_operator_construct(units_get_operator('subtract'), $operand1, $equal_operand);
  }
}