Unlike MethodParser, which parses only a single method, the ParameterParser class parses all the parameters on a single method.
Esempio n. 1
0
 public function testParse()
 {
     $className = '\\Webiny\\Component\\Rest\\Tests\\Mocks\\MockApiClass';
     $methodName = 'someMethod';
     // get method annotations
     $annotations = $this->annotationsFromMethod($className, $methodName);
     $paramAnnotations = $annotations->get('param', new ConfigObject([]));
     // extract params
     $reflection = new \ReflectionClass($className);
     $method = $reflection->getMethod('someMethod');
     $params = $method->getParameters();
     $instance = new ParameterParser($params, $paramAnnotations);
     $parsedParams = $instance->parse();
     $this->assertInternalType('array', $parsedParams);
     $this->assertInstanceOf('\\Webiny\\Component\\Rest\\Parser\\ParsedParameter', $parsedParams[0]);
     // validate parameters
     $this->assertCount(3, $parsedParams);
     // param 1
     $this->assertSame('param1', $parsedParams[0]->name);
     $this->assertSame('string', $parsedParams[0]->type);
     $this->assertTrue($parsedParams[0]->required);
     // param 2
     $this->assertSame('param2', $parsedParams[1]->name);
     $this->assertSame('string', $parsedParams[1]->type);
     $this->assertFalse($parsedParams[1]->required);
     // param 3
     $this->assertSame('param3', $parsedParams[2]->name);
     $this->assertSame('integer', $parsedParams[2]->type);
     $this->assertFalse($parsedParams[2]->required);
 }
Esempio n. 2
0
 /**
  * Parse the method and return an instance of ParsedMethod.
  *
  * @return ParsedMethod
  */
 public function parse()
 {
     $annotations = ['rest' => []];
     foreach ($this->classes as $c) {
         $classAnnotations = $this->annotationsFromClass($c->getName())->toArray();
         if (isset($classAnnotations['rest'])) {
             $annotations['rest'] = array_merge($classAnnotations['rest'], $annotations['rest']);
         }
     }
     $annotations = new ConfigObject($annotations);
     $this->classDefaults = $annotations->get('rest', new ConfigObject([]));
     // get method annotations
     $annotations = ['rest' => [], 'param' => []];
     foreach ($this->classes as $c) {
         try {
             $methodAnnotations = $this->annotationsFromMethod($c->getName(), $this->method->name)->toArray();
         } catch (\Exception $e) {
             continue;
         }
         if (isset($methodAnnotations['rest']) && is_array($methodAnnotations['rest'])) {
             $annotations['rest'] = array_merge($methodAnnotations['rest'], $annotations['rest']);
         }
         if (isset($methodAnnotations['param'])) {
             if (is_array($methodAnnotations['param'])) {
                 $annotations['param'] = array_merge($methodAnnotations['param'], $annotations['param']);
             } else {
                 $annotations['param'][] = $methodAnnotations['param'];
             }
         }
     }
     $annotations = new ConfigObject($annotations);
     $restAnnotations = $annotations->get('rest', new ConfigObject([]));
     $paramAnnotations = $annotations->get('param', new ConfigObject([]));
     // check if we should ignore this method
     if ($restAnnotations->get('ignore', false) !== false) {
         return false;
     }
     // create ApiMethod instance
     $parsedMethod = new ParsedMethod($this->method->name);
     // method
     $parsedMethod->method = $this->getMethod($restAnnotations);
     // role
     $parsedMethod->role = $this->getRole($restAnnotations);
     // cache.ttl
     $parsedMethod->cache = $this->getCache($restAnnotations);
     // header.cache.expires
     $parsedMethod->header = $this->getHeader($restAnnotations);
     // default
     $parsedMethod->default = $this->getDefault($restAnnotations);
     // rateControl.ignore
     $parsedMethod->rateControl = $this->getRateControl($restAnnotations);
     // parse method parameters
     $parameterParser = new ParameterParser($this->method->getParameters(), $paramAnnotations);
     $parameters = $parameterParser->parse();
     foreach ($parameters as $p) {
         $parsedMethod->addParameter($p);
     }
     // build the url pattern
     if ($restAnnotations->get('url', false)) {
         // build url pattern using the provided rest.url pattern
         $urlPattern = $this->buildUrlPatternFromPattern($restAnnotations->get('url'), $parameters);
         $resourceNaming = true;
     } else {
         // build url pattern using method name and parameters
         $urlPattern = $this->buildUrlPatternStandard($this->method->name, $parameters);
         $resourceNaming = false;
     }
     $parsedMethod->setUrlPattern($urlPattern, $resourceNaming);
     return $parsedMethod;
 }