summaryrefslogtreecommitdiffstats
path: root/devel_generate/number.devel_generate.inc
blob: 372942ccb2ed164b316a588e3e1c451c8a31f523 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
// Id$

function number_devel_generate($object, $field, $instance, $bundle) {
  if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
    return devel_generate_multiple('_number_devel_generate', $object, $field, $instance, $bundle);
  }
  else {
    return _number_devel_generate($object, $field, $instance, $bundle);
  }
}

function _number_devel_generate($object, $field, $instance, $bundle) {
  $object_field = array();
  // Make sure the instance settings are all set.
  foreach (array('min', 'max', 'precision', 'scale') as $key) {
    if (empty($instance['settings'][$key])) {
      $instance['settings'][$key] = NULL;
    }
  }
  $min = is_numeric($instance['settings']['min']) ? $instance['settings']['min'] : 0;
  switch ($field['type']) {
    case 'number_integer':
      $max = is_numeric($instance['settings']['max']) ? $instance['settings']['max'] : 10000;
      $decimal = 0;
      $scale = 0;
      break;

    case 'number_decimal':
      $precision = is_numeric($instance['settings']['precision']) ? $instance['settings']['precision'] : 10;
      $scale = is_numeric($instance['settings']['scale']) ? $instance['settings']['scale'] : 2;
      $max = is_numeric($instance['settings']['max']) ? $instance['settings']['max'] : pow(10, ($precision - $scale));
      $decimal = rand(0, (10 * $scale)) / 100;
      break;

    case 'number_float':
      $precision = rand(10, 32);
      $scale = rand(0, 2);
      $decimal = rand(0, (10 * $scale)) / 100;
      $max = is_numeric($instance['settings']['max']) ? $instance['settings']['max'] : pow(10, ($precision - $scale));
      break;
  }
  $object_field['value'] = round((rand($min, $max) + $decimal), $scale);
  return $object_field;
}