You are here

class UnitsConstantMathematicalExpression in Units of Measurement 7.2

Implementation of "mathematical expression" interface for a constant.

Implementation of "mathematical expression" interface for a dimensionless constant.

Hierarchy

Expanded class hierarchy of UnitsConstantMathematicalExpression

File

./units.module, line 1733
Provide API for managing and converting units of measurement.

View source
class UnitsConstantMathematicalExpression implements UnitsMathematicalExpression {

  /**
   * Constant that is held in this mathematical expression.
   *
   * @var float
   */
  protected $constant;

  /**
   * UnitsConstantMathematicalExpression constructor.
   *
   * @param int|float $constant
   *   Constant that should be held in this mathematical expression
   */
  public function __construct($constant) {
    $this->constant = $constant;
  }

  /**
   * Determine physical dimension of this mathematical expression.
   *
   * @return array
   *   Dimension array of this mathematical expression
   */
  public function dimension() {
    return array();
  }

  /**
   * Test whether this mathematical expression includes a dimensionless member.
   *
   * Whether this mathematical expression contains at least 1 dimensionless
   * member.
   *
   * @return bool
   *   Whether this mathematical expression contains at least 1 dimensionless
   *   member
   */
  public function containsDimensionlessMember() {
    return TRUE;
  }

  /**
   * Format a certain amount of quantity within this mathematical expression.
   *
   * @param float $quantity
   *   Quantity to be formatted
   *
   * @return UnitsMathematicalExpression
   *   Formatted quantity into this mathematical expression. Sometimes the
   *   mathematical expression itself must mutate in order to format the
   *   quantity. So the returned mathematical expression may not necessarily be
   *   the mathematical expression on which this method was invoked. For
   *   example, the expression "unit" would mutate into "1 * unit" in order to
   *   have a dimensionless member and therefore be able to format the $quantity
   */
  public function formatQuantity($quantity) {
    $this->constant = $quantity;
    return $this;
  }

  /**
   * Numerically evaluate this mathematical expression.
   *
   * @return float
   *   Numerical value of this mathematical expression
   */
  public function evaluate() {
    return $this->constant;
  }

  /**
   * Decompose (simplify) this mathematical expression.
   *
   * @return UnitsMathematicalExpression
   *   Decomposed (simplified) version of this mathematical expression
   */
  public function decompose() {
    return $this;
  }

  /**
   * Represent this mathematical expression in postfix notation.
   *
   * Represent this mathematical expression in postfix (reverse polish)
   * notation.
   *
   * @param array $options
   *   Options regarding how to format this mathematical expression
   *
   * @return string
   *   Postfix (reverse polish) notation representation of this mathematical
   *   expression
   */
  public function toPostfix($options = array()) {
    return $this->constant;
  }

  /**
   * Represent this mathematical expression in human-friendly infix notation.
   *
   * @param array $options
   *   Options regarding how to format this mathematical expression
   *
   * @return string
   *   Human-friendly formatted version of this mathematical expression taking
   *   into account provided $options
   */
  public function toInfix($options = array()) {
    return $this->constant;
  }

  /**
   * Whether this expression is linearly decomposable.
   *
   * @return bool
   *   Whether this expression is linearly decomposable, i.e. its decomposition
   *   can be plugged in into another mathematical expression via multiplication
   *   without losing sense
   */
  public function isLinear() {
    return TRUE;
  }

  /**
   * Save the mathematical expression into database.
   *
   * @param int $mathematical_expression_id
   *   If the ID of the mathematical expression is known, under which it should
   *   be saved, provide it here. Otherwise it will be generated automatically
   * @param int $order
   *   Order of this member when the mathematical expression is written down in
   *   postfix notation. Primarily this argument is used for internal purposes
   *
   * @return int
   *   Mathematical expression ID under which this expression has been save in
   *   the database
   */
  public function unitsMathematicalExpressionSave($mathematical_expression_id, &$order) {
    if (!$mathematical_expression_id) {

      // TODO: this should be possible to do as: INSERT INTO ... FROM SELECT ... thereby making unnecessary the transaction.
      // See https://www.drupal.org/node/310079 for more details.
      $transaction = db_transaction();
      $select = db_select('units_mathematical_expression_postfix', 'e');
      $select
        ->addExpression('MAX(e.mathematical_expression_id) + 1', 'mathematical_expression_id');
      $mathematical_expression_id = $select
        ->execute()
        ->fetchField();
      if (!$mathematical_expression_id) {
        $mathematical_expression_id = 1;
      }
    }
    db_insert('units_mathematical_expression_postfix')
      ->fields(array(
      'type' => UNITS_TOKEN_TYPE_CONSTANT,
      'mathematical_expression_id' => $mathematical_expression_id,
      $this->constant == UNITS_QUANTITY ? 'value_string' : 'value_numeric' => $this->constant,
      'postfix_order' => ++$order,
    ))
      ->execute();
    return $mathematical_expression_id;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UnitsConstantMathematicalExpression::$constant protected property Constant that is held in this mathematical expression.
UnitsConstantMathematicalExpression::containsDimensionlessMember public function Test whether this mathematical expression includes a dimensionless member. Overrides UnitsMathematicalExpression::containsDimensionlessMember
UnitsConstantMathematicalExpression::decompose public function Decompose (simplify) this mathematical expression. Overrides UnitsMathematicalExpression::decompose
UnitsConstantMathematicalExpression::dimension public function Determine physical dimension of this mathematical expression. Overrides UnitsMathematicalExpression::dimension
UnitsConstantMathematicalExpression::evaluate public function Numerically evaluate this mathematical expression. Overrides UnitsMathematicalExpression::evaluate
UnitsConstantMathematicalExpression::formatQuantity public function Format a certain amount of quantity within this mathematical expression. Overrides UnitsMathematicalExpression::formatQuantity
UnitsConstantMathematicalExpression::isLinear public function Whether this expression is linearly decomposable. Overrides UnitsMathematicalExpression::isLinear
UnitsConstantMathematicalExpression::toInfix public function Represent this mathematical expression in human-friendly infix notation. Overrides UnitsMathematicalExpression::toInfix
UnitsConstantMathematicalExpression::toPostfix public function Represent this mathematical expression in postfix notation. Overrides UnitsMathematicalExpression::toPostfix
UnitsConstantMathematicalExpression::unitsMathematicalExpressionSave public function Save the mathematical expression into database. Overrides UnitsMathematicalExpression::unitsMathematicalExpressionSave
UnitsConstantMathematicalExpression::__construct public function UnitsConstantMathematicalExpression constructor.