mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-09-05 07:16:27 +00:00
Move array methods to separate file
This commit is contained in:
@@ -242,9 +242,6 @@ Curl::setXmlDecoder($function)
|
||||
Curl::success($callback)
|
||||
Curl::unsetHeader($key)
|
||||
Curl::verbose($on = true, $output = STDERR)
|
||||
Curl::array_flatten_multidim($array, $prefix = false)
|
||||
Curl::is_array_assoc($array)
|
||||
Curl::is_array_multidim($array)
|
||||
MultiCurl::__construct($base_url = null)
|
||||
MultiCurl::__destruct()
|
||||
MultiCurl::addCurl(Curl $curl)
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace Curl;
|
||||
|
||||
class ArrayUtil
|
||||
{
|
||||
/**
|
||||
* Is Array Assoc
|
||||
*
|
||||
* @access public
|
||||
* @param $array
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function is_array_assoc($array)
|
||||
{
|
||||
return (bool)count(array_filter(array_keys($array), 'is_string'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Is Array Multidim
|
||||
*
|
||||
* @access public
|
||||
* @param $array
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function is_array_multidim($array)
|
||||
{
|
||||
if (!is_array($array)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool)count(array_filter($array, 'is_array'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Array Flatten Multidim
|
||||
*
|
||||
* @access public
|
||||
* @param $array
|
||||
* @param $prefix
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function array_flatten_multidim($array, $prefix = false)
|
||||
{
|
||||
$return = array();
|
||||
if (is_array($array) || is_object($array)) {
|
||||
if (empty($array)) {
|
||||
$return[$prefix] = '';
|
||||
} else {
|
||||
foreach ($array as $key => $value) {
|
||||
if (is_scalar($value)) {
|
||||
if ($prefix) {
|
||||
$return[$prefix . '[' . $key . ']'] = $value;
|
||||
} else {
|
||||
$return[$key] = $value;
|
||||
}
|
||||
} else {
|
||||
if ($value instanceof \CURLFile) {
|
||||
$return[$key] = $value;
|
||||
} else {
|
||||
$return = array_merge(
|
||||
$return,
|
||||
self::array_flatten_multidim(
|
||||
$value,
|
||||
$prefix ? $prefix . '[' . $key . ']' : $key
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} elseif ($array === null) {
|
||||
$return[$prefix] = $array;
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
}
|
||||
+2
-76
@@ -148,8 +148,8 @@ class Curl
|
||||
// Manually build a single-dimensional array from a multi-dimensional array as using curl_setopt($ch,
|
||||
// CURLOPT_POSTFIELDS, $data) doesn't correctly handle multi-dimensional arrays when files are
|
||||
// referenced.
|
||||
if (self::is_array_multidim($data)) {
|
||||
$data = self::array_flatten_multidim($data);
|
||||
if (\Curl\ArrayUtil::is_array_multidim($data)) {
|
||||
$data = \Curl\ArrayUtil::array_flatten_multidim($data);
|
||||
}
|
||||
|
||||
// Modify array values to ensure any referenced files are properly handled depending on the support of
|
||||
@@ -1379,78 +1379,4 @@ class Curl
|
||||
}
|
||||
return $response_headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is Array Assoc
|
||||
*
|
||||
* @access public
|
||||
* @param $array
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function is_array_assoc($array)
|
||||
{
|
||||
return (bool)count(array_filter(array_keys($array), 'is_string'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Is Array Multidim
|
||||
*
|
||||
* @access public
|
||||
* @param $array
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function is_array_multidim($array)
|
||||
{
|
||||
if (!is_array($array)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool)count(array_filter($array, 'is_array'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Array Flatten Multidim
|
||||
*
|
||||
* @access public
|
||||
* @param $array
|
||||
* @param $prefix
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function array_flatten_multidim($array, $prefix = false)
|
||||
{
|
||||
$return = array();
|
||||
if (is_array($array) || is_object($array)) {
|
||||
if (empty($array)) {
|
||||
$return[$prefix] = '';
|
||||
} else {
|
||||
foreach ($array as $key => $value) {
|
||||
if (is_scalar($value)) {
|
||||
if ($prefix) {
|
||||
$return[$prefix . '[' . $key . ']'] = $value;
|
||||
} else {
|
||||
$return[$key] = $value;
|
||||
}
|
||||
} else {
|
||||
if ($value instanceof \CURLFile) {
|
||||
$return[$key] = $value;
|
||||
} else {
|
||||
$return = array_merge(
|
||||
$return,
|
||||
self::array_flatten_multidim(
|
||||
$value,
|
||||
$prefix ? $prefix . '[' . $key . ']' : $key
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} elseif ($array === null) {
|
||||
$return[$prefix] = $array;
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ class CurlTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
public function testArrayAssociative()
|
||||
{
|
||||
$this->assertTrue(Curl::is_array_assoc(array(
|
||||
$this->assertTrue(\Curl\ArrayUtil::is_array_assoc(array(
|
||||
'foo' => 'wibble',
|
||||
'bar' => 'wubble',
|
||||
'baz' => 'wobble',
|
||||
@@ -23,7 +23,7 @@ class CurlTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
public function testArrayIndexed()
|
||||
{
|
||||
$this->assertFalse(Curl::is_array_assoc(array(
|
||||
$this->assertFalse(\Curl\ArrayUtil::is_array_assoc(array(
|
||||
'wibble',
|
||||
'wubble',
|
||||
'wobble',
|
||||
|
||||
Reference in New Issue
Block a user