You are here

public static function StockLevel::generateSampleValue in Commerce Stock 8

@inheritDoc

Overrides FieldItemBase::generateSampleValue

1 call to StockLevel::generateSampleValue()
StockLevelTest::testSampeValueGenerator in modules/field/tests/src/Kernel/StockLevelTest.php
Tests the ::generateSampleValue() method.

File

modules/field/src/Plugin/Field/FieldType/StockLevel.php, line 190

Class

StockLevel
Plugin implementation of the 'commerce_stock_field' field type.

Namespace

Drupal\commerce_stock_field\Plugin\Field\FieldType

Code

public static function generateSampleValue(FieldDefinitionInterface $field_definition) {

  // Hint: These are our hardcoded values from the schema definitiion.
  // We could use a decimal with 15 digits, but lets keep it closer to the
  // 99% use cases. A random float between -999 and +999 should do it.
  $scale = 4;

  // (mt_rand() / $r_max) = A number between 0 and 1.
  $random_decimal = mt_rand() / mt_getrandmax() * 999 * 2 - 999;

  // @see Drupal\Core\Field\Plugin\Field\FieldTypeNumericItemBase::truncateDecimal()
  $values['value'] = floor($random_decimal * pow(10, $scale)) / pow(10, $scale);
  return $values;
}