本文整理汇总了PHP中Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize方法的典型用法代码示例。如果您正苦于以下问题:PHP NormalizerInterface::normalize方法的具体用法?PHP NormalizerInterface::normalize怎么用?PHP NormalizerInterface::normalize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Serializer\Normalizer\NormalizerInterface
的用法示例。
在下文中一共展示了NormalizerInterface::normalize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addAttributes
/**
* {@inheritdoc}
*/
public function addAttributes(ProductTemplateInterface $template, array $attributes, $locale)
{
$options = ['entity' => 'product', 'locale' => $locale, 'disable_grouping_separator' => true];
$values = $this->buildProductValuesFromTemplateValuesData($template, $attributes, $locale);
$valuesData = $this->normalizer->normalize($values, 'json', $options);
$template->setValuesData($valuesData);
}
示例2: onKernelView
/**
* In an API context, converts any data to a JSON-LD response.
*
* @param GetResponseForControllerResultEvent $event
*
* @return JsonLdResponse|mixed
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$controllerResult = $event->getControllerResult();
if ($controllerResult instanceof Response) {
return;
}
$request = $event->getRequest();
$format = $request->attributes->get('_api_format');
if (self::FORMAT !== $format) {
return;
}
switch ($request->getMethod()) {
case Request::METHOD_POST:
$status = 201;
break;
case Request::METHOD_DELETE:
$status = 204;
break;
default:
$status = 200;
break;
}
$resourceType = $request->attributes->get('_resource_type');
$response = new JsonLdResponse($resourceType ? $this->normalizer->normalize($controllerResult, self::FORMAT, $resourceType->getNormalizationContext() + ['request_uri' => $request->getRequestUri()]) : $controllerResult, $status);
$event->setResponse($response);
}
示例3: process
/**
* {@inheritdoc}
*/
public function process($family)
{
$flatFamily = ['code' => $family->getCode()];
$familyLabels = $this->transNormalizer->normalize($family);
$flatFamily['label'] = $familyLabels['label'][$this->labelLocale];
return $flatFamily;
}
示例4: indexAction
/**
* Get the list of all locales
*
* @return JsonResponse all activated locales
*/
public function indexAction()
{
$locales = $this->localeRepository->getActivatedLocales();
$filteredLocales = $this->collectionFilter->filterCollection($locales, 'pim.internal_api.locale.view');
$normalizedLocales = $this->normalizer->normalize($filteredLocales, 'internal_api');
return new JsonResponse($normalizedLocales);
}
示例5: process
/**
* {@inheritdoc}
*/
public function process($attribute)
{
$context = ['locales' => $this->localeManager->getActiveCodes()];
$flatAttribute = ['type' => $attribute->getAttributeType(), 'code' => $attribute->getCode()] + $this->transNormalizer->normalize($attribute, null, $context);
$flatAttribute = array_merge($flatAttribute, ['group' => $attribute->getGroup() ? $attribute->getGroup()->getCode() : null, 'unique' => (int) $attribute->isUnique(), 'useable_as_grid_filter' => (int) $attribute->isUseableAsGridFilter(), 'allowed_extensions' => implode(self::ITEM_SEPARATOR, $attribute->getAllowedExtensions()), 'metric_family' => $attribute->getMetricFamily(), 'default_metric_unit' => $attribute->getDefaultMetricUnit(), 'localizable' => (int) $attribute->isLocalizable(), 'scopable' => (int) $attribute->isScopable(), 'families' => $this->getAttributeFamilyCodes($attribute)]);
return $flatAttribute;
}
示例6: onKernelException
/**
* Returns a list of violations normalized in the Hydra format.
*
* @param GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if ($exception instanceof ValidationException) {
$event->setResponse(new Response($this->normalizer->normalize($exception->getConstraintViolationList(), 'hydra-error'), Response::HTTP_BAD_REQUEST));
}
}
示例7: onKernelException
/**
* @param GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$request = $event->getRequest();
if (!$request->attributes->has('_resource_type') || self::FORMAT !== $request->attributes->get('_api_format')) {
return;
}
$exception = $event->getException();
$headers = [];
if ($exception instanceof HttpException) {
$status = $exception->getStatusCode();
$headers = $exception->getHeaders();
$data = $exception;
} elseif ($exception instanceof ValidationException) {
$status = Response::HTTP_BAD_REQUEST;
$data = $exception->getConstraintViolationList();
} elseif ($exception instanceof ExceptionInterface || $exception instanceof InvalidArgumentException) {
$status = Response::HTTP_BAD_REQUEST;
$data = $exception;
} else {
$status = Response::HTTP_INTERNAL_SERVER_ERROR;
$data = $exception;
}
$event->setResponse(new Response($this->normalizer->normalize($data, 'hydra-error'), $status, $headers));
}
示例8: indexAction
/**
* Get the attribute collection
*
* @return JsonResponse
*/
public function indexAction()
{
$attributes = $this->attributeRepository->findAll();
$filteredAttributes = $this->collectionFilter->filterCollection($attributes, 'pim.internal_api.attribute.view');
$normalizedAttributes = $this->normalizer->normalize($filteredAttributes, 'internal_api');
return new JsonResponse($normalizedAttributes);
}
示例9: normalize
/**
* {@inheritdoc}
*/
public function normalize($attribute, $format = null, array $context = [])
{
$dateMin = null === $attribute->getDateMin() ? '' : $attribute->getDateMin()->format(\DateTime::ISO8601);
$dateMax = null === $attribute->getDateMax() ? '' : $attribute->getDateMax()->format(\DateTime::ISO8601);
$normalizedAttribute = $this->normalizer->normalize($attribute, 'json', $context) + ['id' => $attribute->getId(), 'wysiwyg_enabled' => $attribute->isWysiwygEnabled(), 'empty_value' => $this->emptyValueProvider->getEmptyValue($attribute), 'field_type' => $this->fieldProvider->getField($attribute), 'is_locale_specific' => (int) $attribute->isLocaleSpecific(), 'locale_specific_codes' => $attribute->getLocaleSpecificCodes(), 'max_characters' => $attribute->getMaxCharacters(), 'validation_rule' => $attribute->getValidationRule(), 'validation_regexp' => $attribute->getValidationRegexp(), 'number_min' => $attribute->getNumberMin(), 'number_max' => $attribute->getNumberMax(), 'decimals_allowed' => $attribute->isDecimalsAllowed(), 'negative_allowed' => $attribute->isNegativeAllowed(), 'date_min' => $dateMin, 'date_max' => $dateMax, 'metric_family' => $attribute->getMetricFamily(), 'default_metric_unit' => $attribute->getDefaultMetricUnit(), 'max_file_size' => $attribute->getMaxFileSize(), 'sort_order' => $attribute->getSortOrder()];
return $normalizedAttribute;
}
示例10: normalizeCompleteness
/**
* Normalize a completeness element
*
* @param array $completeness
* @param string $format
* @param array $context
*
* @return array
*/
protected function normalizeCompleteness($completeness, $format = null, array $context = array())
{
$missing = [];
foreach ($completeness['missing'] as $attribute) {
$missing[] = $attribute->getCode();
}
return ['missing' => $missing, 'completeness' => $this->normalizer->normalize($completeness['completeness'], $format, $context)];
}
示例11: __invoke
/**
* Converts a {@see \Symfony\Component\Debug\Exception\FlattenException}
* to a {@see \Dunglas\ApiBundle\JsonLd\Response}.
*
* @param FlattenException $exception
*
* @return Response
*/
public function __invoke(FlattenException $exception)
{
$exceptionClass = $exception->getClass();
if (is_a($exceptionClass, ExceptionInterface::class, true) || is_a($exceptionClass, InvalidArgumentException::class, true)) {
$exception->setStatusCode(Response::HTTP_BAD_REQUEST);
}
return new Response($this->normalizer->normalize($exception, 'hydra-error'), $exception->getStatusCode(), $exception->getHeaders());
}
示例12: normalize
/**
* {@inheritdoc}
*/
public function normalize($price, $format = null, array $context = [])
{
$price = $this->priceNormalizer->normalize($price, $format, $context);
foreach ($price as $currency => $data) {
$price[$currency] = $this->localizer->convertDefaultToLocalized($data, $context);
}
return $price;
}
示例13: getAction
/**
* Get a single family
*
* @param int $identifier
*
* @return JsonResponse
*/
public function getAction($identifier)
{
$family = $this->familyRepository->findOneByIdentifier($identifier);
if (null === $family) {
throw new NotFoundHttpException(sprintf('Family with code "%s" not found', $identifier));
}
return new JsonResponse($this->normalizer->normalize($family, 'json'));
}
示例14: getAction
/**
* Get a single variant group
*
* @param int $identifier
*
* @return JsonResponse
*/
public function getAction($identifier)
{
$variantGroup = $this->variantGroupRepo->findOneByCode($identifier);
if (!$variantGroup) {
throw new NotFoundHttpException(sprintf('Variant group with code "%s" not found', $identifier));
}
return new JsonResponse($this->normalizer->normalize($variantGroup, 'internal_api', ['with_variant_group_values' => true]));
}
示例15: listProductsAction
/**
* Display the products of a group
*
* @param string $identifier
*
* @return JsonResponse
*
* @AclAncestor("pim_enrich_product_index")
*/
public function listProductsAction($identifier)
{
$group = $this->groupRepository->findOneBy(['code' => $identifier]);
if (!$group) {
throw new NotFoundHttpException(sprintf('Group with code "%s" not found', $identifier));
}
return new JsonResponse($this->normalizer->normalize(['products' => $this->productRepository->getProductsByGroup($group, self::MAX_PRODUCTS), 'productCount' => $this->productRepository->getProductCountByGroup($group)], 'internal_api'));
}