本文整理汇总了PHP中JMS\Serializer\Serializer::serialize方法的典型用法代码示例。如果您正苦于以下问题:PHP Serializer::serialize方法的具体用法?PHP Serializer::serialize怎么用?PHP Serializer::serialize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JMS\Serializer\Serializer
的用法示例。
在下文中一共展示了Serializer::serialize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testSerializeJson
public function testSerializeJson()
{
$uuid = Uuid::fromString('34ca79b8-6181-4b93-903a-ac658e0c5c35');
$object = new ObjectWithUuid($uuid);
$json = $this->serializer->serialize($object, 'json');
$this->assertEquals('{"uuid":"34ca79b8-6181-4b93-903a-ac658e0c5c35"}', $json);
}
示例2: xmlSerialization
/**
* @test serialized xml has type attribute
*/
public function xmlSerialization()
{
$container = new EventContainer(new SerializableEventStub('event.name', 'some data'));
$this->namingStrategy->expects($this->once())->method('classToType')->with(get_class($container->getEvent()))->will($this->returnValue('stub'));
$serialized = $this->serializer->serialize($container, 'xml');
$this->assertEquals(file_get_contents(__DIR__ . '/Fixtures/container.xml'), $serialized);
}
示例3: put
public function put($resource, $body, $type, $options = [])
{
$options['body'] = $this->serializer->serialize($body, 'json');
$options['headers'] = ['Content-Type' => 'application/json'];
$content = $this->client->put($resource, $options)->getBody()->getContents();
return $this->deserialize($content, $type);
}
示例4: serializeResponse
/**
* @param GetResponseForControllerResultEvent $event
*/
public function serializeResponse(GetResponseForControllerResultEvent $event)
{
if ($this->doSerialize) {
$data = $event->getControllerResult();
$apiResponse = new ApiResponse(200, $data);
$data = array_merge($apiResponse->toArray(), $this->data->all());
$data = array_filter($data);
if (!isset($data['data'])) {
$data['data'] = [];
}
$context = new SerializationContext();
$context->setSerializeNull(true);
if (method_exists($context, 'enableMaxDepthChecks')) {
$context->enableMaxDepthChecks();
}
if ($action = $this->getAction($event)) {
$context->setGroups($action->getSerializationGroups());
}
if ($fields = $event->getRequest()->query->get('fields')) {
$context->addExclusionStrategy(new FieldsListExclusionStrategy($fields));
}
$json = $this->serializer->serialize($data, 'json', $context);
$response = new Response($json, 200, ['Content-Type' => 'application/json']);
$event->setResponse($response);
$event->stopPropagation();
}
}
示例5: render
/**
* Render the view into a string and return for output
*
* @param mixed $input
* @return string
* @throws \Exception
*/
public function render($input = null)
{
$context = new SerializationContext();
$context->setSerializeNull(true);
$context->enableMaxDepthChecks();
FrontController::getInstance()->getResponse()->headers->set('Content-Type', 'application/json');
return $this->serializer->serialize($input, $this->format, $context);
}
示例6: dump
/**
*
* @param \CSanquer\FakeryGenerator\Model\Config $config
* @param string $dir
* @param string $format
* @return string filename
*/
public function dump(Config $config, $dir, $format = 'json')
{
$format = in_array($format, ['json', 'xml']) ? $format : 'json';
$serialized = $this->serializer->serialize($config, $format);
$filename = $dir . '/' . $config->getClassName(true) . '_fakery_generator_config_' . date('Y-m-d_H-i-s') . '.' . $format;
file_put_contents($filename, $serialized);
return $filename;
}
示例7: json
public function json($data, $groups = null)
{
if ($groups) {
$this->context->setGroups($groups);
}
$serializedData = $this->serializer->serialize($data, 'json', $this->context);
return $serializedData;
}
示例8: transform
/**
* {@inheritdoc}
*/
public function transform($data)
{
try {
return $this->serializer->serialize($data, $this->format);
} catch (JMSException $e) {
throw new StorageException('The JMS serializer failed serializing the data: ' . $e->getMessage(), 0, $e);
}
}
示例9: serialize
/**
* @param mixed $content
*
* @return string
*/
protected function serialize($content)
{
if (!empty($content)) {
if (is_object($content)) {
$content = $this->serializer->serialize($content, 'json');
}
}
return $content;
}
示例10: append
public function append(EventStream $events)
{
foreach ($events as $event) {
$data = $this->serializer->serialize($event, 'json');
$event = $this->serializer->serialize(['type' => get_class($event), 'created_on' => (new DateTimeImmutable('now', new DateTimeZone('UTC')))->getTimestamp(), 'data' => $data], 'json');
$this->predis->rpush('events:' . $events->aggregateId(), $event);
$this->predis->rpush('published_events', $event);
}
}
示例11: getResponse
/**
* @inheritDoc
*/
public function getResponse($responseType, $view, $templateName, $params, $status = 200, $headers = [], $groups = ['Default'])
{
if ($responseType === 'html') {
$response = $this->getHtml($view, $templateName, $params);
} else {
$response = $this->serializer->serialize($params, $responseType, SerializationContext::create()->setGroups($groups));
}
return new Response($response, $status, $headers);
}
示例12: serialize
/**
* @param array|object $data
* @param string $dtoClassName
* @param string $outputFormat
* @return array
*/
public function serialize($data, $dtoClassName, $outputFormat = 'json')
{
// TODO: check if $data is object or array
$itemArray = [];
foreach ($data as $item) {
$dto = $this->shifter->toDto($item, new $dtoClassName());
$serializedData = $this->serializer->serialize($dto, $outputFormat);
$itemArray[] = (array) json_decode($serializedData);
}
return $itemArray;
}
示例13: store
/**
* @param object $entidad
* @param string $contenido
*/
public function store($entidad, $contenido)
{
if (!method_exists($entidad, 'getDetalles')) {
throw new HttpException(400, "No el objeto no tiene detalles serializables");
}
$observacion = new Observacion();
$observacion->setContenido($contenido);
$observacion->setRelated($entidad->getId());
$observacion->setLastState($this->serializer->serialize($entidad->getDetalles(), 'json'));
$this->manager->persist($observacion);
}
示例14: put
public function put($resource, $body, $type, $options = [])
{
$options['future'] = true;
$options['body'] = $this->serializer->serialize($body, 'json');
$options['headers'] = ['Content-Type' => 'application/json'];
return $this->client->put($resource, $options)->then(function (Response $reponse) {
return $reponse->getBody()->getContents();
})->then(function ($content) use($type) {
return $this->deserialize($content, $type);
});
}
示例15: onKernelView
/**
* @param GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$result = $event->getControllerResult();
if ($result instanceof Response) {
return $result;
}
$serialized = $this->serializer->serialize($result, 'json');
$response = new Response();
$response->setContent($serialized);
$response->headers->add(array('Content-type' => 'application/json'));
$event->setResponse($response);
}