1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Config\Settings;
use function is_array;
// phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
/**
* @psalm-immutable
*/
final class Transformations
{
/**
* Displays a part of a string.
* - The first option is the number of characters to skip from the beginning of the string (Default 0).
* - The second option is the number of characters to return (Default: until end of string).
* - The third option is the string to append and/or prepend when truncation occurs (Default: "…").
*
* @var array<int, int|string>
* @psalm-var array{0: int, 1: 'all'|int, 2: string}
*/
public $Substring;
/**
* Converts Boolean values to text (default 'T' and 'F').
* - First option is for TRUE, second for FALSE. Nonzero=true.
*
* @var string[]
* @psalm-var array{0: string, 1: string}
*/
public $Bool2Text;
/**
* LINUX ONLY: Launches an external application and feeds it the column data via standard input.
* Returns the standard output of the application. The default is Tidy, to pretty-print HTML code.
* For security reasons, you have to manually edit the file
* libraries/classes/Plugins/Transformations/Abs/ExternalTransformationsPlugin.php and list the tools
* you want to make available.
* - The first option is then the number of the program you want to use.
* - The second option should be blank for historical reasons.
* - The third option, if set to 1, will convert the output using htmlspecialchars() (Default 1).
* - The fourth option, if set to 1, will prevent wrapping and ensure that the output appears
* all on one line (Default 1).
*
* @var array<int, int|string>
* @psalm-var array{0: int, 1: string, 2: int, 3: int}
*/
public $External;
/**
* Prepends and/or Appends text to a string.
* - First option is text to be prepended. second is appended (enclosed in single quotes, default empty string).
*
* @var string[]
* @psalm-var array{0: string, 1: string}
*/
public $PreApPend;
/**
* Displays hexadecimal representation of data.
* Optional first parameter specifies how often space will be added (defaults to 2 nibbles).
*
* @var string[]
* @psalm-var array{0: 0|positive-int}
*/
public $Hex;
/**
* Displays a TIME, TIMESTAMP, DATETIME or numeric unix timestamp column as formatted date.
* - The first option is the offset (in hours) which will be added to the timestamp (Default: 0).
* - Use second option to specify a different date/time format string.
* - Third option determines whether you want to see local date or UTC one (use "local" or "utc" strings) for that.
* According to that, date format has different value - for "local" see the documentation
* for PHP's strftime() function and for "utc" it is done using gmdate() function.
*
* @var array<int, int|string>
* @psalm-var array{0: 0|positive-int, 1: string, 2: 'local'|'utc'}
*/
public $DateFormat;
/**
* Displays a clickable thumbnail.
* The options are the maximum width and height in pixels.
* The original aspect ratio is preserved.
*
* @var array<(int|string), (int|string|array<string, string>|null)>
* @psalm-var array{
* 0: 0|positive-int,
* 1: 0|positive-int,
* wrapper_link: string|null,
* wrapper_params: array<array-key, string>
* }
*/
public $Inline;
/**
* Displays an image and a link; the column contains the filename.
* - The first option is a URL prefix like "https://www.example.com/".
* - The second and third options are the width and the height in pixels.
*
* @var array<int, int|string|null>
* @psalm-var array{0: string|null, 1: 0|positive-int, 2: 0|positive-int}
*/
public $TextImageLink;
/**
* Displays a link; the column contains the filename.
* - The first option is a URL prefix like "https://www.example.com/".
* - The second option is a title for the link.
*
* @var array<int, string|null>
* @psalm-var array{0: string|null, 1: string|null, 2: bool|null}
*/
public $TextLink;
/**
* @param array<int|string, mixed> $transformations
*/
public function __construct(array $transformations = [])
{
$this->Substring = $this->setSubstring($transformations);
$this->Bool2Text = $this->setBool2Text($transformations);
$this->External = $this->setExternal($transformations);
$this->PreApPend = $this->setPreApPend($transformations);
$this->Hex = $this->setHex($transformations);
$this->DateFormat = $this->setDateFormat($transformations);
$this->Inline = $this->setInline($transformations);
$this->TextImageLink = $this->setTextImageLink($transformations);
$this->TextLink = $this->setTextLink($transformations);
}
/**
* @param array<int|string, mixed> $transformations
*
* @return array<int, int|string>
* @psalm-return array{0: int, 1: 'all'|int, 2: string}
*/
private function setSubstring(array $transformations): array
{
$substring = [0, 'all', '…'];
if (isset($transformations['Substring']) && is_array($transformations['Substring'])) {
if (isset($transformations['Substring'][0])) {
$substring[0] = (int) $transformations['Substring'][0];
}
if (isset($transformations['Substring'][1]) && $transformations['Substring'][1] !== 'all') {
$substring[1] = (int) $transformations['Substring'][1];
}
if (isset($transformations['Substring'][2])) {
$substring[2] = (string) $transformations['Substring'][2];
}
}
return $substring;
}
/**
* @param array<int|string, mixed> $transformations
*
* @return string[]
* @psalm-return array{0: string, 1: string}
*/
private function setBool2Text(array $transformations): array
{
$bool2Text = ['T', 'F'];
if (isset($transformations['Bool2Text']) && is_array($transformations['Bool2Text'])) {
if (isset($transformations['Bool2Text'][0])) {
$bool2Text[0] = (string) $transformations['Bool2Text'][0];
}
if (isset($transformations['Bool2Text'][1])) {
$bool2Text[1] = (string) $transformations['Bool2Text'][1];
}
}
return $bool2Text;
}
/**
* @param array<int|string, mixed> $transformations
*
* @return array<int, int|string>
* @psalm-return array{0: int, 1: string, 2: int, 3: int}
*/
private function setExternal(array $transformations): array
{
$external = [0, '-f /dev/null -i -wrap -q', 1, 1];
if (isset($transformations['External']) && is_array($transformations['External'])) {
if (isset($transformations['External'][0])) {
$external[0] = (int) $transformations['External'][0];
}
if (isset($transformations['External'][1])) {
$external[1] = (string) $transformations['External'][1];
}
if (isset($transformations['External'][2])) {
$external[2] = (int) $transformations['External'][2];
}
if (isset($transformations['External'][3])) {
$external[3] = (int) $transformations['External'][3];
}
}
return $external;
}
/**
* @param array<int|string, mixed> $transformations
*
* @return string[]
* @psalm-return array{0: string, 1: string}
*/
private function setPreApPend(array $transformations): array
{
$preApPend = ['', ''];
if (isset($transformations['PreApPend']) && is_array($transformations['PreApPend'])) {
if (isset($transformations['PreApPend'][0])) {
$preApPend[0] = (string) $transformations['PreApPend'][0];
}
if (isset($transformations['PreApPend'][1])) {
$preApPend[1] = (string) $transformations['PreApPend'][1];
}
}
return $preApPend;
}
/**
* @param array<int|string, mixed> $transformations
*
* @return string[]
* @psalm-return array{0: 0|positive-int}
*/
private function setHex(array $transformations): array
{
if (isset($transformations['Hex']) && is_array($transformations['Hex'])) {
if (isset($transformations['Hex'][0])) {
$length = (int) $transformations['Hex'][0];
if ($length >= 0) {
return [$length];
}
}
}
return [2];
}
/**
* @param array<int|string, mixed> $transformations
*
* @return array<int, int|string>
* @psalm-return array{0: 0|positive-int, 1: string, 2: 'local'|'utc'}
*/
private function setDateFormat(array $transformations): array
{
$dateFormat = [0, '', 'local'];
if (isset($transformations['DateFormat']) && is_array($transformations['DateFormat'])) {
if (isset($transformations['DateFormat'][0])) {
$offset = (int) $transformations['DateFormat'][0];
if ($offset >= 1) {
$dateFormat[0] = $offset;
}
}
if (isset($transformations['DateFormat'][1])) {
$dateFormat[1] = (string) $transformations['DateFormat'][1];
}
if (isset($transformations['DateFormat'][2]) && $transformations['DateFormat'][2] === 'utc') {
$dateFormat[2] = 'utc';
}
}
return $dateFormat;
}
/**
* @param array<int|string, mixed> $transformations
*
* @return array<(int|string), (int|string|array<string, string>|null)>
* @psalm-return array{
* 0: 0|positive-int,
* 1: 0|positive-int,
* wrapper_link: string|null,
* wrapper_params: array<array-key, string>
* }
*/
private function setInline(array $transformations): array
{
$inline = [100, 100, 'wrapper_link' => null, 'wrapper_params' => []];
if (isset($transformations['Inline']) && is_array($transformations['Inline'])) {
if (isset($transformations['Inline'][0])) {
$width = (int) $transformations['Inline'][0];
if ($width >= 0) {
$inline[0] = $width;
}
}
if (isset($transformations['Inline'][1])) {
$height = (int) $transformations['Inline'][1];
if ($height >= 0) {
$inline[1] = $height;
}
}
if (isset($transformations['Inline']['wrapper_link'])) {
$inline['wrapper_link'] = (string) $transformations['Inline']['wrapper_link'];
}
if (
isset($transformations['Inline']['wrapper_params'])
&& is_array($transformations['Inline']['wrapper_params'])
) {
/**
* @var int|string $key
* @var mixed $value
*/
foreach ($transformations['Inline']['wrapper_params'] as $key => $value) {
$inline['wrapper_params'][$key] = (string) $value;
}
}
}
return $inline;
}
/**
* @param array<int|string, mixed> $transformations
*
* @return array<int, int|string|null>
* @psalm-return array{0: string|null, 1: 0|positive-int, 2: 0|positive-int}
*/
private function setTextImageLink(array $transformations): array
{
$textImageLink = [null, 100, 50];
if (isset($transformations['TextImageLink']) && is_array($transformations['TextImageLink'])) {
if (isset($transformations['TextImageLink'][0])) {
$textImageLink[0] = (string) $transformations['TextImageLink'][0];
}
if (isset($transformations['TextImageLink'][1])) {
$width = (int) $transformations['TextImageLink'][1];
if ($width >= 0) {
$textImageLink[1] = $width;
}
}
if (isset($transformations['TextImageLink'][2])) {
$height = (int) $transformations['TextImageLink'][2];
if ($height >= 0) {
$textImageLink[2] = $height;
}
}
}
return $textImageLink;
}
/**
* @param array<int|string, mixed> $transformations
*
* @return array<int, string|null>
* @psalm-return array{0: string|null, 1: string|null, 2: bool|null}
*/
private function setTextLink(array $transformations): array
{
$textLink = [null, null, null];
if (isset($transformations['TextLink']) && is_array($transformations['TextLink'])) {
if (isset($transformations['TextLink'][0])) {
$textLink[0] = (string) $transformations['TextLink'][0];
}
if (isset($transformations['TextLink'][1])) {
$textLink[1] = (string) $transformations['TextLink'][1];
}
if (isset($transformations['TextLink'][2])) {
$textLink[2] = (bool) $transformations['TextLink'][2];
}
}
return $textLink;
}
}
|