Skip to content

Commit b6e6524

Browse files
committed
Move array methods to separate file
1 parent bf701e7 commit b6e6524

File tree

4 files changed

+84
-81
lines changed

4 files changed

+84
-81
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,6 @@ Curl::setXmlDecoder($function)
242242
Curl::success($callback)
243243
Curl::unsetHeader($key)
244244
Curl::verbose($on = true, $output = STDERR)
245-
Curl::array_flatten_multidim($array, $prefix = false)
246-
Curl::is_array_assoc($array)
247-
Curl::is_array_multidim($array)
248245
MultiCurl::__construct($base_url = null)
249246
MultiCurl::__destruct()
250247
MultiCurl::addCurl(Curl $curl)

src/Curl/ArrayUtil.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Curl;
4+
5+
class ArrayUtil
6+
{
7+
/**
8+
* Is Array Assoc
9+
*
10+
* @access public
11+
* @param $array
12+
*
13+
* @return boolean
14+
*/
15+
public static function is_array_assoc($array)
16+
{
17+
return (bool)count(array_filter(array_keys($array), 'is_string'));
18+
}
19+
20+
/**
21+
* Is Array Multidim
22+
*
23+
* @access public
24+
* @param $array
25+
*
26+
* @return boolean
27+
*/
28+
public static function is_array_multidim($array)
29+
{
30+
if (!is_array($array)) {
31+
return false;
32+
}
33+
34+
return (bool)count(array_filter($array, 'is_array'));
35+
}
36+
37+
/**
38+
* Array Flatten Multidim
39+
*
40+
* @access public
41+
* @param $array
42+
* @param $prefix
43+
*
44+
* @return array
45+
*/
46+
public static function array_flatten_multidim($array, $prefix = false)
47+
{
48+
$return = array();
49+
if (is_array($array) || is_object($array)) {
50+
if (empty($array)) {
51+
$return[$prefix] = '';
52+
} else {
53+
foreach ($array as $key => $value) {
54+
if (is_scalar($value)) {
55+
if ($prefix) {
56+
$return[$prefix . '[' . $key . ']'] = $value;
57+
} else {
58+
$return[$key] = $value;
59+
}
60+
} else {
61+
if ($value instanceof \CURLFile) {
62+
$return[$key] = $value;
63+
} else {
64+
$return = array_merge(
65+
$return,
66+
self::array_flatten_multidim(
67+
$value,
68+
$prefix ? $prefix . '[' . $key . ']' : $key
69+
)
70+
);
71+
}
72+
}
73+
}
74+
}
75+
} elseif ($array === null) {
76+
$return[$prefix] = $array;
77+
}
78+
return $return;
79+
}
80+
}

src/Curl/Curl.php

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ public function buildPostData($data)
148148
// Manually build a single-dimensional array from a multi-dimensional array as using curl_setopt($ch,
149149
// CURLOPT_POSTFIELDS, $data) doesn't correctly handle multi-dimensional arrays when files are
150150
// referenced.
151-
if (self::is_array_multidim($data)) {
152-
$data = self::array_flatten_multidim($data);
151+
if (\Curl\ArrayUtil::is_array_multidim($data)) {
152+
$data = \Curl\ArrayUtil::array_flatten_multidim($data);
153153
}
154154

155155
// Modify array values to ensure any referenced files are properly handled depending on the support of
@@ -1379,78 +1379,4 @@ private function parseResponseHeaders($raw_response_headers)
13791379
}
13801380
return $response_headers;
13811381
}
1382-
1383-
/**
1384-
* Is Array Assoc
1385-
*
1386-
* @access public
1387-
* @param $array
1388-
*
1389-
* @return boolean
1390-
*/
1391-
public static function is_array_assoc($array)
1392-
{
1393-
return (bool)count(array_filter(array_keys($array), 'is_string'));
1394-
}
1395-
1396-
/**
1397-
* Is Array Multidim
1398-
*
1399-
* @access public
1400-
* @param $array
1401-
*
1402-
* @return boolean
1403-
*/
1404-
public static function is_array_multidim($array)
1405-
{
1406-
if (!is_array($array)) {
1407-
return false;
1408-
}
1409-
1410-
return (bool)count(array_filter($array, 'is_array'));
1411-
}
1412-
1413-
/**
1414-
* Array Flatten Multidim
1415-
*
1416-
* @access public
1417-
* @param $array
1418-
* @param $prefix
1419-
*
1420-
* @return array
1421-
*/
1422-
public static function array_flatten_multidim($array, $prefix = false)
1423-
{
1424-
$return = array();
1425-
if (is_array($array) || is_object($array)) {
1426-
if (empty($array)) {
1427-
$return[$prefix] = '';
1428-
} else {
1429-
foreach ($array as $key => $value) {
1430-
if (is_scalar($value)) {
1431-
if ($prefix) {
1432-
$return[$prefix . '[' . $key . ']'] = $value;
1433-
} else {
1434-
$return[$key] = $value;
1435-
}
1436-
} else {
1437-
if ($value instanceof \CURLFile) {
1438-
$return[$key] = $value;
1439-
} else {
1440-
$return = array_merge(
1441-
$return,
1442-
self::array_flatten_multidim(
1443-
$value,
1444-
$prefix ? $prefix . '[' . $key . ']' : $key
1445-
)
1446-
);
1447-
}
1448-
}
1449-
}
1450-
}
1451-
} elseif ($array === null) {
1452-
$return[$prefix] = $array;
1453-
}
1454-
return $return;
1455-
}
14561382
}

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function testExtensionsLoaded()
1414

1515
public function testArrayAssociative()
1616
{
17-
$this->assertTrue(Curl::is_array_assoc(array(
17+
$this->assertTrue(\Curl\ArrayUtil::is_array_assoc(array(
1818
'foo' => 'wibble',
1919
'bar' => 'wubble',
2020
'baz' => 'wobble',
@@ -23,7 +23,7 @@ public function testArrayAssociative()
2323

2424
public function testArrayIndexed()
2525
{
26-
$this->assertFalse(Curl::is_array_assoc(array(
26+
$this->assertFalse(\Curl\ArrayUtil::is_array_assoc(array(
2727
'wibble',
2828
'wubble',
2929
'wobble',

0 commit comments

Comments
 (0)