From d3976988c4777483f5a501d959fb318236f38c02 Mon Sep 17 00:00:00 2001 From: Michael Ochmann Date: Thu, 9 Mar 2023 13:53:50 +0100 Subject: [PATCH] initial commit --- .gitignore | 5 ++ composer.json | 18 +++++ src/Curl.php | 89 +++++++++++++++++++++++++ src/Error.php | 11 ++++ src/Info.php | 67 +++++++++++++++++++ src/Option.php | 176 +++++++++++++++++++++++++++++++++++++++++++++++++ src/Pause.php | 12 ++++ 7 files changed, 378 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/Curl.php create mode 100644 src/Error.php create mode 100644 src/Info.php create mode 100644 src/Option.php create mode 100644 src/Pause.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fd896f2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.idea +.vscode +.DS_Store + +vendor diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..dbb2f1a --- /dev/null +++ b/composer.json @@ -0,0 +1,18 @@ +{ + "name": "massivedynamic/curl", + "description": "a OOP wrapper for PHP cURL", + "type": "library", + "license": "MIT", + "autoload": { + "psr-4": { + "massivedynamic\\curl\\": "src/" + } + }, + "authors": [ + { + "name": "MikO", + "email": "miko@massivednyamic.eu" + } + ], + "require": {} +} diff --git a/src/Curl.php b/src/Curl.php new file mode 100644 index 0000000..4a135ba --- /dev/null +++ b/src/Curl.php @@ -0,0 +1,89 @@ +lastReturn = ""; + $this->handle = curl_init($url); + } + + public function errno() : int { + return curl_errno($this->handle); + } + + public function error() : Error { + $errno = $this->errno(); + return new Error(curl_strerror($errno), $errno); + } + + public function escape(string $string) : ?string { + $out = curl_escape($this->handle, $string); + + return $out === false ? null : $out; + } + + public function exec() : bool { + $result = curl_exec($this->handle); + + if (is_string($result)) { + $this->lastReturn = $result; + return true; + } + + return $result; + } + + public function getInfo(Info $option) : mixed { + return curl_getinfo($this->handle, $option->value); + } + + public function pause(Pause $flag) : int { + return curl_pause($this->handle, $flag->value); + } + + public function reset() : void { + $this->lastReturn = ""; + curl_reset($this->handle); + } + + public function setOpt(Option $option, mixed $value = null) : bool { + if (is_array($option)) { + $options = []; + array_walk($option, fn($key, $value) => $key instanceof \BackedEnum ? $options[$key->value] = $key : $options[$key] = $value); + assert($value === null, "Value hast to be null on `setOpt` with an array"); + return curl_setopt_array($this->handle, $options); + } + + return curl_setopt($this->handle, $option->value, $value === null ? "" : $value); + } + + public function result() : string { + return $this->lastReturn; + } + + public function close() : void { + curl_close($this->handle); + } + + public function unescape(string $string) : ?string { + $out = curl_unescape($this->handle, $string); + + return $out === false ? null : $out; + } + + public function upkeep() : bool { + return curl_upkeep($this->handle); + } + + public function version() : ?array { + $info = curl_version($this->handle); + + return $info === false ? null : $info; + } +} \ No newline at end of file diff --git a/src/Error.php b/src/Error.php new file mode 100644 index 0000000..330e2de --- /dev/null +++ b/src/Error.php @@ -0,0 +1,11 @@ +