edifact icon indicating copy to clipboard operation
edifact copied to clipboard

[Interpreter] Should a segment array appear as an array of arrays with one repetition?

Open sabas opened this issue 9 years ago • 2 comments

https://github.com/php-edifact/edifact/blob/master/src/EDI/Interpreter.php#L177

If a segment repeats only one time it appears directly as a value of the measurements key. If it repeats twice or more measurements has as value an array of two or more arrays.

        array(5) {
          ["segmentIdx"]=>
          int(11)
          ["segmentCode"]=>
          string(3) "MEA"
          ["measurementApplicationQualifier"]=>
          string(3) "VGM"
          ["measurementDetails"]=>
          string(0) ""
          ["valuerange"]=>
          array(2) {
            ["measureUnitQualifier"]=>
            array(1) {
              [0]=>
              string(3) "KGM"
            }
            ["measurementValue"]=>
            array(1) {
              [1]=>
              string(4) "5951"
            }
          }
        }

sabas avatar Nov 01 '16 19:11 sabas

For usage in implementation, it's difficult to know if it's an array of arrays (multiple returns) or just an array for one result.

I was wondering why you haven't choose to make return in stdclass ?

homer8173 avatar Dec 15 '16 12:12 homer8173

I would check with is_array... The choice was done initially because it was easy enough to work with the array and to json_encode it :-)

sabas avatar Dec 15 '16 20:12 sabas

@sabas - 6 years later I got hit by this as I was reading an EDIFACT with could have multiple DTM segments but is not having them all the time. As I'm interating over the array this fails when there is only one DTM segment. Would be great to always return an array of results for consistancy whenever maxrepeat is > 1

swiffer avatar Dec 18 '22 13:12 swiffer

@swiffer wow 6 years time flies hahaha

I actually need some time to remember the issue, but I suspect it would need to tweak the doAddArray function https://github.com/php-edifact/edifact/blob/master/src/EDI/Interpreter.php#L578

So to always make it an array, something like

if (isset($array[$jsonMessage['key']])) {
$array[$jsonMessage['key']] = [];
}
$array[$jsonMessage['key']][] = $jsonMessage['value'];

Would you try something like this? In case we could do some sort of flag to control this mode.

sabas avatar Dec 28 '22 11:12 sabas

@sabas for now i'm using something like this:

    if (!array_is_list($detail['SG6'][0]['date/time/period'])) {
        $detail['SG6'][0]['date/time/period'] = [$detail['SG6'][0]['date/time/period']];
    }

you mean having an option passed to:

  new Interpreter(,...) ?

swiffer avatar Jan 27 '23 15:01 swiffer

@swiffer what I was thinking is to have a flag is a similar fashion to patchFiles which is a boolean enabling the segment patching feature https://github.com/php-edifact/edifact/blob/master/src/EDI/Interpreter.php#L45

sabas avatar Feb 02 '23 15:02 sabas

sounds good to me

swiffer avatar Feb 03 '23 07:02 swiffer

@swiffer Hi Matthias, would you please send me (mail is in profile) an EDI sample so I can think about the solution? Perhaps also highlighting what would be the desired structure... :)

Also offtopic, I updated the mappings and I wanted to alert you that now the names don't contain slashes (I found out xml doesn't want them in the name attribute) so date/time/period => dateOrtimeOrperiod

sabas avatar Feb 23 '23 20:02 sabas

Ok, I made a new flag, if set true then if the maxrepeat is greater than 1 it makes an array also for a single segment $interpreter->forceArrayWhenRepeatable(false);

sabas avatar Aug 06 '23 16:08 sabas