本文整理汇总了PHP中Symfony\Component\Serializer\Serializer::encode方法的典型用法代码示例。如果您正苦于以下问题:PHP Serializer::encode方法的具体用法?PHP Serializer::encode怎么用?PHP Serializer::encode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Serializer\Serializer
的用法示例。
在下文中一共展示了Serializer::encode方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: loginRequest
/**
* 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;
}
示例2: format
/**
* @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;
}
示例3: encode
/**
* @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);
}
示例4: login
/**
* 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.');
}
示例5: export
/**
* 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();
}
}
}
示例6: testEncode
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);
}
示例7: encode
/**
* @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);
}
示例8: serialize
/**
* 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);
}
示例9: encode
/**
* @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);
}