You are here

function units_mathematical_expression_load in Units of Measurement 7.2

Load mathematical expression from database.

Parameters

int $mathematical_expression_id: Mathematical expression to load

Return value

\UnitsMathematicalExpressionWrapper Loaded mathematical expression that corresponds to the provided $mathematical_expression_id

Throws

\UnitsMathematicalExpressionMalformedException Exception is thrown if the mathematical expression appears to be poorly stored in database (unknown units, operators or anything alike)

2 calls to units_mathematical_expression_load()
units_field_field_load in units_field/units_field.module
Implements hook_field_load().
units_units_unit_load in ./units.module
Implements hook_ENTITY_TYPE_load().

File

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

Code

function units_mathematical_expression_load($mathematical_expression_id) {
  if (!$mathematical_expression_id) {
    return NULL;
  }
  $select = db_select('units_mathematical_expression_postfix', 'e');
  $select
    ->fields('e', array(
    'type',
    'value_numeric',
    'value_string',
    'postfix_order',
  ));
  $select
    ->condition('mathematical_expression_id', $mathematical_expression_id);
  $select
    ->orderBy('postfix_order');
  $operators = units_get_operator();
  $stack = array();
  foreach ($select
    ->execute() as $token) {
    switch ($token->type) {
      case UNITS_TOKEN_TYPE_CONSTANT:
        $object = new UnitsConstantMathematicalExpression($token->value_string == UNITS_QUANTITY ? $token->value_string : $token->value_numeric);
        break;
      case UNITS_TOKEN_TYPE_UNIT:
        $object = units_unit_machine_name_load($token->value_string);
        if (!$object) {
          throw new UnitsMathematicalExpressionMalformedException(t('Encountered unknown unit when loading a mathematical expression from database: %unit', array(
            '%unit' => $token->value_string,
          )));
        }
        break;
      case UNITS_TOKEN_TYPE_OPERATOR:
        if (!isset($operators[$token->value_string])) {
          throw new UnitsMathematicalExpressionMalformedException(t('Encountered unknown operator when loading a mathematical expression from database: %operator', array(
            '%operator' => $token->value_string,
          )));
        }
        $operator2 = units_mathematical_expression_postfix_stack_pop($stack);
        $operator1 = units_mathematical_expression_postfix_stack_pop($stack);
        $object = units_mathematical_expression_operator_construct($operators[$token->value_string], $operator1, $operator2);
        break;
      default:
        throw new UnitsMathematicalExpressionMalformedException(t('Encountered unknown token type when loading a mathematical expression from database: %token_type', array(
          '%token_type' => $token->type,
        )));
        break;
    }
    array_push($stack, $object);
  }
  $object = units_mathematical_expression_postfix_stack_pop($stack);
  $wrapper = new UnitsMathematicalExpressionWrapper();
  $wrapper
    ->setExpression($object);
  $wrapper
    ->setMathematicalExpressionId($mathematical_expression_id);
  return $wrapper;
}