/** * Generates and adds fake data for a ComplexValue on a entity. * * @param AttributeInterface $attribute an instance of the ComplexValue to fill with fake data. * @param array $options array of options to customize fake data creation. * * @return void */ protected function createComplexValue(AttributeInterface $attribute, array $options = array()) { $values = array(); $value_holder = $attribute->createValueHolder(); $value_type = $value_holder->getValueType(); if (class_exists($value_type) && preg_grep('#ComplexValueInterface$#', class_implements($value_type))) { foreach ($value_type::getPropertyMap() as $property_name => $property_type) { switch ($property_type) { case ComplexValue::VALUE_TYPE_TEXT: if ($this->shouldGuessByName($options)) { $values[$property_name] = TextGuesser::guess($property_name, $this->faker); } if (empty($values[$property_name])) { $values[$property_name] = $this->faker->words(3, true); } break; case ComplexValue::VALUE_TYPE_URL: $values[$property_name] = $this->faker->url; break; case ComplexValue::VALUE_TYPE_BOOLEAN: $values[$property_name] = $this->faker->boolean(); break; case ComplexValue::VALUE_TYPE_INTEGER: $values[$property_name] = $this->faker->numberBetween(0, 10); break; case ComplexValue::VALUE_TYPE_FLOAT: $values[$property_name] = $this->faker->randomFloat(5); break; case ComplexValue::VALUE_TYPE_ARRAY: $numberOfEntries = $this->faker->numberBetween(0, 3); for ($i = 0; $i < $numberOfEntries; $i++) { $values[$property_name][$this->faker->word] = $this->faker->word; } break; default: throw new BadValueException(sprintf('Unexpected ComplexValue property type "%s" on %s', $property_type, $value_type)); } } } return new $value_type($values); }
/** * @return Result */ protected function sanitizeAttributeValue(AttributeInterface $attribute, $value) { $errors = []; $sanitized_value = null; $value_holder = $attribute->createValueHolder(); $result = $value_holder->setValue($value); if ($result->getSeverity() > IncidentInterface::NOTICE) { foreach ($result->getViolatedRules() as $rule) { foreach ($rule->getIncidents() as $name => $incident) { $incident_params = $incident->getParameters(); $errors[$attribute->getName()]['@incidents'][] = ['path' => $attribute->getPath(), 'incidents' => [$name => $incident_params]]; } } } return empty($errors) ? Success::unit($value_holder->toNative()) : Error::unit($errors); }