/** * @test * @covers ::getParameter * @uses \AyeAye\Api\Request */ public function testGetParameter() { $request = new Request(null, null, null, ['key' => 'value1', 'Key' => 'value2']); $this->assertSame('value1', $request->getParameter('key', 'default')); $this->assertSame('value2', $request->getParameter('Key', 'default')); $this->assertSame('value1', $request->getParameter('KEY', 'default')); $this->assertSame('default', $request->getParameter('KEYZ', 'default')); }
/** * Creates an array of parameters with which to call a method. * * Matches the parameters in a given method to the values available in the * given request. For absent values, either the default, or null, will be * used. * * @param \ReflectionMethod $method * @param Request $request * @return array */ protected function mapRequestToArguments(\ReflectionMethod $method, Request $request) { $map = []; foreach ($method->getParameters() as $parameter) { $value = $request->getParameter($parameter->getName(), $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null); if ($parameter->getClass() && $parameter->getClass()->implementsInterface(Deserializable::class)) { /** @var Deserializable $deserializable */ $value = $parameter->getClass()->newInstanceWithoutConstructor()->ayeAyeDeserialize($value); $className = $parameter->getClass()->getName(); if (!is_object($value) || get_class($value) !== $className) { throw new \RuntimeException("{$className}::ayeAyeDeserialize did not return an instance of itself"); } } $map[$parameter->getName()] = $value; } return $map; }