Ejemplo n.º 1
0
 /**
  * @param string $requestUrl
  * @param array $query
  * @return void
  */
 public function parse($requestUrl, array $query = [])
 {
     foreach ($this->getAcceptedParameters() as $parameter) {
         $r = false;
         $name = $parameter->getName();
         $dataType = $parameter->getDataType();
         if (array_key_exists($name, $query) === false) {
             $this->parameters[$name] = $parameter->getDefaultValue();
         } else {
             if ($parameter->getDefaultValue() == null && array_key_exists($name, $query) == false) {
                 $r = false;
             } else {
                 // validate arrays here as builtin validation relies on strings
                 if ($parameter->expects('array') == true) {
                     if (is_array($query[$name]) === true) {
                         $r = true;
                     }
                 } else {
                     $r = preg_match(sprintf('/%s/i', Parameter::getValidatorPattern($dataType)), $query[$name]);
                 }
             }
             if ($r === 0 || $r === false) {
                 // FIXME
                 $app->abort(400, sprintf('Bad value for \'%s\', expected \'%s\'.', $name, $dataType));
             }
             $this->parameters[$name] = $this->processValue($query[$name]);
         }
     }
     // comma separated lists but we want arrays
     $this->parameters['embed'] = strlen($this->parameters['embed']) ? explode(',', $this->parameters['embed']) : [];
     $this->parameters['fields'] = strlen($this->parameters['fields']) ? explode(',', $this->parameters['fields']) : [];
     $this->convertDottedNotation($this->parameters['embed']);
     $this->convertDottedNotation($this->parameters['fields']);
 }
 private function processAction(CompiledActionDefinitionInterface $action)
 {
     $href = $action->getEndpointUrl();
     $routeName = $action->getRouteName();
     $controller = sprintf('%s::%s', $action->getResourceDefinition()->getControllerClass(), 'handle');
     $defaults = ['_controller' => $controller, '_format' => 'json', '_rested' => ['action' => $action->getType(), 'controller' => $action->getControllerName(), 'route_name' => $routeName]];
     // add constraints and validators to the cache
     $requirements = [];
     foreach ($action->getTokens() as $token) {
         if ($token->acceptAnyValue() === false) {
             $requirements[$token->getName()] = Parameter::getValidatorPattern($token->getDataType());
         }
     }
     $route = new Route($href, $defaults, $requirements, [], '', [], $action->getHttpMethod());
     $this->routes->add($routeName, $route);
 }
Ejemplo n.º 3
0
 /**
  * @return null|string
  */
 public function getTypeValidatorName()
 {
     return Parameter::getValidator($this->getDataType());
 }
 private function addRoutesFromResourceDefinition(CompilerInterface $compiler, CompilerCacheInterface $cache, Router $router, ResourceDefinition $definition, $resourceClass)
 {
     $compiledDefinition = $compiler->compile($definition);
     foreach ($compiledDefinition->getActions() as $action) {
         $href = $action->getEndpointUrl(false);
         $routeName = $action->getRouteName();
         $controller = sprintf('%s@preHandle', $resourceClass);
         $route = $router->{$action->getHttpMethod()}($href, ['as' => $routeName, 'uses' => $controller, '_rested' => ['action' => $action->getType(), 'controller' => $action->getControllerName(), 'route_name' => $routeName]]);
         // add constraints and validators to the cache
         foreach ($action->getTokens() as $token) {
             if ($token->acceptAnyValue() === false) {
                 $route->where($token->getName(), Parameter::getValidatorPattern($token->getDataType()));
             }
         }
         $cache->registerResourceDefinition($routeName, $compiledDefinition);
     }
 }