Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion Curl/CurlOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@
*/
final class CurlOptions
{
/**
* @var array Associtative array of constant value => constant type constrain
*/
static private $option_value_types = array();

/**
* @var array Associative array of constant value => constant name
*/
static private $option_value_names = array();

/**
* Checks which cURL constants is defined and loads them as valid options
*/
Expand Down Expand Up @@ -141,10 +149,30 @@ static private function loadOptions() {
foreach ($options as $option => $type) {
if (defined($option)) {
static::$option_value_types[constant($option)] = $type;
static::$option_value_names[constant($option)] = $option;
}
}
}

/**
* Fetch option constant realname from a given curl option value
*
* @param int $option curl option value CURLOPT_*
* @return string curl option constant name
*/
static public function getName($option)
{
if (!static::$option_value_types) {
static::loadOptions();
}

if (!array_key_exists($option, static::$option_value_names)) {
throw new \OutOfRangeException('Illegal index ' . $option);
}

return static::$option_value_names[$option];
}

/**
* Determine whether or not the value passed is a valid cURL option
*
Expand Down Expand Up @@ -179,7 +207,7 @@ static public function checkOptionValue($option, $value, $throw = true) {
$result = static::checkType($value, static::$option_value_types[$option]);

if(!$result && $throw) {
throw new \InvalidArgumentException("Invalid value for the given cURL option");
throw new \InvalidArgumentException("Invalid value for the given cURL option " . self::$option_value_names[$option]);
}

return $result;
Expand All @@ -188,6 +216,13 @@ static public function checkOptionValue($option, $value, $throw = true) {
}
}

/**
* Check a value against a given string type
*
* @param mixed $value option value to check against the input type
* @param string $type a given type in bool, string, int, resource, callable, ..
* @return bool true if type ok, false otherwise
*/
static private function checkType($value, $type) {

$result = false;
Expand Down