Ejemplo n.º 1
0
 /**
  * @param array $batch
  * @return BatchRequest
  */
 public static function fromArray(array $batch)
 {
     $requests = [];
     foreach ($batch as $requestArray) {
         if (!is_array($requestArray)) {
             throw new InvalidArgumentException();
         }
         $requests[] = Request::fromArray($requestArray);
     }
     return new self($requests);
 }
Ejemplo n.º 2
0
 /**
  * @param ReflectionMethod $Method
  * @param Request $Request
  * @return array
  * @throws InvalidParameter
  * @throws MissingParameter
  * @throws TooManyParameters
  */
 private function getParametersForMethodAndRequest(ReflectionMethod $Method, Request $Request)
 {
     $parameters = $Request->getParams();
     $methodParameters = $Method->getParameters();
     $isAssociative = $this->isAssociative($parameters);
     $n = 0;
     $parametersByPosition = [];
     foreach ($methodParameters as $ExpectedParameter) {
         $key = $isAssociative ? $ExpectedParameter->getName() : $n;
         if (!array_key_exists($key, $parameters)) {
             if (!$ExpectedParameter->isOptional()) {
                 throw new MissingParameter('Required parameter <' . $ExpectedParameter->getName() . '> was not passed');
             }
             if (count($parameters) == 0) {
                 continue;
             }
             $parametersByPosition[] = $ExpectedParameter->getDefaultValue() ?: null;
             continue;
         }
         $parameter = $parameters[$key];
         unset($parameters[$key]);
         $ExpectedClass = $ExpectedParameter->getClass();
         if (!is_null($ExpectedClass) && (!is_object($parameter) || !$ExpectedClass->isInstance($parameter))) {
             throw new InvalidParameter('Parameter <' . $ExpectedParameter->getName() . '> must be of type <' . $ExpectedClass->getName() . '>');
         }
         if ($ExpectedParameter->isArray() && !is_array($parameter)) {
             throw new InvalidParameter('Parameter <' . $ExpectedParameter->getName() . '> must be an array');
         }
         $parametersByPosition[] = $parameter;
         $n++;
     }
     if (count($parameters) == 0) {
         return $parametersByPosition;
     }
     if (isset($ExpectedParameter) && !$isAssociative) {
         // Check if the method has a variadic argument that will accept any number of arguments
         if (method_exists($ExpectedParameter, 'isVariadic') && $ExpectedParameter->isVariadic()) {
             return array_merge($parametersByPosition, $parameters);
         }
     }
     throw new TooManyParameters();
 }
Ejemplo n.º 3
0
 public function testThatWithIdDoesNotChangeRequest()
 {
     $Request = new Request('foo');
     $this->assertSame('foo', $Request->getMethod());
     $this->assertSame([], $Request->getParams());
     $this->assertNull($Request->getId());
     $OtherRequest = $Request->withId(123);
     $this->assertSame('foo', $Request->getMethod());
     $this->assertSame([], $Request->getParams());
     $this->assertNull($Request->getId());
     $this->assertSame('foo', $OtherRequest->getMethod());
     $this->assertSame([], $OtherRequest->getParams());
     $this->assertSame(123, $OtherRequest->getId());
 }