You are here

public static function DoubleFieldField::generateValue in Double Field 7.2

Generate field value.

1 call to DoubleFieldField::generateValue()
DoubleFieldWidget::__construct in tests/double_field_widget.inc
Constructor for DoubleFieldWidget.

File

tests/double_field_field.inc, line 41
The file contains a class that assists to test Double Field field settings.

Class

DoubleFieldField
Helper class to test Double Field field settings.

Code

public static function generateValue($subfield_settings) {
  switch ($subfield_settings['type']) {
    case 'varchar':
      $value = DrupalTestCase::randomName($subfield_settings['maxlength']);
      break;
    case 'text':

      // 128 is default maxlength in form API.
      $value = DrupalTestCase::randomName(128);
      break;
    case 'int':

      // These size values based on mysql limitation because pgsql
      // and sqlite have higher level of restrictions.
      // See http://dev.mysql.com/doc/refman/5.0/en/integer-types.html.
      $sizes = array(
        'tiny' => array(
          -128,
          127,
        ),
        'small' => array(
          -32768,
          32767,
        ),
        'medium' => array(
          -8388608,
          8388607,
        ),
        'normal' => array(
          -mt_getrandmax(),
          -mt_getrandmax(),
        ),
        'big' => array(
          -mt_getrandmax(),
          mt_getrandmax(),
        ),
      );
      $value = mt_rand($sizes[$subfield_settings['size']][0], $sizes[$subfield_settings['size']][1]);
      break;
    case 'float':

      // 2 Bytes?
      $value = mt_rand(-32768, 32767) / pow(10, mt_rand(0, 5));
      break;
    case 'decimal':
      $integer = DoubleFieldTestCase::randomNumber($subfield_settings['precision'] - $subfield_settings['scale']);
      $fracional = DoubleFieldTestCase::randomNumber($subfield_settings['scale']);
      $value = trim($integer . '.' . $fracional, '0');
      break;
  }
  return $value;
}