本文整理汇总了PHP中Symfony\Component\Serializer\SerializerInterface::normalize方法的典型用法代码示例。如果您正苦于以下问题:PHP SerializerInterface::normalize方法的具体用法?PHP SerializerInterface::normalize怎么用?PHP SerializerInterface::normalize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Serializer\SerializerInterface
的用法示例。
在下文中一共展示了SerializerInterface::normalize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: normalize
/**
* {@inheritdoc}
*/
public function normalize($entity, $format = null, array $context = [])
{
$data = $entity->getData();
$fieldName = $this->getFieldValue($entity);
$result = null;
if (is_array($data)) {
$data = new ArrayCollection($data);
}
if (is_null($data)) {
$result = [$fieldName => ''];
} elseif (is_int($data)) {
$result = [$fieldName => (string) $data];
} elseif (is_float($data)) {
$result = [$fieldName => sprintf(sprintf('%%.%sF', $this->precision), $data)];
} elseif (is_string($data)) {
$result = [$fieldName => $data];
} elseif (is_bool($data)) {
$result = [$fieldName => (string) (int) $data];
} elseif (is_object($data)) {
$context['field_name'] = $fieldName;
$result = $this->serializer->normalize($data, $format, $context);
}
if (null === $result) {
throw new \RuntimeException(sprintf('Cannot normalize product value "%s" which data is a(n) "%s"', $fieldName, is_object($data) ? get_class($data) : gettype($data)));
}
return $result;
}
示例2: collect
/**
* {@inheritdoc}
*/
public function collect(RequestObject $requestObject, array $parameters = [])
{
$parameters = $this->resolveCollectorParameters($parameters);
$logFile = sprintf('%s/%s.%s', $this->logFolder, $this->kernelEnvironment, $parameters['logFile']);
$this->logger->pushHandler(new StreamHandler($logFile));
$this->logger->info('request_collector.collect', $this->serializer->normalize($requestObject));
}
示例3: normalizeDecimal
/**
* Normalize a decimal attribute value
*
* @param mixed $data
* @param string $format
* @param array $context
*
* @return mixed|null
*/
protected function normalizeDecimal($data, $format, $context)
{
if (false === is_numeric($data)) {
return $this->serializer->normalize($data, $format, $context);
}
return floatval($data);
}
示例4: normalize
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
$normalizedItems = [];
foreach ($object as $item) {
$normalizedItems[$item->getLocale()] = $this->serializer->normalize($item, $format, $context);
}
return $normalizedItems;
}
示例5: normalize
/**
* {@inheritdoc}
*/
public function normalize($entity, $format = null, array $context = [])
{
$data = $entity->getData();
$fieldName = $this->getFieldName($entity);
if ($this->filterLocaleSpecific($entity)) {
return [];
}
$result = null;
if (is_array($data)) {
$data = new ArrayCollection($data);
}
$type = $entity->getAttribute()->getAttributeType();
$backendType = $entity->getAttribute()->getBackendType();
if (AttributeTypes::BOOLEAN === $type) {
$result = [$fieldName => (string) (int) $data];
} elseif (is_null($data)) {
$result = [$fieldName => ''];
if ('metric' === $backendType) {
$result[$fieldName . '-unit'] = '';
}
} elseif (is_int($data)) {
$result = [$fieldName => (string) $data];
} elseif (is_float($data) || 'decimal' === $entity->getAttribute()->getBackendType()) {
$pattern = $entity->getAttribute()->isDecimalsAllowed() ? sprintf('%%.%sF', $this->precision) : '%d';
$result = [$fieldName => sprintf($pattern, $data)];
} elseif (is_string($data)) {
$result = [$fieldName => $data];
} elseif (is_object($data)) {
// TODO: Find a way to have proper currency-suffixed keys for normalized price data
// even when an empty collection is passed
if ('prices' === $backendType && $data instanceof Collection && $data->isEmpty()) {
$result = [];
} elseif ('options' === $backendType && $data instanceof Collection && $data->isEmpty() === false) {
$data = $this->sortOptions($data);
$context['field_name'] = $fieldName;
$result = $this->serializer->normalize($data, $format, $context);
} else {
$context['field_name'] = $fieldName;
if ('metric' === $backendType) {
$context['decimals_allowed'] = $entity->getAttribute()->isDecimalsAllowed();
} elseif ('media' === $backendType) {
$context['value'] = $entity;
}
$result = $this->serializer->normalize($data, $format, $context);
}
}
if (null === $result) {
throw new \RuntimeException(sprintf('Cannot normalize product value "%s" which data is a(n) "%s"', $fieldName, is_object($data) ? get_class($data) : gettype($data)));
}
$localizer = $this->localizerRegistry->getLocalizer($type);
if (null !== $localizer) {
foreach ($result as $field => $data) {
$result[$field] = $localizer->localize($data, $context);
}
}
return $result;
}
示例6: normalize
/**
* Returned normalized data
*
* @param Collection $object object to normalize
* @param mixed $format
* @param array $context
* @return array
*/
public function normalize($object, $format = null, array $context = array())
{
$result = array();
foreach ($object as $item) {
$serializedItem = $this->serializer->normalize($item, $format, $context);
$result[] = $serializedItem;
}
return $result;
}
示例7: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->normalizer = new EntityReferenceFieldItemNormalizer();
$this->serializer = $this->prophesize(Serializer::class);
// Set up the serializer to return an entity property.
$this->serializer->normalize(Argument::cetera())->willReturn(['value' => 'test']);
$this->normalizer->setSerializer($this->serializer->reveal());
$this->fieldItem = $this->prophesize(EntityReferenceItem::class);
$this->fieldItem->getIterator()->willReturn(new \ArrayIterator(['target_id' => []]));
}
示例8: normalize
public function normalize(Kwf_Model_Row_Interface $row, $column, array $settings, $format = null, array $context = array())
{
if (!isset($settings['rule'])) {
throw new \Exception("rule setting is required");
}
$rows = $row->getChildRows($settings['rule']);
if (isset($settings['groups'])) {
$context['groups'] = $settings['groups'];
}
return $this->serializer->normalize($rows, $format, $context);
}
示例9: normalize
/**
* @param AbstractTypedAddress $object
* @param mixed $format
* @param array $context
* @return array
*/
public function normalize($object, $format = null, array $context = array())
{
$result = $this->addressNormalizer->normalize($object, $format, $context);
$types = $object->getTypes();
if (!$types->isEmpty()) {
$result['types'] = $this->serializer->normalize($object->getTypes(), $format, $context);
} else {
$result['types'] = array();
}
return $result;
}
示例10: normalize
/**
* {@inheritdoc}
*/
public function normalize($data, $format = null, array $context = [])
{
$result = [];
foreach ($data as $value) {
if ($value instanceof ProductValueInterface) {
$result[$value->getAttribute()->getCode()][] = $this->serializer->normalize($value, 'json', $context);
} else {
$result[] = $this->serializer->normalize($value, 'json', $context);
}
}
return $result;
}
示例11: normalize
/**
* {@inheritdoc}
*/
public function normalize($entity, $format = null, array $context = [])
{
if ($entity->getData() instanceof Collection) {
$value = [];
foreach ($entity->getData() as $item) {
$value[] = $this->serializer->normalize($item, $format, $context);
}
} else {
$value = $this->serializer->normalize($entity->getData(), $format, $context);
}
return ['locale' => $entity->getLocale(), 'scope' => $entity->getScope(), 'value' => $value];
}
示例12: 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;
}
示例13: getSimpleValue
/**
* @param ProductValueInterface $productValue
* @param null $format
* @param array $context
*
* @return mixed
*/
protected function getSimpleValue(ProductValueInterface $productValue, $format = null, array $context = [])
{
$attributeType = $productValue->getAttribute()->getAttributeType();
$context['is_decimals_allowed'] = $productValue->getAttribute()->isDecimalsAllowed();
// if decimals_allowed is false, we return an integer
// if true, we return a string to avoid to loose precision (http://floating-point-gui.de)
if (AttributeTypes::NUMBER === $attributeType && null !== $productValue->getData() && is_numeric($productValue->getData())) {
return $productValue->getAttribute()->isDecimalsAllowed() ? number_format($productValue->getData(), static::DECIMAL_PRECISION, '.', '') : (int) $productValue->getData();
}
if (in_array($attributeType, [AttributeTypes::OPTION_SIMPLE_SELECT, AttributeTypes::REFERENCE_DATA_SIMPLE_SELECT])) {
return $productValue->getData()->getCode();
}
return $this->serializer->normalize($productValue->getData(), $format, $context);
}
示例14: normalize
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = [])
{
if (!$this->serializer instanceof NormalizerInterface) {
throw new \LogicException('Serializer must be a normalizer');
}
$data = [];
if (null !== $object->getFamily()) {
$data[self::FAMILY_FIELD] = $this->serializer->normalize($object->getFamily(), $format, $context);
}
foreach ($object->getGroups() as $group) {
$data[self::GROUPS_FIELD][] = $this->serializer->normalize($group, $format, $context);
$inGroupField = sprintf('%s_%d', self::IN_GROUP_FIELD, $group->getId());
$data[$inGroupField] = true;
}
if ($object->getCreated()) {
$data[self::CREATED_FIELD] = $this->serializer->normalize($object->getCreated(), $format, $context);
}
if ($object->getUpdated()) {
$data[self::UPDATED_FIELD] = $this->serializer->normalize($object->getUpdated(), $format, $context);
}
foreach ($object->getValues() as $value) {
$normalizedData = $this->serializer->normalize($value, $format, $context);
if (null !== $normalizedData) {
$data = array_replace($data, $normalizedData);
}
}
$completenesses = [];
foreach ($object->getCompletenesses() as $completeness) {
$completenesses = array_merge($completenesses, $this->serializer->normalize($completeness, $format, $context));
}
$data[self::COMPLETENESSES_FIELD] = $completenesses;
$data[self::ENABLED_FIELD] = (bool) $object->isEnabled();
return $data;
}
示例15: normalize
/**
* {@inheritdoc}
*/
public function normalize($entity, $format = null, array $context = [])
{
$attribute = $entity->getAttribute();
$context['decimals_allowed'] = $attribute->isDecimalsAllowed();
if ($entity->getData() instanceof Collection) {
$data = [];
foreach ($entity->getData() as $item) {
$data[] = $this->serializer->normalize($item, $format, $context);
sort($data);
}
} else {
$data = $this->serializer->normalize($entity->getData(), $format, $context);
}
// fix the number of decimals to be ISO ORM/mongo
// we decided to do it in the standard format right now but that it could/should be changed
if (AttributeTypes::NUMBER === $attribute->getAttributeType() && null !== $data && is_numeric($data)) {
$precision = true === $context['decimals_allowed'] ? static::NUMBER_DECIMAL_PRECISION : 0;
$data = number_format($data, $precision, '.', '');
}
return ['locale' => $entity->getLocale(), 'scope' => $entity->getScope(), 'data' => $data];
}