Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to use arrays in fields #63

Open
michalbundyra opened this issue Jan 10, 2020 · 9 comments
Open

Possibility to use arrays in fields #63

michalbundyra opened this issue Jan 10, 2020 · 9 comments

Comments

@michalbundyra
Copy link
Member

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


Originally posted by @jg-development at zfcampus/zf-apigility#70

@michalbundyra
Copy link
Member Author

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


Originally posted by @Michal-sk at zfcampus/zf-apigility#70 (comment)

@michalbundyra
Copy link
Member Author

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.


Originally posted by @Wilt at zfcampus/zf-apigility#70 (comment)

@michalbundyra
Copy link
Member Author

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

Originally posted by @Michal-sk at zfcampus/zf-apigility#70 (comment)

@michalbundyra
Copy link
Member Author

@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.


Originally posted by @Michal-sk at zfcampus/zf-apigility#70 (comment)

@michalbundyra
Copy link
Member Author

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?


Originally posted by @Wilt at zfcampus/zf-apigility#70 (comment)

@michalbundyra
Copy link
Member Author

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


Originally posted by @Michal-sk at zfcampus/zf-apigility#70 (comment)

@michalbundyra
Copy link
Member Author

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

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

Originally posted by @Wilt at zfcampus/zf-apigility#70 (comment)

@michalbundyra
Copy link
Member Author

No, i use multiform


Originally posted by @Michal-sk at zfcampus/zf-apigility#70 (comment)

@michalbundyra
Copy link
Member Author

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


Originally posted by @Michal-sk at zfcampus/zf-apigility#70 (comment)

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

No branches or pull requests

1 participant