本文整理汇总了PHP中Symfony\Component\Serializer\SerializerInterface::denormalize方法的典型用法代码示例。如果您正苦于以下问题:PHP SerializerInterface::denormalize方法的具体用法?PHP SerializerInterface::denormalize怎么用?PHP SerializerInterface::denormalize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Serializer\SerializerInterface
的用法示例。
在下文中一共展示了SerializerInterface::denormalize方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: denormalize
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = [])
{
$result = $this->createObject($class);
$fields = $this->fieldHelper->getFields($class, true);
foreach ($fields as $field) {
$fieldName = $field['name'];
if (array_key_exists($fieldName, $data)) {
$value = $data[$fieldName];
if ($data[$fieldName] !== null && ($this->fieldHelper->isRelation($field) || $this->fieldHelper->isDateTimeField($field))) {
if ($this->fieldHelper->isMultipleRelation($field)) {
$entityClass = sprintf('ArrayCollection<%s>', $field['related_entity_name']);
} elseif ($this->fieldHelper->isSingleRelation($field)) {
$entityClass = $field['related_entity_name'];
} else {
$entityClass = 'DateTime';
$context = array_merge($context, ['type' => $field['type']]);
}
$context = array_merge($context, ['fieldName' => $fieldName]);
$value = $this->serializer->denormalize($value, $entityClass, $format, $context);
}
$this->fieldHelper->setObjectValue($result, $fieldName, $value);
}
}
return $result;
}
示例2: denormalize
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = [])
{
if (empty($data)) {
return null;
}
$options = new ArrayCollection();
foreach ($data as $optionCode) {
$option = $this->serializer->denormalize($optionCode, AttributeTypes::OPTION_SIMPLE_SELECT, $format, $context);
$options->add($option);
}
return $options;
}
示例3: denormalize
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = [])
{
if (empty($data)) {
return null;
}
$options = new ArrayCollection();
foreach ($data as $optionCode) {
$option = $this->serializer->denormalize($optionCode, 'pim_catalog_simpleselect', $format, $context);
$options->add($option);
}
return $options;
}
示例4: denormalize
/**
* @param mixed $data
* @param string $class
* @param mixed $format
* @param array $context
* @return AbstractTypedAddress
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
/** @var AbstractTypedAddress $result */
$result = $this->addressNormalizer->denormalize($data, $class, $format, $context);
if (!empty($data['types']) && is_array($data['types'])) {
$types = $this->serializer->denormalize($data['types'], static::TYPES_TYPE, $format, $context);
if ($types) {
foreach ($types as $type) {
$result->addType($type);
}
}
}
return $result;
}
示例5: denormalize
/**
* Returns collection of denormalized data
*
* @param mixed $data
* @param string $class
* @param mixed $format
* @param array $context
* @return ArrayCollection
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
if (!is_array($data)) {
return new ArrayCollection();
}
$itemType = $this->getItemType($class);
if (!$itemType) {
return new ArrayCollection($data);
}
$result = new ArrayCollection();
foreach ($data as $item) {
$result->add($this->serializer->denormalize($item, $itemType, $format, $context));
}
return $result;
}
示例6: denormalize
/**
* @param array $values
* @param string $class
* @param string $format
* @param array $context
*
* @return array|object
*/
public function denormalize($values, $class, $format = null, array $context = array())
{
if (!is_array($values)) {
return $values;
}
$filtered = array_filter($values, function ($value) use($class, $format) {
return $this->serializer->supportsDenormalization($value, $class, $format);
});
if (count($filtered) !== count($values)) {
throw new \InvalidArgumentException('Not all values within the array can be denormalized.');
}
return array_map(function ($value) use($class, $format, $context) {
return $this->serializer->denormalize($value, $class, $format, $context);
}, $filtered);
}
示例7: denormalizeObject
/**
* @param array $data
* @param string $name
* @param string $type
* @param mixed $format
* @param array $context
*
* @return null|object
*/
protected function denormalizeObject(array $data, $name, $type, $format = null, $context = array())
{
$result = null;
if (!empty($data[$name])) {
$result = $this->serializer->denormalize($data[$name], $type, $format, $context);
}
return $result;
}
示例8: handleUpdateRequest
/**
* {@inheritdoc}
*/
public function handleUpdateRequest(Request $request, $identifier)
{
$parameters = $request->request->all();
$resource = $this->getResourceById($identifier);
$className = ClassUtils::getRealClass(get_class($resource));
$resource = $this->serializer->denormalize($parameters, $className, self::RESPONSE_FORMAT, ['resource' => $resource]);
$data = $this->serializer->serialize($resource, self::RESPONSE_FORMAT, ['group' => $this->getResourceType()]);
$this->manager->updateResource($resource);
return new Response($data);
}
示例9: denormalize
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = [])
{
if (!isset($context['attribute'])) {
throw new InvalidArgumentException('Attribute must be passed in the context');
}
$attribute = $context['attribute'];
if (!$attribute instanceof AttributeInterface) {
throw new InvalidArgumentException(sprintf('Attribute must be an instance of %s, %s given', 'Pim\\Bundle\\CatalogBundle\\Model\\AttributeInterface', is_object($attribute) ? get_class($attribute) : gettype($attribute)));
}
$data = $data + ['locale' => null, 'scope' => null, 'value' => null];
$value = new $this->entityClass();
$value->setAttribute($attribute);
$value->setLocale($data['locale']);
$value->setScope($data['scope']);
$valueData = $this->serializer->denormalize($data['value'], $attribute->getAttributeType(), $format, $context);
if (null !== $valueData) {
$value->setData($valueData);
}
return $value;
}
示例10: denormalize
/**
* @param mixed $data
* @param string $class
* @param mixed $format
* @param array $context
* @return mixed
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
$fieldRules = $this->getProcessedFieldRules();
if (!is_array($data)) {
if ($this->primaryField) {
$data = array($this->primaryField['normalizeName'] => $data);
} else {
return $this->createNewObject();
}
}
$object = $this->createNewObject();
foreach ($fieldRules as $field) {
if (!$field['denormalize'] || !array_key_exists($field['normalizeName'], $data)) {
continue;
}
$value = $data[$field['normalizeName']];
if (isset($field['type']) && $value !== null) {
$value = $this->serializer->denormalize($data[$field['normalizeName']], $field['type'], $format, array_merge($context, $field['context']));
}
$this->getPropertyAccessor()->setValue($object, $field['denormalizeName'], $value);
}
return $object;
}