Initial implementation of the URLENCODE() web function (#2031)

* Initial implementation of the URLENCODE() web function
This commit is contained in:
Mark Baker 2021-04-28 17:10:36 +02:00 committed by GitHub
parent 451cb44613
commit 475874bed3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 146 additions and 47 deletions

View File

@ -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' => [

View File

@ -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

View File

@ -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);
}
}

View File

@ -0,0 +1,75 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\Web;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Settings;
use Psr\Http\Client\ClientExceptionInterface;
class Service
{
/**
* WEBSERVICE.
*
* Returns data from a web service on the Internet or Intranet.
*
* Excel Function:
* Webservice(url)
*
* @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;
}
/**
* 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));
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Web;
use PhpOffice\PhpSpreadsheet\Calculation\Web\Service;
use PHPUnit\Framework\TestCase;
class UrlEncodeTest extends TestCase
{
/**
* @dataProvider providerURLENCODE
*
* @param string $expectedResult
* @param mixed $text
*/
public function testURLENCODE($expectedResult, $text): void
{
$result = Service::urlEncode($text);
self::assertSame($expectedResult, $result);
}
public function providerURLENCODE(): array
{
return require 'tests/data/Calculation/Web/URLENCODE.php';
}
}

View File

@ -0,0 +1,12 @@
<?php
return [
[
'http%3A%2F%2Fcontoso.sharepoint.com%2Fteams%2FFinance%2FDocuments%2FApril%20Reports%2FProfit%20and%20Loss%20Statement.xlsx',
'http://contoso.sharepoint.com/teams/Finance/Documents/April Reports/Profit and Loss Statement.xlsx',
],
[
'#VALUE!',
123,
],
];