diff --git a/src/PhpSpreadsheet/Calculation/Calculation.php b/src/PhpSpreadsheet/Calculation/Calculation.php index f24cbeb5..1a0fb6d7 100644 --- a/src/PhpSpreadsheet/Calculation/Calculation.php +++ b/src/PhpSpreadsheet/Calculation/Calculation.php @@ -933,7 +933,7 @@ class Calculation ], 'ENCODEURL' => [ 'category' => Category::CATEGORY_WEB, - 'functionCall' => [Functions::class, 'DUMMY'], + 'functionCall' => [Web\Service::class, 'urlEncode'], 'argumentCount' => '1', ], 'EOMONTH' => [ @@ -2569,7 +2569,7 @@ class Calculation ], 'WEBSERVICE' => [ 'category' => Category::CATEGORY_WEB, - 'functionCall' => [Web::class, 'WEBSERVICE'], + 'functionCall' => [Web\Service::class, 'webService'], 'argumentCount' => '1', ], 'WEEKDAY' => [ diff --git a/src/PhpSpreadsheet/Calculation/Database.php b/src/PhpSpreadsheet/Calculation/Database.php index a4c4d7d2..65031674 100644 --- a/src/PhpSpreadsheet/Calculation/Database.php +++ b/src/PhpSpreadsheet/Calculation/Database.php @@ -17,7 +17,8 @@ class Database * * @Deprecated 1.17.0 * - * @see Use the evaluate() method in the Database\DAverage class instead + * @see Database\DAverage::evaluate() + * Use the evaluate() method in the Database\DAverage class instead * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related @@ -52,7 +53,8 @@ class Database * * @Deprecated 1.17.0 * - * @see Use the evaluate() method in the Database\DCount class instead + * @see Database\DCount::evaluate() + * Use the evaluate() method in the Database\DCount class instead * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related @@ -89,7 +91,8 @@ class Database * * @Deprecated 1.17.0 * - * @see Use the evaluate() method in the Database\DCountA class instead + * @see Database\DCountA::evaluate() + * Use the evaluate() method in the Database\DCountA class instead * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related @@ -124,7 +127,8 @@ class Database * * @Deprecated 1.17.0 * - * @see Use the evaluate() method in the Database\DGet class instead + * @see Database\DGet::evaluate() + * Use the evaluate() method in the Database\DGet class instead * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related @@ -159,7 +163,8 @@ class Database * * @Deprecated 1.17.0 * - * @see Use the evaluate() method in the Database\DMax class instead + * @see Database\DMax::evaluate() + * Use the evaluate() method in the Database\DMax class instead * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related @@ -194,7 +199,8 @@ class Database * * @Deprecated 1.17.0 * - * @see Use the evaluate() method in the Database\DMin class instead + * @see Database\DMin::evaluate() + * Use the evaluate() method in the Database\DMin class instead * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related @@ -228,7 +234,8 @@ class Database * * @Deprecated 1.17.0 * - * @see Use the evaluate() method in the Database\DProduct class instead + * @see Database\DProduct::evaluate() + * Use the evaluate() method in the Database\DProduct class instead * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related @@ -263,7 +270,8 @@ class Database * * @Deprecated 1.17.0 * - * @see Use the evaluate() method in the Database\DStDev class instead + * @see Database\DStDev::evaluate() + * Use the evaluate() method in the Database\DStDev class instead * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related @@ -298,7 +306,8 @@ class Database * * @Deprecated 1.17.0 * - * @see Use the evaluate() method in the Database\DStDevP class instead + * @see Database\DStDevP::evaluate() + * Use the evaluate() method in the Database\DStDevP class instead * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related @@ -332,7 +341,8 @@ class Database * * @Deprecated 1.17.0 * - * @see Use the evaluate() method in the Database\DSum class instead + * @see Database\DSum::evaluate() + * Use the evaluate() method in the Database\DSum class instead * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related @@ -367,7 +377,8 @@ class Database * * @Deprecated 1.17.0 * - * @see Use the evaluate() method in the Database\DVar class instead + * @see Database\DVar::evaluate() + * Use the evaluate() method in the Database\DVar class instead * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related @@ -402,7 +413,8 @@ class Database * * @Deprecated 1.17.0 * - * @see Use the evaluate() method in the Database\DVarP class instead + * @see Database\DVarP::evaluate() + * Use the evaluate() method in the Database\DVarP class instead * * @param mixed[] $database The range of cells that makes up the list or database. * A database is a list of related data in which rows of related diff --git a/src/PhpSpreadsheet/Calculation/Web.php b/src/PhpSpreadsheet/Calculation/Web.php index 5cfd2ea8..3f3d945d 100644 --- a/src/PhpSpreadsheet/Calculation/Web.php +++ b/src/PhpSpreadsheet/Calculation/Web.php @@ -2,9 +2,9 @@ namespace PhpOffice\PhpSpreadsheet\Calculation; -use PhpOffice\PhpSpreadsheet\Settings; -use Psr\Http\Client\ClientExceptionInterface; - +/** + * @deprecated 1.18.0 + */ class Web { /** @@ -15,39 +15,13 @@ class Web * Excel Function: * Webservice(url) * + * @see Web\Service::webService() + * Use the webService() method in the Web\Service class instead + * * @return string the output resulting from a call to the webservice */ public static function WEBSERVICE(string $url) { - $url = trim($url); - if (strlen($url) > 2048) { - return Functions::VALUE(); // Invalid URL length - } - - if (!preg_match('/^http[s]?:\/\//', $url)) { - return Functions::VALUE(); // Invalid protocol - } - - // Get results from the the webservice - $client = Settings::getHttpClient(); - $requestFactory = Settings::getRequestFactory(); - $request = $requestFactory->createRequest('GET', $url); - - try { - $response = $client->sendRequest($request); - } catch (ClientExceptionInterface $e) { - return Functions::VALUE(); // cURL error - } - - if ($response->getStatusCode() != 200) { - return Functions::VALUE(); // cURL error - } - - $output = $response->getBody()->getContents(); - if (strlen($output) > 32767) { - return Functions::VALUE(); // Output not a string or too long - } - - return $output; + return Web\Service::webService($url); } } diff --git a/src/PhpSpreadsheet/Calculation/Web/Service.php b/src/PhpSpreadsheet/Calculation/Web/Service.php new file mode 100644 index 00000000..05e04bf9 --- /dev/null +++ b/src/PhpSpreadsheet/Calculation/Web/Service.php @@ -0,0 +1,75 @@ + 2048) { + return Functions::VALUE(); // Invalid URL length + } + + if (!preg_match('/^http[s]?:\/\//', $url)) { + return Functions::VALUE(); // Invalid protocol + } + + // Get results from the the webservice + $client = Settings::getHttpClient(); + $requestFactory = Settings::getRequestFactory(); + $request = $requestFactory->createRequest('GET', $url); + + try { + $response = $client->sendRequest($request); + } catch (ClientExceptionInterface $e) { + return Functions::VALUE(); // cURL error + } + + if ($response->getStatusCode() != 200) { + return Functions::VALUE(); // cURL error + } + + $output = $response->getBody()->getContents(); + if (strlen($output) > 32767) { + return Functions::VALUE(); // Output not a string or too long + } + + return $output; + } + + /** + * URLENCODE. + * + * Returns data from a web service on the Internet or Intranet. + * + * Excel Function: + * urlEncode(text) + * + * @param mixed $text + * + * @return string the url encoded output + */ + public static function urlEncode($text) + { + if (!is_string($text)) { + return Functions::VALUE(); + } + + return str_replace('+', '%20', urlencode($text)); + } +} diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Web/UrlEncodeTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Web/UrlEncodeTest.php new file mode 100644 index 00000000..3ce6098d --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Web/UrlEncodeTest.php @@ -0,0 +1,26 @@ +