fillup / array2xml

Simple library for converting arrays to XML
MIT License
12 stars 4 forks source link

$elementName issue on A2X.php page #1

Open jpnathannew opened 7 years ago

jpnathannew commented 7 years ago

Dear @fillup , Hope you are doing great,

I am using your one of the SDK walmart-partner-api-sdk-php. when I tried create configurable product I face one issue on variantAttributeNames array preparation.

My sample request array given below requestArray.txt

For eg: My prepared array under Footwear->Shoes category

'variantAttributeNames' => [
          'variantAttributeName' => ['color', 'shoeSize']
],

Expected XML

<variantAttributeNames>
      <variantAttributeName>color</variantAttributeName>
      <variantAttributeName>clothingSize</variantAttributeName>
</variantAttributeNames>

But I got the different type of XML. I got the following xml from A2X.php

<variantAttributeNames>
      <variantAttributeName>
            <item>color</item>
      </variantAttributeName>
      <variantAttributeName>
            <item>clothingSize</item>
      </variantAttributeName>
</variantAttributeNames>

item tag automatically added after array to xml conversion. After that I checked the A2X.php. I found one static name item on line number 121

$elementName = 'item'

After that I modified my array and modified A2X,php file. I removed variantAttributeName from my array and replace the item to variantAttributeName in A2X.php

My updated request array given below requestArray.txt

Now I get the expected result

<variantAttributeNames>
      <variantAttributeName>color</variantAttributeName>
      <variantAttributeName>clothingSize</variantAttributeName>
</variantAttributeNames>

Will my changes affect any other parts of SDK? Do you have any functions that create products XML like the expected xml above? if you have the function in SDK then I can use that function to prepare XML from array. I can revert my changes.

fillup commented 7 years ago

Hi @jpnathannew, The default 'item' element tag is used when an array is provided and the schema does not have an entry for how each item should be wrapped. So if your original array is:

$data => [
  'variantAttributeNames' => ['color', 'shoeSize']
];

Then you can tell the class how to serialize them via the schema with something like:

$schema = [
  '/variantAttributeNames' => [
      'sendItemsAs' => 'variantAttributeName'
  ]
];

$a2x = new A2X($data, $schema);
$xml = $a2x->asXml();
die($xml);

Should output (although not pretty printed like this for readability):

<variantAttributeNames>
      <variantAttributeName>color</variantAttributeName>
      <variantAttributeName>clothingSize</variantAttributeName>
</variantAttributeNames>

Now, with all that said, array2xml actually understands simple plurals for sending array items, so if the parent element ends with an s it will just strip that off for children. So you can just do:

$data => [
  'variantAttributeNames' => ['color', 'shoeSize']
];

$a2x = new A2X($data);
$xml = $a2x->asXml();
die($xml);

And you should still get:

<variantAttributeNames>
      <variantAttributeName>color</variantAttributeName>
      <variantAttributeName>clothingSize</variantAttributeName>
</variantAttributeNames>

Let me know if that works for you.

jpnathannew commented 7 years ago

Hi @fillup ,

After your comment I checked with following schema

$schema = [
    '/MPItemFeed/MPItem' => [
        'sendItemsAs' => 'MPItem',
        'includeWrappingTag' => false,
    ],
    '/variantAttributeNames' => [
      'sendItemsAs' => 'variantAttributeName'
    ],
];

If I do that change in SDK. Its working fine. Can you do this change in walmart-partner-api-sdk-php\src\item.php->bulk() function?