/** * Convert a localized attribute * * @param LocalizerInterface $localizer * @param array $item * @param array $options * @param string $code * * @throws \LogicException * * @return array */ protected function convertAttribute(LocalizerInterface $localizer, array $item, array $options, $code) { if ($localizer->isValid($item['data'], $options, $code)) { $item['data'] = $localizer->convertLocalizedToDefault($item['data'], $options); return $item; } }
/** * Check if number provided respects the pattern of the localizer. * If false, throw an exception. * It true, delocalize localized number to the default format * * @param string $number * * @throws TransformationFailedException * * @return string */ public function reverseTransform($number) { $violations = $this->localizer->validate($number, 'code', $this->options); if (null === $violations || 0 === $violations->count()) { return $this->localizer->delocalize($number, $this->options); } throw new TransformationFailedException($violations->get(0)->getMessage()); }
/** * {@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; }
/** * {@inheritdoc} */ public function denormalize($data, $class, $format = null, array $context = []) { $data = $this->valuesDenormalizer->denormalize($data, $class, $format, $context); if (null !== $data) { $data = $this->localizer->localize($data, $context); } return $data; }
/** * {@inheritdoc} */ public function denormalize($data, $class, $format = null, array $context = []) { $metric = $this->metricDenormalizer->denormalize($data, $class, $format, $context); if (null !== $metric) { $metric->setData($this->localizer->localize($metric->getData(), $context)); } return $metric; }
/** * {@inheritdoc} */ public function normalize($metric, $format = null, array $context = []) { $metric = $this->metricNormalizer->normalize($metric, $format, $context); if (!isset($context['field_name']) || !isset($metric[$context['field_name']])) { return $metric; } $metric[$context['field_name']] = $this->localizer->convertDefaultToLocalized($metric[$context['field_name']], $context); return $metric; }
/** * Convert a localized attribute * * @param LocalizerInterface $localizer * @param array $item * @param array $options * @param string $path * * @return array */ protected function convertLocalizedToDefaultValue(LocalizerInterface $localizer, array $item, array $options, $path) { $violations = $localizer->validate($item['data'], $path, $options); if (null !== $violations) { $this->violations->addAll($violations); } $item['data'] = $localizer->delocalize($item['data'], $options); return $item; }
/** * {@inheritdoc} */ public function normalize($entity, $format = null, array $context = []) { $result = $this->valuesNormalizer->normalize($entity, $format, $context); if (AttributeTypes::NUMBER === $entity->getAttribute()->getAttributeType()) { foreach ($result as $field => $data) { $result[$field] = $this->localizer->convertDefaultToLocalized($data, $context); } } return $result; }
/** * {@inheritdoc} */ protected function convertValue($value) { $result = $this->getBackendData($value); $data = isset($result['data']) ? $result['data'] : null; $unit = $result['unit']; if ($data && $unit) { $formattedData = $this->localizer->localize($data, ['locale' => $this->translator->getLocale()]); return $this->getTemplate()->render(['data' => $formattedData, 'unit' => $unit]); } }
/** * {@inheritdoc} */ public function normalize($price, $format = null, array $context = []) { $price = $this->priceNormalizer->normalize($price, $format, $context); foreach ($price as $currency => $data) { $formattedPrice = [['currency' => $currency, 'data' => $data]]; $localizedPrice = $this->localizer->convertDefaultToLocalized($formattedPrice, $context); $price[$currency] = $localizedPrice[0]['data']; } return $price; }
/** * {@inheritdoc} */ public function denormalize($data, $class, $format = null, array $context = []) { $prices = $this->priceDenormalizer->denormalize($data, $class, $format, $context); if (null !== $prices) { foreach ($prices as $price) { $price->setData($this->localizer->localize($price->getData(), $context)); } } return $prices; }
/** * {@inheritdoc} */ public function normalize($metric, $format = null, array $context = []) { $metric = $this->metricNormalizer->normalize($metric, $format, $context); if (!isset($context['field_name']) || !isset($metric[$context['field_name']])) { return $metric; } $formattedMetric = ['data' => $metric[$context['field_name']]]; $localizedMetric = $this->localizer->localize($formattedMetric, $context); $metric[$context['field_name']] = $localizedMetric['data']; return $metric; }
/** * {@inheritdoc} */ protected function convertValue($value) { $data = $this->getBackendData($value); $prices = []; foreach ($data as $price) { if (isset($price['data']) && $price['data'] !== null) { $formattedPrice = $this->localizer->convertDefaultToLocalizedFromLocale($price['data'], $this->translator->getLocale()); $prices[] = sprintf('%s %s', $formattedPrice, Intl::getCurrencyBundle()->getCurrencySymbol($price['currency'])); } } return implode(', ', $prices); }
/** * {@inheritdoc} */ protected function convertValue($value) { $result = $this->getBackendData($value); return $this->localizer->convertDefaultToLocalizedFromLocale($result, $this->translator->getLocale()); }
function it_returns_null_if_there_is_no_localizer(LocalizerInterface $localizer) { $localizer->supports('pim_catalog_number')->willReturn(false); $this->addLocalizer($localizer); $this->getLocalizer('pim_catalog_number')->shouldReturn(null); }
/** * {@inheritdoc} */ public function normalize($price, $format = null, array $context = []) { $price = $this->priceNormalizer->normalize($price, $format, $context); $price['data'] = $this->localizer->localize($price['data'], $context); return $price; }
/** * {@inheritdoc} */ protected function convertValue($value) { $result = $this->getBackendData($value); return $this->localizer->localize($result, ['locale' => $this->translator->getLocale()]); }
/** * {@inheritdoc} */ public function normalize($metric, $format = null, array $context = []) { $metric = $this->metricNormalizer->normalize($metric, $format, $context); $metric['data'] = $this->localizer->localize($metric['data'], $context); return $metric; }