Пример #1
0
 public function readEntity(RoutingContextInterface $context, \ReflectionClass $ref, &$isRead)
 {
     if ($ref->name !== JsonEntity::class) {
         return;
     }
     $isRead = true;
     $request = $context->getRequest();
     return new JsonEntity(@json_decode($request->getContents(), true));
 }
Пример #2
0
 public function readEntity(RoutingContextInterface $context, \ReflectionClass $ref, &$isRead)
 {
     if ($ref->name !== \DOMDocument::class) {
         return;
     }
     $request = $context->getRequest();
     if (!$request->hasEntity()) {
         return;
     }
     $isRead = true;
     $builder = new XmlDocumentBuilder();
     return $builder->buildFromSource($request->getEntity()->getContents());
 }
Пример #3
0
 public function readEntity(RoutingContextInterface $context, \ReflectionClass $ref, &$isRead)
 {
     if (!$ref->implementsInterface(EntityInterface::class)) {
         return;
     }
     $request = $context->getRequest();
     if (!$request->hasEntity()) {
         return;
     }
     $entity = $request->getEntity();
     if ($ref->isInstance($entity)) {
         $isRead = true;
         return $entity;
     }
 }
Пример #4
0
 public function readEntity(RoutingContextInterface $context, \ReflectionClass $ref, &$isRead)
 {
     if (!$ref->isSubclassOf(TransferObject::class)) {
         return;
     }
     $isRead = true;
     $data = [];
     $request = $context->getRequest();
     $mediaType = $request->getMediaType();
     if ($mediaType->is('*/json')) {
         $data = (array) json_decode($requets->getContents(), true);
     } elseif ($mediaType->is(Http::FORM_ENCODED) || $mediaType->is(Http::FORM_MULTIPART_ENCODED)) {
         $entity = $request->getEntity();
         if ($entity instanceof FormEntityInterface) {
             $data = $entity->getFields();
         }
     }
     $object = $context->get($ref->name);
     if ($object instanceof TransferObject) {
         $object->populateInputData($data);
     }
     return $object;
 }
Пример #5
0
 public function transformActionArguments(\ReflectionFunctionAbstract $ref, RoutingContextInterface $context)
 {
     $request = $context->getRequest();
     $match = $context->getRouteMatch();
     $instances = [$context, $request, $match];
     $args = [];
     $resolverNeeded = [];
     // Merge query params with route params, route params have preceedence.
     $params = Arrays::mergeDeep($request->getUri()->getQuery(), $match->getParameters());
     foreach ($ref->getParameters() as $i => $param) {
         $paramType = $param->getClass();
         if ($paramType) {
             $isRead = false;
             foreach ($this->plugins->getEntityReaders() as $reader) {
                 $result = $reader->readEntity($context, $paramType, $isRead);
                 if ($isRead) {
                     $args[$i] = $result;
                     continue 2;
                 }
             }
         }
         if (array_key_exists($param->name, $params)) {
             $value = $params[$param->name];
             $isResolved = false;
             foreach ($this->plugins->getValueConverters() as $converter) {
                 $result = $converter->resolveInput($value, $context, $isResolved, $paramType);
                 if ($isResolved) {
                     $args[$i] = $result;
                     continue 2;
                 }
             }
             if ($paramType !== NULL) {
                 $resolverNeeded[$i] = $param;
                 continue;
             }
             $args[$i] = $value;
             continue;
         }
         if ($paramType === NULL) {
             if (!$param->isOptional()) {
                 throw new \InvalidArgumentException(sprintf('Missing value of parameter "%s"', $param->name));
             }
             $args[$i] = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : NULL;
             continue;
         }
         foreach ($instances as $instance) {
             if ($paramType->isInstance($instance)) {
                 $args[$i] = $instance;
                 continue 2;
             }
         }
         $resolverNeeded[$i] = $param;
     }
     foreach ($resolverNeeded as $i => $param) {
         $paramType = $param->getClass();
         $isResolved = false;
         foreach ($this->plugins->getValueResolvers() as $resolver) {
             $result = $resolver->resolve($args, $context, $isResolved, $paramType);
             if ($isResolved) {
                 $args[$i] = $result;
                 continue 2;
             }
         }
         $args[$i] = $this->get($param);
     }
     ksort($args, SORT_NUMERIC);
     return $args;
 }
Пример #6
0
 /**
  * Get the HTTP request (shortcut to the context).
  * 
  * @var HttpRequest
  */
 public function getRequest()
 {
     return $this->context->getRequest();
 }