From 86fee3f07a479f8ba4d0cc6f2be261b1d09b915a Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sun, 10 Dec 2023 12:28:38 -0600 Subject: [PATCH 1/3] Add addFields() to Form --- src/Form.php | 12 ++++++++++++ test/FormTest.php | 5 ++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Form.php b/src/Form.php index bf790eb8..77d6f35e 100644 --- a/src/Form.php +++ b/src/Form.php @@ -49,6 +49,18 @@ public function addField(string $name, string $content, ?string $contentType = n $this->fields[] = new FormField($name, BufferedContent::fromString($content, $contentType)); } + /** + * Adds each member of the array as an entry for the given key name. Array keys are persevered. + * + * @param array $fields + */ + public function addFields(string $name, array $fields): void + { + foreach ($fields as $key => $content) { + $this->addField(\sprintf('%s[%s]', $name, $key), $content); + } + } + public function addStream(string $name, HttpContent $content, ?string $filename = null): void { if ($this->used) { diff --git a/test/FormTest.php b/test/FormTest.php index af792222..25da1492 100644 --- a/test/FormTest.php +++ b/test/FormTest.php @@ -16,8 +16,11 @@ public function testUrlEncoded(): void $body->addField('d', 'd'); $body->addField('encoding', '1+2'); + $body->addFields('list', ['one', 'two']); + $body->addFields('map', ['one' => 'one', 'two' => 'two']); + $content = buffer($body->getContent()); - $this->assertEquals("a=a&b=b&c=c&d=d&encoding=1%2B2", $content); + $this->assertEquals("a=a&b=b&c=c&d=d&encoding=1%2B2&list%5B0%5D=one&list%5B1%5D=two&map%5Bone%5D=one&map%5Btwo%5D=two", $content); } public function testMultiPartFieldsStream(): void From 2c8919fa4aff9fafb4f555c12024bd7b0cc5a92d Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Fri, 15 Dec 2023 17:56:13 -0600 Subject: [PATCH 2/3] Rename to addNestedFields --- src/Form.php | 2 +- test/FormTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Form.php b/src/Form.php index 77d6f35e..040fce61 100644 --- a/src/Form.php +++ b/src/Form.php @@ -54,7 +54,7 @@ public function addField(string $name, string $content, ?string $contentType = n * * @param array $fields */ - public function addFields(string $name, array $fields): void + public function addNestedFields(string $name, array $fields): void { foreach ($fields as $key => $content) { $this->addField(\sprintf('%s[%s]', $name, $key), $content); diff --git a/test/FormTest.php b/test/FormTest.php index 25da1492..2babd3f0 100644 --- a/test/FormTest.php +++ b/test/FormTest.php @@ -16,8 +16,8 @@ public function testUrlEncoded(): void $body->addField('d', 'd'); $body->addField('encoding', '1+2'); - $body->addFields('list', ['one', 'two']); - $body->addFields('map', ['one' => 'one', 'two' => 'two']); + $body->addNestedFields('list', ['one', 'two']); + $body->addNestedFields('map', ['one' => 'one', 'two' => 'two']); $content = buffer($body->getContent()); $this->assertEquals("a=a&b=b&c=c&d=d&encoding=1%2B2&list%5B0%5D=one&list%5B1%5D=two&map%5Bone%5D=one&map%5Btwo%5D=two", $content); From d2d3473a8fdedda2cb8375b35a71042693644fce Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sat, 16 Dec 2023 11:27:12 -0600 Subject: [PATCH 3/3] Support depth > 1 --- src/Form.php | 27 ++++++++++++++++++++++++--- test/FormTest.php | 25 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/Form.php b/src/Form.php index 040fce61..55b0374c 100644 --- a/src/Form.php +++ b/src/Form.php @@ -52,15 +52,36 @@ public function addField(string $name, string $content, ?string $contentType = n /** * Adds each member of the array as an entry for the given key name. Array keys are persevered. * - * @param array $fields + * @param array $fields */ public function addNestedFields(string $name, array $fields): void { - foreach ($fields as $key => $content) { - $this->addField(\sprintf('%s[%s]', $name, $key), $content); + foreach ($this->flattenArray($fields) as $key => $value) { + $this->addField($name . $key, $value); } } + /** + * @return array + */ + private function flattenArray(array $fields): array + { + $result = []; + foreach ($fields as $outerKey => $value) { + $key = "[{$outerKey}]"; + if (!\is_array($value)) { + $result[$key] = (string) $value; + continue; + } + + foreach ($this->flattenArray($value) as $innerKey => $flattened) { + $result[$key . $innerKey] = $flattened; + } + } + + return $result; + } + public function addStream(string $name, HttpContent $content, ?string $filename = null): void { if ($this->used) { diff --git a/test/FormTest.php b/test/FormTest.php index 2babd3f0..78e5483a 100644 --- a/test/FormTest.php +++ b/test/FormTest.php @@ -23,6 +23,31 @@ public function testUrlEncoded(): void $this->assertEquals("a=a&b=b&c=c&d=d&encoding=1%2B2&list%5B0%5D=one&list%5B1%5D=two&map%5Bone%5D=one&map%5Btwo%5D=two", $content); } + public function testNestedArrays(): void + { + $body = new Form(); + + $body->addNestedFields('map', [ + [ + 'one' => 'one', + 'two' => 'two', + ], + [ + 'one' => [1], + 'two' => [1, 2], + 'three' => [1, 2, 3], + ], + [ + 3 => 'three', + 10 => 'ten', + 42 => 'forty-two', + ], + ]); + + $content = buffer($body->getContent()); + $this->assertEquals("map%5B0%5D%5Bone%5D=one&map%5B0%5D%5Btwo%5D=two&map%5B1%5D%5Bone%5D%5B0%5D=1&map%5B1%5D%5Btwo%5D%5B0%5D=1&map%5B1%5D%5Btwo%5D%5B1%5D=2&map%5B1%5D%5Bthree%5D%5B0%5D=1&map%5B1%5D%5Bthree%5D%5B1%5D=2&map%5B1%5D%5Bthree%5D%5B2%5D=3&map%5B2%5D%5B3%5D=three&map%5B2%5D%5B10%5D=ten&map%5B2%5D%5B42%5D=forty-two", $content); + } + public function testMultiPartFieldsStream(): void { $body = new Form('ea4ba2aa9af22673bc01ae7a64c95440');