php


Symfony3 Serializer - How to separate values from dot strings and combine in one array column


I get data from http request in given form
{
"start_date": "2017-03-13",
"end_date": "2017-03-19",
"visitors_total": 2555,
"views_total": 2553,
"visitors_country.france": 100,
"visitors_country.germany": 532,
"visitors_country.poland": 32,
"views_country.france": 110,
"views_country.germany": 821,
"views_country.poland": 312,
}
doctrine entity defination for columns
"start_date" => datetime
"end_date" => datetime
"visitors_total" => int
"views_total" => int
"visitors_country" => array
"views_country => array
For the visitors_country and views_country , array keys/values are separated by dots. These dot separated values
"views_country.france": 110,
"views_country.germany": 821,
"views_country.poland": 312,
shoud be
'view_country' => array(
'france'=> 110,
'germany'=> 821,
'poland'=> 312,
);
I am using Symfony serialize component for the serialization of requested data and having problem to denormalize the data.
I did something like this
class ArrayDotNormalizer implements DenormalizerInterface
{
/**
* {#inheritdoc}
*
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
// Actually, this function applies to each column of requested data ,
//but how to separate values by dot and join them in one array and store as array json in db ?
}
/**
* {#inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return strpos($data, '.') !== false;
}
}
Any idea to solve this?
Try with this:
class ArrayDotNormalizer extends ObjectNormalizer
{
/**
* {#inheritdoc}
*/
protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = [])
{
if (strpos($attribute, '.') !== false) {
list($attribute, $country) = explode('.', $attribute);
$currentValue = (array) $this->propertyAccessor->getValue($object, $attribute);
$value = array_replace($currentValue, [$country => $value]);
}
return parent::setAttributeValue($object, $attribute, $value, $format, $context);
}
}
and use this normalizer in your serializer:
$serializer = new Serializer([new ArrayDotNormalizer()], [new JsonEncoder()]);
Result:

Related Links

Trying to change it so that the system gets the date. Not to get the date from the user
Prestashop payment module
PHP log in form error [closed]
array_key_exsists not working as expected PHP
How to validate input field against the database in Yii 2.0?
Cannot use object of type PDOStatement as array - pdo query
Symfony 2.5 - I am unable to catch PDOException
Google App Engine Failed opening SMARTY php scripts
Symfony2 and Twig performance in a vagrant box
Gmail: Too many login attempts
Stop input from sending empty values
Create TABLE in PHP using
basic php: redirect page using page id
json encode array from php sql server and convert to Android
bindParam with md5() column PDO PHP
Get PHP Opcodes Dynamically at Runtime

Categories

HOME
loopbackjs
nsview
autocomplete
elisp
flurry
mosquitto
clone
msmq
locationmanager
ex
texas-instruments
supervisord
heap-dump
maven-plugin
tfsbuild
parceler
azure-machine-learning
exchange-server-2010
mod-wsgi
clipboard
network-analysis
audit
rspec-rails
creation
rails-postgresql
des
textmate2
keyboard-layout
code-rally
cartopy
unification
export-to-pdf
swift2.3
sonarqube5.2
visual-format-language
extjs4.1
simple-schema
connect-direct
1010
kendo-editor
dynamics-crm-4
emf-compare
sidewaffle
d3v4
allegro
pjax
xmlbeans
pyaudio
jenkins-workflow
declarative
right-to-left
highlighting
rails-engines
jeasyui
mathnet
psd
wif4.5
webgrind
lovefield
godeps
blockquote
gql
karma-coverage
ampersand
unidata
dbmigrate
quercus
fuseesb
subview
findersync
mojo
google-hangouts
flurry-analytics
dmake
iplimage
quantify
.htpasswd
pre
usb-flash-drive
eclim
grails-2.3
form-for
chartfx
statechart
blobstorage
spiral
ntlmv2
smartfox
email-verification
tridion-2011
wndproc
firebird1.5
prototypal-inheritance
nsfont
fbml
representation
kernel32
mediacontroller
css-friendly
django-pagination
anonymous-types
sscli
.net-client-profile

Resources

Encrypt Message