/** * Test flattenMultidimensionalArray method */ public function testFlattenMultidimensionalArray() { $emptyArray = array(); $notMultidimensionalArray = array('foo', 'bar', 'var'); $multidimensionalArray = array('key1' => 'value1', 'key2' => array('key2.1' => 'value2.1', 'key2.2' => 'value2.2'), 'key3' => array('key3.1' => 'value3.1', 'key3.2' => array('key3.2.1' => 'value3.2.1', 'key3.2.2' => 'value3.2.2', 'key3.2.3' => 'value3.2.3'), 'key3.3' => array('key3.3.1' => 'value3.3.1', 'key3.3.2' => 'value3.3.2'))); $flattenedMultidimensionalArray = array('key1' => 'value1', 'key2[key2.1]' => 'value2.1', 'key2[key2.2]' => 'value2.2', 'key3[key3.1]' => 'value3.1', 'key3[key3.2][key3.2.1]' => 'value3.2.1', 'key3[key3.2][key3.2.2]' => 'value3.2.2', 'key3[key3.2][key3.2.3]' => 'value3.2.3', 'key3[key3.3][key3.3.1]' => 'value3.3.1', 'key3[key3.3][key3.3.2]' => 'value3.3.2'); $this->assertEquals($emptyArray, ArrayUtil::flattenMultidimensionalArray($emptyArray)); $this->assertEquals($notMultidimensionalArray, ArrayUtil::flattenMultidimensionalArray($notMultidimensionalArray)); $this->assertEquals(false, $multidimensionalArray == ArrayUtil::flattenMultidimensionalArray($multidimensionalArray)); $this->assertEquals($flattenedMultidimensionalArray, ArrayUtil::flattenMultidimensionalArray($multidimensionalArray)); }
/** * Note: If data is a multidimensional array, we have to flatten it before posting it, i.e., an array like: * * [ * key1: value1 * key2: [ * key2.1: value2.1 * key2.2: value2.2 * ] * key3: [ * key3.1: [ * key3.1.1: value3.1.1 * key3.1.2: value3.1.2 * ] * key3.2: value3.2 * ] * ] * * must become * * [ * key1: value1 * key2[key2.1]: value2.1 * key2[key2.2]: value2.2 * key3[key3.1][key3.1.1]: value3.1.1 * key3[key3.1][key3.1.2]: value3.1.2 * key3[key3.2]: value3.2 * ] * * Otherwise, curl is not able to perform the request and throws an error * * @param string $method * @param string $url * @param array $data * @param array $headers * @return mixed */ private function sendData($method, $url, $data, $headers) { // Set headers $this->setHeaders($headers); // Set template $this->setTemplate($method); // Flatten array if it is multidimensional if (ArrayUtil::isMultidimensionalArray($data)) { $data = ArrayUtil::flattenMultidimensionalArray($data); } // We have to post files and data separately $postData = array(); $files = array(); foreach ($data as $key => $value) { if ($value instanceof UploadedFile) { $files[$key] = $this->saveTemporaryFile($value); } else { $postData[$key] = $value; } } // Prepare the request $request = Request::$method($url); // Set fields data if (is_array($postData) && !empty($postData)) { $request->body($postData); } // Set files data if (is_array($files) && !empty($files)) { $request->attach($files); } // Perform the request $this->data = $request->send(); // Remove temporary files if (is_array($files) && !empty($files)) { foreach ($files as $file) { $this->removeTemporaryFile($file); } } }