Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Possibility to use arrays in fields #70

Open
jg-development opened this issue Sep 24, 2014 · 10 comments
Open

Possibility to use arrays in fields #70

jg-development opened this issue Sep 24, 2014 · 10 comments

Comments

@jg-development
Copy link

Hi,

I want to post 2 addresses in one request.
(like billingaddress and deliveryaddress)
It seems Apigility cannot work with a fieldname like "billingAddress[salutation]". In the config this is just a string and not an array. Is it in any way possible to use input parameters as arrays?

Greetings Jan

@jg-development jg-development changed the title Possibility to use arrays in fiels Possibility to use arrays in fiedls Sep 24, 2014
@jg-development jg-development changed the title Possibility to use arrays in fiedls Possibility to use arrays in fields Sep 24, 2014
@michsk
Copy link

michsk commented Aug 12, 2015

I made a quick patch which works with the multiform and can handle flat arrays. Deep arrays foo[bar[baz]] are not supported only foo[] will work with this patch.

Index: vendor/zfcampus/zf-content-negotiation/src/MultipartContentParser.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>windows-1252
===================================================================
--- vendor/zfcampus/zf-content-negotiation/src/MultipartContentParser.php   (revision d779bbfde1cc5802c0d6a5f2ac9cc34bf5b71475)
+++ vendor/zfcampus/zf-content-negotiation/src/MultipartContentParser.php   (revision )
@@ -111,7 +111,9 @@
                     // Time to handle the data we've already parsed!
                     // Data
                     if (! $filename) {
-                        $data->set($name, rtrim($content, "\r\n"));
+//                        $content = rtrim($content, "\r\n");
+//                        $data->set($name, $content);
+                        $this->processContent($data, $name, $content);
                     }

                     // File (successful upload so far)
@@ -264,6 +266,35 @@
         }

         return $data->toArray();
+    }
+
+    /**
+     * Process the non-file data.
+     * @param Parameters $parameters
+     * @param $name
+     * @param $content
+     */
+    protected function processContent(Parameters $parameters, $name, $content)
+    {
+        $isArray    = strstr($name, '[]');
+        $name       = rtrim($name, '[]');
+        $content    = rtrim($content, "\r\n");
+
+        if ($parameters->offsetExists($name)) {
+            $data = $parameters->get($name);
+
+            if (is_array($data)) {
+                $data[] = $content;
+            } else {
+                $data = [$data, $content];
+            }
+
+            $parameters->set($name, $data);
+        } elseif ($isArray)  {
+            $parameters->set($name, (array) $content);
+        } else {
+            $parameters->set($name, $content);
+        }
     }

     /**

It will behave somewhat like the $_POST. Thats why we remove the [] from the name

@Wilt
Copy link

Wilt commented Aug 12, 2015

I am not exactly sure where you do not manage to configure your nested field. But Apigility is definitely able to handle such cases. Check an example here on stackoverflow.
If this is not what you mean then please be more specific about where the configuration is failing.

@michsk
Copy link

michsk commented Aug 13, 2015

When i was implementing your SO solution i had the feeling that it's for a fixed list? What if i would want to accept a normal array.

So my post could contain:

foo-bar[] = 1
foo-bar[] = 2
foo-bar[] = 3

@michsk
Copy link

michsk commented Aug 14, 2015

@Wilt i found some other posts of you regarding this aswell as the post about the raw $data param we have in the methods. I am using this raw $data param, and that doesnt seem to work with arrays.

@Wilt
Copy link

Wilt commented Aug 17, 2015

I am not sure what you want to achieve. From the address example at the beginning I assumed you want to post a collection of addresses. But with your last foo-bar example it seems you simply want to post some array values. Do you want to post an array variable or a collection?
Can you be more explicit about your data type?

@michsk
Copy link

michsk commented Aug 17, 2015

Array values as defined in foobar example. And get them from the raw data param which we have in the controllers

@Wilt
Copy link

Wilt commented Aug 18, 2015

Maybe the problem lies in your json structure. If I post data like this it works totally fine:

{
    "foo-bar": [1, 2, 3]
}

@michsk
Copy link

michsk commented Aug 18, 2015

No, i use multiform

@michsk
Copy link

michsk commented Sep 14, 2015

Created a pull request for this, maybe that makes it more obviouse; pull request: zfcampus/zf-content-negotiation#57

@michalbundyra
Copy link
Member

This repository has been closed and moved to laminas-api-tools/api-tools; a new issue has been opened at laminas-api-tools/api-tools#63.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants