PrivateBin/lib/Proxy/ShlinkProxy.php
Matt Van Horn c5c910a537
Switch to short array syntax (#1640)
Mass-converts all array() literals to [] across lib/, tpl/, tst/ as
discussed in #1640. PHP supports short array syntax since 5.4 and the
project's required PHP version is 7.4+, so this is a pure stylistic
change with no behavior impact.

Also flips the StyleCI configuration so future PRs are checked against
short_array_syntax instead of long_array_syntax. Per @elrido in #1640,
the StyleCI swap and the mass change need to land together so no
branch breaks under the wrong rule.

Generated with `php-cs-fixer fix --rules=array_syntax` against the
.styleci.yml finder paths. 39 files touched, 487 insertions / 487
deletions (every change is a single-token replacement). Verified:

- `php -l` clean on all touched files
- `phpunit` from `tst/`: 230 / 230 tests pass (with 6329 assertions),
  excluding `ControllerWithGcsTest` which fails on PHP 8.5 because of
  an unrelated upstream `google/cloud-core` deprecation. The same 14
  GCS-suite errors reproduce on `master` before this change.

Closes #1640
2026-05-03 05:23:11 -07:00

76 lines
1.9 KiB
PHP

<?php declare(strict_types=1);
/**
* PrivateBin
*
* a zero-knowledge paste bin
*
* @link https://github.com/PrivateBin/PrivateBin
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
*/
namespace PrivateBin\Proxy;
use PrivateBin\Configuration;
use PrivateBin\Exception\JsonException;
use PrivateBin\Json;
/**
* ShlinkProxy
*
* Forwards a URL for shortening to shlink and stores the result.
*/
class ShlinkProxy extends AbstractProxy
{
/**
* Overrides the abstract parent function to get the proxy URL.
*
* @param Configuration $conf
* @return string
*/
protected function _getProxyUrl(Configuration $conf): string
{
return $conf->getKey('apiurl', 'shlink');
}
/**
* Overrides the abstract parent function to get contents from Shlink API.
*
* @access protected
* @param Configuration $conf
* @param string $link
* @return array
*/
protected function _getProxyPayload(Configuration $conf, string $link): array
{
$shlink_api_key = $conf->getKey('apikey', 'shlink');
$body = [
'longUrl' => $link,
];
try {
return [
'method' => 'POST',
'header' => "Content-Type: application/json\r\n" .
'X-Api-Key: ' . $shlink_api_key . "\r\n",
'content' => Json::encode($body),
];
} catch (JsonException $e) {
error_log('[' . get_class($this) . '] Error encoding body: ' . $e->getMessage());
return [];
}
}
/**
* Extracts the short URL from the shlink API response.
*
* @access protected
* @param array $data
* @return ?string
*/
protected function _extractShortUrl(array $data): ?string
{
return $data['shortUrl'] ?? null;
}
}