*/ private $autoPopulationSettings; public function __construct( callable $nextHandler, array $autoPopulationSettings ) { $this->nextHandler = $nextHandler; $this->autoPopulationSettings = $autoPopulationSettings; } /** * @param Call $call * @param array $options * * @return PromiseInterface */ public function __invoke(Call $call, array $options) { $next = $this->nextHandler; if (empty($this->autoPopulationSettings)) { return $next($call, $options); } $request = $call->getMessage(); foreach ($this->autoPopulationSettings as $fieldName => $valueType) { $getFieldName = 'get' . ucwords($fieldName); // We use a getter instead of a hazzer here because there's no need to // differentiate between isset and an empty default value. Even if a // field is explicitly set to an empty string, we want to autopopulate it. if (empty($request->$getFieldName())) { $setFieldName = 'set' . ucwords($fieldName); switch ($valueType) { case Format::UUID4: $request->$setFieldName(Uuid::uuid4()->toString()); break; default: throw new \UnexpectedValueException(sprintf( 'Value type %s::%s not supported for auto population of the field %s', Format::class, Format::name($valueType), $fieldName )); } } } $call = $call->withMessage($request); return $next( $call, $options ); } }