number.devel_generate.inc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. // Id$
  3. function number_devel_generate($object, $field, $instance, $bundle) {
  4. if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
  5. return devel_generate_multiple('_number_devel_generate', $object, $field, $instance, $bundle);
  6. }
  7. else {
  8. return _number_devel_generate($object, $field, $instance, $bundle);
  9. }
  10. }
  11. function _number_devel_generate($object, $field, $instance, $bundle) {
  12. $object_field = array();
  13. // Make sure the instance settings are all set.
  14. foreach (array('min', 'max', 'precision', 'scale') as $key) {
  15. if (empty($instance['settings'][$key])) {
  16. $instance['settings'][$key] = NULL;
  17. }
  18. }
  19. $min = is_numeric($instance['settings']['min']) ? $instance['settings']['min'] : 0;
  20. switch ($field['type']) {
  21. case 'number_integer':
  22. $max = is_numeric($instance['settings']['max']) ? $instance['settings']['max'] : 10000;
  23. $decimal = 0;
  24. $scale = 0;
  25. break;
  26. case 'number_decimal':
  27. $precision = is_numeric($instance['settings']['precision']) ? $instance['settings']['precision'] : 10;
  28. $scale = is_numeric($instance['settings']['scale']) ? $instance['settings']['scale'] : 2;
  29. $max = is_numeric($instance['settings']['max']) ? $instance['settings']['max'] : pow(10, ($precision - $scale));
  30. $decimal = rand(0, (10 * $scale)) / 100;
  31. break;
  32. case 'number_float':
  33. $precision = rand(10, 32);
  34. $scale = rand(0, 2);
  35. $decimal = rand(0, (10 * $scale)) / 100;
  36. $max = is_numeric($instance['settings']['max']) ? $instance['settings']['max'] : pow(10, ($precision - $scale));
  37. break;
  38. }
  39. $object_field['value'] = round((rand($min, $max) + $decimal), $scale);
  40. return $object_field;
  41. }