DynamicPageList3/includes/Variables.php

137 lines
3 KiB
PHP
Raw Normal View History

2020-11-22 20:00:48 +00:00
<?php
/**
* DynamicPageList3
* DPL Variables Class
*
* @author IlyaHaykinson, Unendlich, Dangerville, Algorithmix, Theaitetos, Alexia E. Smith
* @license GPL-2.0-or-later
* @package DynamicPageList3
*
2021-02-22 23:48:01 +00:00
*/
2020-11-22 20:00:48 +00:00
namespace DPL;
class Variables {
/**
* Memory storage for variables.
*
2021-02-22 23:48:01 +00:00
* @var array
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
public static $memoryVar = [];
2020-11-22 20:00:48 +00:00
/**
* Memory storage for arrays of variables.
*
2021-02-22 23:48:01 +00:00
* @var array
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
public static $memoryArray = [];
2020-11-22 20:00:48 +00:00
// expects pairs of 'variable name' and 'value'
// if the first parameter is empty it will be ignored {{#vardefine:|a|b}} is the same as {{#vardefine:a|b}}
2021-02-22 23:48:01 +00:00
public static function setVar( $arg ) {
$numargs = count( $arg );
if ( $numargs >= 3 && $arg[2] == '' ) {
2020-11-22 20:00:48 +00:00
$start = 3;
} else {
$start = 2;
}
2021-02-22 23:48:01 +00:00
for ( $i = $start; $i < $numargs; $i++ ) {
2020-11-22 20:00:48 +00:00
$var = $arg[$i];
2021-02-22 23:48:01 +00:00
if ( ++$i <= $numargs - 1 ) {
2020-11-22 20:00:48 +00:00
self::$memoryVar[$var] = $arg[$i];
} else {
self::$memoryVar[$var] = '';
}
}
return '';
}
2021-02-22 23:48:01 +00:00
public static function setVarDefault( $arg ) {
$numargs = count( $arg );
if ( $numargs > 3 ) {
2020-11-22 20:00:48 +00:00
$value = $arg[3];
} else {
return '';
}
$var = $arg[2];
2021-02-22 23:48:01 +00:00
if ( !array_key_exists( $var, self::$memoryVar ) || self::$memoryVar[$var] == '' ) {
2020-11-22 20:00:48 +00:00
self::$memoryVar[$var] = $value;
}
return '';
}
2021-02-22 23:48:01 +00:00
public static function getVar( $var ) {
if ( array_key_exists( $var, self::$memoryVar ) ) {
2020-11-22 20:00:48 +00:00
return self::$memoryVar[$var];
}
return '';
}
2021-02-22 23:48:01 +00:00
public static function setArray( $arg ) {
$numargs = count( $arg );
if ( $numargs < 5 ) {
2020-11-22 20:00:48 +00:00
return '';
}
2021-02-22 23:48:01 +00:00
$var = trim( $arg[2] );
2020-11-22 20:00:48 +00:00
$value = $arg[3];
$delimiter = $arg[4];
2021-02-22 23:48:01 +00:00
if ( $var == '' ) {
2020-11-22 20:00:48 +00:00
return '';
}
2021-02-22 23:48:01 +00:00
if ( $value == '' ) {
2020-11-22 20:00:48 +00:00
self::$memoryArray[$var] = [];
return;
}
2021-02-22 23:48:01 +00:00
if ( $delimiter == '' ) {
2020-11-22 20:00:48 +00:00
self::$memoryArray[$var] = [
$value
];
return;
}
2021-02-22 23:48:01 +00:00
if ( 0 !== strpos( $delimiter, '/' ) || ( strlen( $delimiter ) - 1 ) !== strrpos( $delimiter, '/' ) ) {
2020-11-22 20:00:48 +00:00
$delimiter = '/\s*' . $delimiter . '\s*/';
}
2021-02-22 23:48:01 +00:00
self::$memoryArray[$var] = preg_split( $delimiter, $value );
return "value={$value}, delimiter={$delimiter}," . count( self::$memoryArray[$var] );
2020-11-22 20:00:48 +00:00
}
2021-02-22 23:48:01 +00:00
public static function dumpArray( $arg ) {
$numargs = count( $arg );
if ( $numargs < 3 ) {
2020-11-22 20:00:48 +00:00
return '';
}
2021-02-22 23:48:01 +00:00
$var = trim( $arg[2] );
2020-11-22 20:00:48 +00:00
$text = " array {$var} = {";
$n = 0;
2021-02-22 23:48:01 +00:00
if ( array_key_exists( $var, self::$memoryArray ) ) {
foreach ( self::$memoryArray[$var] as $value ) {
if ( $n++ > 0 ) {
2020-11-22 20:00:48 +00:00
$text .= ', ';
}
$text .= "{$value}";
}
}
return $text . "}\n";
}
2021-02-22 23:48:01 +00:00
public static function printArray( $var, $delimiter, $search, $subject ) {
$var = trim( $var );
if ( $var == '' ) {
2020-11-22 20:00:48 +00:00
return '';
}
2021-02-22 23:48:01 +00:00
if ( !array_key_exists( $var, self::$memoryArray ) ) {
2020-11-22 20:00:48 +00:00
return '';
}
$values = self::$memoryArray[$var];
$rendered_values = [];
2021-02-22 23:48:01 +00:00
foreach ( $values as $v ) {
$temp_result_value = str_replace( $search, $v, $subject );
2020-11-22 20:00:48 +00:00
$rendered_values[] = $temp_result_value;
}
return [
2021-02-22 23:48:01 +00:00
implode( $delimiter, $rendered_values ),
2020-11-22 20:00:48 +00:00
'noparse' => false,
'isHTML' => false
];
}
}