encode() final public method

final public encode ( $data, $format )
Ejemplo n.º 1
0
 /**
  * Executes a login HTTP request.
  *
  * @param string $name
  *   The username.
  * @param string $pass
  *   The user password.
  * @param string $format
  *   The format to use to make the request.
  *
  * @return \Psr\Http\Message\ResponseInterface The HTTP response.
  *   The HTTP response.
  */
 protected function loginRequest($name, $pass, $format = 'json')
 {
     $user_login_url = Url::fromRoute('user.login.http')->setRouteParameter('_format', $format)->setAbsolute();
     $request_body = [];
     if (isset($name)) {
         $request_body['name'] = $name;
     }
     if (isset($pass)) {
         $request_body['pass'] = $pass;
     }
     $result = \Drupal::httpClient()->post($user_login_url->toString(), ['body' => $this->serializer->encode($request_body, $format), 'headers' => ['Accept' => "application/{$format}"], 'http_errors' => FALSE, 'cookies' => $this->cookies]);
     return $result;
 }
Ejemplo n.º 2
0
 /**
  * @param $request
  * @param array $data
  * @param int $status
  * @param null $message
  * @return Response
  */
 public function format($request, $data = array(), $status = 200, $message = null)
 {
     $data = ['http' => ['status' => $status, 'message' => $message ? $message : Response::$statusTexts[$status]], 'response' => $data];
     $format = 'json';
     if ($request->query->has('format')) {
         $format = $request->query->get('format');
     } else {
         if ($request->headers->has('format')) {
             $format = $request->headers->get('format');
         }
     }
     if (!in_array($format, ['xml', 'json'])) {
         $format = 'json';
     }
     $response = new Response($this->serializer->encode($data, $format), $status);
     $response->headers->set('Content-Type', 'text/' . $format);
     return $response;
 }
Ejemplo n.º 3
0
 /**
  * @param object $object
  * @param string $format
  * @param array  $context
  *
  * @return array
  */
 public function encode($object, $format, array $context = [])
 {
     if ($object instanceof Request or $object instanceof Request) {
         $context['xml_root_node_name'] = 'methodCall';
     } elseif ($object instanceof Response) {
         $context['xml_root_node_name'] = 'methodResponse';
     }
     $array = $this->serializer->normalize($object, $format);
     $array = $this->encodeValue($array);
     return $this->serializer->encode($array, $context['encoding'], $context);
 }
 /**
  * Logs in a user.
  *
  * @param \Symfony\Component\HttpFoundation\Request $request
  *   The request.
  *
  * @return \Symfony\Component\HttpFoundation\Response
  *   A response which contains the ID and CSRF token.
  */
 public function login(Request $request)
 {
     $format = $this->getRequestFormat($request);
     $content = $request->getContent();
     $credentials = $this->serializer->decode($content, $format);
     if (!isset($credentials['name']) && !isset($credentials['pass'])) {
         throw new BadRequestHttpException('Missing credentials.');
     }
     if (!isset($credentials['name'])) {
         throw new BadRequestHttpException('Missing credentials.name.');
     }
     if (!isset($credentials['pass'])) {
         throw new BadRequestHttpException('Missing credentials.pass.');
     }
     $this->floodControl($request, $credentials['name']);
     if ($this->userIsBlocked($credentials['name'])) {
         throw new BadRequestHttpException('The user has not been activated or is blocked.');
     }
     if ($uid = $this->userAuth->authenticate($credentials['name'], $credentials['pass'])) {
         $this->flood->clear('user.http_login', $this->getLoginFloodIdentifier($request, $credentials['name']));
         /** @var \Drupal\user\UserInterface $user */
         $user = $this->userStorage->load($uid);
         $this->userLoginFinalize($user);
         // Send basic metadata about the logged in user.
         $response_data = [];
         if ($user->get('uid')->access('view', $user)) {
             $response_data['current_user']['uid'] = $user->id();
         }
         if ($user->get('roles')->access('view', $user)) {
             $response_data['current_user']['roles'] = $user->getRoles();
         }
         if ($user->get('name')->access('view', $user)) {
             $response_data['current_user']['name'] = $user->getAccountName();
         }
         $response_data['csrf_token'] = $this->csrfToken->get('rest');
         $logout_route = $this->routeProvider->getRouteByName('user.logout.http');
         // Trim '/' off path to match \Drupal\Core\Access\CsrfAccessCheck.
         $logout_path = ltrim($logout_route->getPath(), '/');
         $response_data['logout_token'] = $this->csrfToken->get($logout_path);
         $encoded_response_data = $this->serializer->encode($response_data, $format);
         return new Response($encoded_response_data);
     }
     $flood_config = $this->config('user.flood');
     if ($identifier = $this->getLoginFloodIdentifier($request, $credentials['name'])) {
         $this->flood->register('user.http_login', $flood_config->get('user_window'), $identifier);
     }
     // Always register an IP-based failed login event.
     $this->flood->register('user.failed_login_ip', $flood_config->get('ip_window'));
     throw new BadRequestHttpException('Sorry, unrecognized username or password.');
 }
 /**
  * Outputs serialized entities
  *
  * @param Request $request
  */
 protected function export(Request $request)
 {
     $iterator = $this->queryGenerator->createQueryBuilder($request, $this->configuration->getName())->getQuery()->iterate();
     $headersSent = false;
     $manager = $this->doctrine->getManagerForClass($this->configuration->getEntityClass());
     foreach ($iterator as $index => $item) {
         if (!count($item[0])) {
             continue;
         }
         $norm = $this->serializer->normalize($item[0], $this->options['serializer_format'], $this->options['serializer_context']);
         if (!$headersSent) {
             echo $this->serializer->encode(array_keys($norm), $this->options['serializer_format'], $this->options['serializer_context']);
             $headersSent = true;
         }
         echo $this->serializer->encode($norm, $this->options['serializer_format'], $this->options['serializer_context']);
         flush();
         if (0 === ($index + 100) % $this->options['batch_size']) {
             $manager->clear();
         }
     }
 }
Ejemplo n.º 6
0
 public function testEncode()
 {
     $serializer = new Serializer(array(), array('json' => new JsonEncoder()));
     $data = array('foo', array(5, 3));
     $result = $serializer->encode($data, 'json');
     $this->assertEquals(json_encode($data), $result);
 }
Ejemplo n.º 7
0
 /**
  * @param object $object
  * @param string $format
  * @param array  $context
  *
  * @return array
  */
 public function encode($object, $format, array $context = [])
 {
     $context['xml_root_node_name'] = 'soap:Envelope';
     $array = $this->serializer->normalize($object, $format);
     return $this->serializer->encode($array, $context['encoding'], $context);
 }
Ejemplo n.º 8
0
 /**
  * PHP to JSON Serialization.
  *
  * This method, when supplied with an array, will do its best
  * to convert the array into friendly JSON format.
  *
  * @param string|array|object $phpFormat  The PHP formatted data input (string|array|object) to serialize.
  *
  * @return string The JSON string representing the original PHP string, array (or object).
  *
  * @api
  */
 public function serialize($phpFormat) : string
 {
     return $this->serializer->encode($phpFormat, static::FORMAT);
 }
Ejemplo n.º 9
0
 /**
  * @param object $object
  * @param string $format
  * @param array  $context
  *
  * @return array
  */
 public function encode($object, $format = null, array $context = array())
 {
     $array = $this->serializer->normalize($object, $format);
     return $this->serializer->encode($array, $context['encoding'], $context);
 }