本文整理汇总了PHP中Symfony\Component\Serializer\Serializer::normalize方法的典型用法代码示例。如果您正苦于以下问题:PHP Serializer::normalize方法的具体用法?PHP Serializer::normalize怎么用?PHP Serializer::normalize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Serializer\Serializer
的用法示例。
在下文中一共展示了Serializer::normalize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testCustomNormalizerCanNormalizeCollectionsAndScalar
public function testCustomNormalizerCanNormalizeCollectionsAndScalar()
{
$serializer = new Serializer(array(new TestNormalizer()), array());
$this->assertNull($serializer->normalize(array('a', 'b')));
$this->assertNull($serializer->normalize(new \ArrayObject(array('c', 'd'))));
$this->assertNull($serializer->normalize(array()));
$this->assertNull($serializer->normalize('test'));
}
示例2: itShouldCollect
/**
* @test
*/
public function itShouldCollect()
{
$this->serializer->normalize(Argument::any())->shouldBeCalled()->willReturn([]);
$this->logger->pushHandler(Argument::any())->shouldBeCalled()->willReturn(true);
$this->logger->info(Argument::type('string'), Argument::type('array'))->shouldBeCalled();
$this->collector->collect($this->requestObject, ['logFile' => 'test.log']);
}
示例3: save
public function save(Article $article)
{
$metadata = $this->entityManager->getClassMetadata(get_class($article));
$type = "app." . $metadata->getTableName();
$this->fluentLogger->post($type, array_filter($this->serializer->normalize($article), function ($idx) {
return $idx != 'id';
}, ARRAY_FILTER_USE_KEY));
}
示例4: normalize
/**
* {@inheritdoc}
*/
public function normalize($elements, $format = null, array $context = [])
{
$normalizedElements = [];
foreach ($elements as $key => $element) {
$normalizedElements[$key] = $this->serializer->normalize($element, $format, $context);
}
return $normalizedElements;
}
示例5: diff
/**
* Compares given value object with value object data structure
*
* Returns set of value object attribute names. E.g.:
*
* [
* // Items that are missing in value object
* 'add' => ['field1', 'field2', ...]
* // Items that shuld not be present in value object
* 'remove' => ['field3', 'field4', ...]
* ]
*
* @param object $object
* @param array $comparedData
* @param string $propertyIdentifier
* @return array
*/
public function diff($object, $comparedData, $propertyIdentifier)
{
$objectData = $this->serilaizer->normalize($object);
$comparingColumn = $this->getAttributesOfValueObjects($comparedData, $propertyIdentifier);
$toAdd = array_diff($comparingColumn, array_column($objectData, $propertyIdentifier));
$toRemove = array_diff(array_column($objectData, $propertyIdentifier), $comparingColumn);
// @todo: add items to be updated
return ['add' => $toAdd, 'remove' => $toRemove];
}
示例6: process
/**
* {@inheritdoc}
*/
public function process($product)
{
$data['media'] = [];
$mediaValues = $this->getMediaProductValues($product);
foreach ($mediaValues as $mediaValue) {
$data['media'][] = $this->serializer->normalize($mediaValue->getMedia(), 'flat', ['field_name' => 'media', 'prepare_copy' => true, 'value' => $mediaValue]);
}
$data['product'] = $this->serializer->normalize($product, 'flat', $this->getNormalizerContext());
return $data;
}
示例7: getFormExportCode
/**
* @param $fillpdf_form
* @return string
*/
public function getFormExportCode(FillPdfFormInterface $fillpdf_form) {
$fields = $this->entityHelper->getFormFields($fillpdf_form);
$form_config = [
'form' => $this->serializer->normalize($fillpdf_form),
'fields' => $this->serializer->normalize($fields),
];
$code = $this->serializer->serialize($form_config, 'json');
return $code;
}
示例8: __invoke
/**
* Retrieves a collection of resources.
*
* @param Request $request
*
* @throws RuntimeException|RootNodeNotFoundException
*
* @return array|\Dunglas\ApiBundle\Model\PaginatorInterface|\Traversable
*/
public function __invoke(Request $request)
{
$user = $this->userService->getUser();
$preferences = $this->userPreferenceService->getPreferences($user);
list($resourceType) = $this->extractAttributes($request);
/*
* @var ResourceInterface $resourceType
*/
$serializedData = $this->serializer->normalize($preferences, 'json', $resourceType->getNormalizationContext());
return new JsonResponse($serializedData);
}
示例9: process
/**
* {@inheritdoc}
*/
public function process($product)
{
$contextChannel = $this->channelManager->getChannelByCode($this->channel);
$this->productBuilder->addMissingProductValues($product, [$contextChannel], $contextChannel->getLocales()->toArray());
$data['media'] = [];
$mediaValues = $this->getMediaProductValues($product);
foreach ($mediaValues as $mediaValue) {
$data['media'][] = $this->serializer->normalize($mediaValue->getMedia(), 'flat', ['field_name' => 'media', 'prepare_copy' => true, 'value' => $mediaValue]);
}
$data['product'] = $this->serializer->normalize($product, 'flat', $this->getNormalizerContext());
return $data;
}
示例10: process
/**
* {@inheritdoc}
*/
public function process($product)
{
$data['media'] = [];
$productMedias = $this->getProductMedias($product);
if (count($productMedias) > 0) {
try {
$data['media'] = $this->serializer->normalize($productMedias, 'flat', ['field_name' => 'media', 'prepare_copy' => true]);
} catch (FileNotFoundException $e) {
throw new InvalidItemException($e->getMessage(), ['item' => $product->getIdentifier()->getData(), 'uploadDirectory' => $this->uploadDirectory]);
}
}
$data['product'] = $this->serializer->normalize($product, 'flat', $this->getNormalizerContext());
return $data;
}
示例11: __invoke
/**
* Retrieves a collection of resources.
*
* @param Request $request
*
* @throws \Exception If the format is invalid
* @throws RuntimeException|RootNodeNotFoundException
*
* @return JsonResponse
*/
public function __invoke(Request $request)
{
$data = json_decode($request->getContent());
if (property_exists($data, 'preferenceKey') && property_exists($data, 'preferenceValue')) {
$preference = $this->systemPreferenceService->setSystemPreference($data->preferenceKey, $data->preferenceValue);
} else {
throw new \Exception('Invalid format');
}
/**
* @var ResourceInterface $resourceType
*/
list($resourceType) = $this->extractAttributes($request);
$serializedData = $this->serializer->normalize($preference, 'json', $resourceType->getNormalizationContext());
return new JsonResponse($serializedData);
}
示例12: syncAction
/**
* @return JsonResponse
*
* @Route("/sync")
*/
public function syncAction()
{
$youtube = new Youtube(['key' => $this->getParameter('youtube_api_key')]);
$videos = $youtube->getPlaylistItemsByPlaylistId($this->getParameter('youtube_playlist_id'));
$em = $this->getDoctrine()->getManager();
foreach ($videos as $video) {
$name = $video->snippet->title;
$youtubeId = $video->snippet->resourceId->videoId;
$description = $video->snippet->description;
if (property_exists($video->snippet->thumbnails, 'maxres')) {
$thumbnail = $video->snippet->thumbnails->maxres->url;
} elseif (property_exists($video->snippet->thumbnails, 'high')) {
$thumbnail = $video->snippet->thumbnails->high->url;
} elseif (property_exists($video->snippet->thumbnails, 'medium')) {
$thumbnail = $video->snippet->thumbnails->medium->url;
} else {
$thumbnail = $video->snippet->thumbnails->default->url;
}
$video = $em->getRepository('TGVideoBundle:Video')->findOneByYoutubeId($youtubeId);
if ($video === null) {
$video = new Video();
$video->setYoutubeId($youtubeId);
$em->persist($video);
}
$video->setName($name);
$video->setDescription($description);
$video->setThumbnail($thumbnail);
$video->setUrl('https://www.youtube.com/embed/' . $youtubeId);
}
$em->flush();
$videos = $this->getDoctrine()->getRepository('TGVideoBundle:Video')->findAll([], ['createdAt' => 'DESC']);
$serializer = new Serializer([$this->get('tg_video.normalizer')]);
return new JsonResponse(['videos' => $serializer->normalize($videos)]);
}
示例13: testNormalizeWithSupportOnData
public function testNormalizeWithSupportOnData()
{
$normalizer1 = $this->getMock('Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface');
$normalizer1->method('supportsNormalization')->willReturnCallback(function ($data, $format) {
return isset($data->test);
});
$normalizer1->method('normalize')->willReturn('test1');
$normalizer2 = $this->getMock('Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface');
$normalizer2->method('supportsNormalization')->willReturn(true);
$normalizer2->method('normalize')->willReturn('test2');
$serializer = new Serializer(array($normalizer1, $normalizer2));
$data = new \stdClass();
$data->test = true;
$this->assertEquals('test1', $serializer->normalize($data));
$this->assertEquals('test2', $serializer->normalize(new \stdClass()));
}
示例14: __invoke
/**
* Retrieves a collection of resources.
*
* @param Request $request
*
* @return array|\Dunglas\ApiBundle\Model\PaginatorInterface|\Traversable
* @throws \Exception If the format is invalid
*
* @throws RuntimeException|RootNodeNotFoundException
*/
public function __invoke(Request $request)
{
$user = $this->userService->getUser();
$data = json_decode($request->getContent());
if (property_exists($data, "preferenceKey") && property_exists($data, "preferenceValue")) {
$preference = $this->userPreferenceService->setPreference($user, $data->preferenceKey, $data->preferenceValue);
} else {
throw new \Exception("Invalid format");
}
list($resourceType) = $this->extractAttributes($request);
/**
* @var ResourceInterface $resourceType
*/
$serializedData = $this->serializer->normalize($preference, 'json', $resourceType->getNormalizationContext());
return new JsonResponse($serializedData);
}
示例15: ajaxGetAction
/**
* @Route("/player/ajax/get", name="team_player_ajax_get")
*/
public function ajaxGetAction(Request $request)
{
if ($request->isXmlHttpRequest()) {
try {
$user = $this->getUser();
$em = $this->getDoctrine()->getManager();
$player = $em->getRepository('TeamBundle:Player')->findOneBy(array('id' => $request->get('id')));
if ($user->getTeam() == $player->getTeam()) {
$normalizer = new ObjectNormalizer();
$normalizer->setCircularReferenceHandler(function ($object) {
return $object->getId();
});
$serializer = new Serializer(array($normalizer));
$player = $serializer->normalize($player);
$response = new Response(json_encode(array('status' => 'ok', 'player' => $player)));
} else {
$response = new Response(json_encode(array('status' => 'ko', 'message' => 'Vous n\'avez pas la permission d\'éditer ce joueur', 'debug' => 'Utilisateur connecté != manager de l\'équipe du joueur')));
}
} catch (\Exception $e) {
$response = new Response(json_encode(array('status' => 'ko', 'message' => 'Une erreur inconnue s\'est produite', 'debug' => $e->getMessage())));
}
$response->headers->set('Content-Type', 'application/json');
return $response;
}
$response = new Response(json_encode(array('status' => 'ko', 'message' => 'Accès non autorisé', 'debug' => 'Bad request')));
$response->headers->set('Content-Type', 'application/json');
return $response;
}