/** * @param ParameterDefinitionInterface[] $definitions * @param $arguments * @throws InvalidParameterException for mismatched types * @return array */ public static function hydrateParameters($definitions, $arguments) { $i = 0; $parameters = []; foreach ($definitions as $parameterDefinition) { $type = $parameterDefinition->getType(); if (in_array($type, InvalidParameterException::$scalarTypes, true)) { $validator = 'is_' . $type; if (!$validator($arguments[$i])) { throw InvalidParameterException::forTypeMismatch(['string', 'int', 'integer', 'bool'], $arguments[$i]); } } else { $class = $type; if (!class_exists($class)) { throw new RuntimeException(sprintf('Class `%s` does not exist', $class)); } if (!$arguments[$i] instanceof $class) { throw InvalidParameterException::forTypeMismatch($class, $arguments[$i]); } } $parameters[$parameterDefinition->getName()] = $arguments[$i]; $i++; } return $parameters; }
/** * @test */ public function it_returns_correct_message_for_multiple_classes() { $exception = InvalidParameterException::forTypeMismatch([\stdClass::class, \Exception::class], '1'); static::assertEquals('Expected an instance of classes stdClass, Exception; got string instead', $exception->getMessage()); }