U
in package
Some really small, simple, and self-contained utility public static functions
Table of Contents
Constants
- FLASH_ERROR : mixed = 'error'
- Session key for error flash messages (must match Output::FLASH_ERROR)
- FLASH_SUCCESS : mixed = 'success'
- Session key for success flash messages (must match Output::FLASH_SUCCESS)
Methods
- absolute_url() : mixed
- absolute_url_ref() : mixed
- add_url_parm() : mixed
- addSession() : mixed
- allow_track() : mixed
- Indicate if the user has not requested "Do not Track"
- apache_request_headers() : mixed
- apcAvailable() : mixed
- Return if APC cache is available (Deprecated)
- apcuAvailable() : mixed
- Return if APCU cache is available
- appCacheDelete() : mixed
- appCacheGet() : mixed
- appCacheSet() : mixed
- array_Integer_Deserialize() : mixed
- Deserialize an tightly serialized integer-only PHP array
- array_Integer_Serialize() : mixed
- Tightly serialize an integer-only PHP array
- array_Integer_Serialize_Map() : mixed
- array_kshift() : mixed
- Pull off the first element of a key/value array
- conservativeUrl() : mixed
- conversion_time() : mixed
- Give the current time in the "conversion format"
- createGUID() : mixed
- Create a unique GUID return string
- curPHPUrl() : mixed
- Return the URL as seen by PHP (no query string or parameters)
- die_with_error_log() : mixed
- displaySize() : mixed
- echo_log() : mixed
- endsWith() : mixed
- error_out() : mixed
- escapeHtml() : mixed
- Make an htmlentities() that is more tolerant than the PHP 8.2 version.
- flashError() : void
- Set an error flash message. Use when $OUTPUT is not available (e.g. library code).
- flashSuccess() : void
- Set a success flash message. Use when $OUTPUT is not available (e.g. library code).
- get() : mixed
- Produce a Python-style get() to avoid use of ternary operator
- get_base_url() : mixed
- Get the protocol, host, and port from an absolute URL
- get_request_document() : mixed
- Get the last bit of the path
- get_rest_parent() : mixed
- Get the path to one above the current request, w/o trailing slash
- get_rest_path() : mixed
- Get the path to the current request, w/o trailing slash
- getCaller() : mixed
- Get caller outside this file as string
- getIgnoreCase() : mixed
- Like get(), but match array keys without regard to case.
- getServerBase() : mixed
- goodFolder() : mixed
- htmlent_utf8() : mixed
- htmlpre_utf8() : mixed
- htmlspec_utf8() : mixed
- http_response_code() : mixed
- isCli() : mixed
- isEmpty() : bool
- Determine if a variable is empty
- isGUIDValid() : mixed
- isKeyNotEmpty() : bool
- Check if a key in an array is not empty
- isNotEmpty() : bool
- Imitate StringUtils.isNotEmpty()
- iso8601() : mixed
- isValidCSSColor() : mixed
- Validate a CSS color value
- json_decode() : object
- Build an "always works" json_decode()
- line_out() : mixed
- lmsDie() : mixed
- lti_sha256() : mixed
- parse_rest_path() : mixed
- Get the controller for the current request
- print_stack_trace() : mixed
- reconstruct_query() : mixed
- remove_relative_path() : mixed
- Remove any relative elements from a path
- rest_path() : mixed
- Return a rest-path
- route_get_local_path() : mixed
- safe_array() : mixed
- safe_href() : mixed
- safe_var_dump() : mixed
- session_empty() : mixed
- Empty the session (clear all session data).
- session_safe_id() : mixed
- startsWith() : mixed
- strlen() : int
- Build a PHP 7 strlen for PHP 8.2 and later
- success_out() : mixed
- youtubeWatchUrl() : mixed
- Build a YouTube watch URL, appending playlist when youtube_playlist is configured.
Constants
FLASH_ERROR
Session key for error flash messages (must match Output::FLASH_ERROR)
public
mixed
FLASH_ERROR
= 'error'
FLASH_SUCCESS
Session key for success flash messages (must match Output::FLASH_SUCCESS)
public
mixed
FLASH_SUCCESS
= 'success'
Methods
absolute_url()
public
static absolute_url(mixed $url) : mixed
Parameters
- $url : mixed
absolute_url_ref()
public
static absolute_url_ref(mixed &$url) : mixed
Parameters
- $url : mixed
add_url_parm()
public
static add_url_parm(mixed $url, mixed $key, mixed $val) : mixed
Parameters
- $url : mixed
- $key : mixed
- $val : mixed
addSession()
public
static addSession(mixed $url[, mixed $force = false ]) : mixed
Parameters
- $url : mixed
- $force : mixed = false
allow_track()
Indicate if the user has not requested "Do not Track"
public
static allow_track() : mixed
This is just the inverted value for the "Do not Track". Using "allow" sematics makes writing code easier!
http://donottrack.us/
apache_request_headers()
public
static apache_request_headers() : mixed
apcAvailable()
Return if APC cache is available (Deprecated)
public
static apcAvailable() : mixed
apcuAvailable()
Return if APCU cache is available
public
static apcuAvailable() : mixed
appCacheDelete()
public
static appCacheDelete(mixed $key) : mixed
Parameters
- $key : mixed
appCacheGet()
public
static appCacheGet(mixed $key[, mixed $default = null ]) : mixed
Parameters
- $key : mixed
- $default : mixed = null
appCacheSet()
public
static appCacheSet(mixed $key, mixed $value[, mixed $ttl = 0 ]) : mixed
Parameters
- $key : mixed
- $value : mixed
- $ttl : mixed = 0
array_Integer_Deserialize()
Deserialize an tightly serialized integer-only PHP array
public
static array_Integer_Deserialize(mixed $input) : mixed
$str = '1=42,2=43,3=44'; $arar = U::array_Integer_Deserialize($str); print_r($arar); // Array ( '1' => 42 ,'2' => 43, '3' => 44 );
https://stackoverflow.com/questions/4923951/php-split-string-in-key-value-pairs
Parameters
- $input : mixed
array_Integer_Serialize()
Tightly serialize an integer-only PHP array
public
static array_Integer_Serialize(mixed $arar) : mixed
$arar = Array ( 1 => 42 ,2 => 43, 3 => 44 ); $str = U::array_Integer_Serialize($arar); echo($str); // 1=42,2=43,3=44
https://stackoverflow.com/questions/30231476/i-want-to-array-key-and-array-value-comma-separated-string
Parameters
- $arar : mixed
array_Integer_Serialize_Map()
public
static array_Integer_Serialize_Map(mixed $a, mixed $b) : mixed
Parameters
- $a : mixed
- $b : mixed
array_kshift()
Pull off the first element of a key/value array
public
static array_kshift(mixed &$arr) : mixed
$arr = array('x'=>'ball','y'=>'hat','z'=>'apple'); print_r($arr); print_r(array_kshift($arr)); // [x] => ball print_r($arr);
http://php.net/manual/en/function.array-shift.php#84179
Parameters
- $arr : mixed
conservativeUrl()
public
static conservativeUrl(mixed $url) : mixed
Parameters
- $url : mixed
conversion_time()
Give the current time in the "conversion format"
public
static conversion_time([mixed $time = "now" ]) : mixed
201711261315
Parameters
- $time : mixed = "now"
createGUID()
Create a unique GUID return string
public
static createGUID() : mixed
https://www.texelate.co.uk/blog/create-a-guid-with-php
curPHPUrl()
Return the URL as seen by PHP (no query string or parameters)
public
static curPHPUrl() : mixed
Borrowed from from_request on OAuthRequest.php
die_with_error_log()
public
static die_with_error_log(mixed $msg[, mixed $extra = false ][, mixed $prefix = "DIE:" ]) : mixed
Parameters
- $msg : mixed
- $extra : mixed = false
- $prefix : mixed = "DIE:"
displaySize()
public
static displaySize(mixed $size) : mixed
Parameters
- $size : mixed
echo_log()
public
static echo_log(mixed $msg) : mixed
Parameters
- $msg : mixed
endsWith()
public
static endsWith(mixed $haystack, mixed $needle) : mixed
Parameters
- $haystack : mixed
- $needle : mixed
error_out()
public
static error_out(mixed $output) : mixed
Parameters
- $output : mixed
escapeHtml()
Make an htmlentities() that is more tolerant than the PHP 8.2 version.
public
static escapeHtml(mixed $string) : mixed
https://commons.apache.org/proper/commons-text/apidocs/org/apache/commons/text/StringEscapeUtils.html
Parameters
- $string : mixed
flashError()
Set an error flash message. Use when $OUTPUT is not available (e.g. library code).
public
static flashError(string $message) : void
Displayed by Output::flashMessages() on next render.
Parameters
- $message : string
flashSuccess()
Set a success flash message. Use when $OUTPUT is not available (e.g. library code).
public
static flashSuccess(string $message) : void
Displayed by Output::flashMessages() on next render.
Parameters
- $message : string
get()
Produce a Python-style get() to avoid use of ternary operator
public
static get(mixed $arr, mixed $key[, mixed $default = null ]) : mixed
Parameters
- $arr : mixed
- $key : mixed
- $default : mixed = null
get_base_url()
Get the protocol, host, and port from an absolute URL
public
static get_base_url(mixed $url) : mixed
input: http://localhost:8888/tsugi output: http://localhost:8888
Parameters
- $url : mixed
get_request_document()
Get the last bit of the path
public
static get_request_document() : mixed
input: /py4e/lessons/intro?x=2 output: intro
get_rest_parent()
Get the path to one above the current request, w/o trailing slash
public
static get_rest_parent([mixed $uri = false ]) : mixed
input: /py4e/lessons/intro?x=2 output: /py4e/lessons
input: /py4e/lessons/intro/?x=2 output: /py4e/lessons/intro
Parameters
- $uri : mixed = false
get_rest_path()
Get the path to the current request, w/o trailing slash
public
static get_rest_path([mixed $uri = false ]) : mixed
input: /py4e/lessons/intro?x=2 output: /py4e/lessons/intro
input: /py4e/lessons/intro/?x=2 output: /py4e/lessons/intro
Parameters
- $uri : mixed = false
getCaller()
Get caller outside this file as string
public
static getCaller([mixed $count = 1 ]) : mixed
error_log("Called by: ".U::getCaller()); error_log("Called by: ".U::getCaller(2));
Called by: /Applications/MAMP/htdocs/py4e/tsugi/vendor/tsugi/lib/src/Core/LTIX.php:1766 /Applications/MAMP/htdocs/py4e/tools/pythonauto/sendgrade.php:18
Parameters
- $count : mixed = 1
getIgnoreCase()
Like get(), but match array keys without regard to case.
public
static getIgnoreCase(mixed $arr, mixed $key[, mixed $default = null ]) : mixed
HTTP header names are case-insensitive (RFC 7230). HTTP/2 requires names to be lowercase, so LMS responses (e.g. Canvas) may use 'link' instead of 'Link'. Exact match is preferred when present.
Parameters
- $arr : mixed
- $key : mixed
- $default : mixed = null
getServerBase()
public
static getServerBase(mixed $url) : mixed
Parameters
- $url : mixed
goodFolder()
public
static goodFolder(mixed $folder) : mixed
Parameters
- $folder : mixed
htmlent_utf8()
public
static htmlent_utf8(mixed $string) : mixed
Parameters
- $string : mixed
htmlpre_utf8()
public
static htmlpre_utf8(mixed $string) : mixed
Parameters
- $string : mixed
htmlspec_utf8()
public
static htmlspec_utf8(mixed $string) : mixed
Parameters
- $string : mixed
http_response_code()
public
static http_response_code([mixed $newcode = null ]) : mixed
Parameters
- $newcode : mixed = null
isCli()
public
static isCli() : mixed
isEmpty()
Determine if a variable is empty
public
static isEmpty(mixed $string) : bool
This is needed because in PHP 8.2 strlen() demands a string (i.e. can't handle false, etc) Sadly, empty('0') becomes falsey and so returns true - What??
The method name is taken from Java since its rules are corect.
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#isEmpty--
Parameters
- $string : mixed
Return values
boolisGUIDValid()
public
static isGUIDValid(mixed $guid) : mixed
Parameters
- $guid : mixed
isKeyNotEmpty()
Check if a key in an array is not empty
public
static isKeyNotEmpty(array<string|int, mixed> $collection, string $key) : bool
Parameters
- $collection : array<string|int, mixed>
- $key : string
Return values
boolisNotEmpty()
Imitate StringUtils.isNotEmpty()
public
static isNotEmpty(mixed $string) : bool
Sigh, Java 8.0 does not have isNotEmpty - which is super useful so we imitate
https://commons.apache.org/proper/commons-lang/javadocs/api-3.3/org/apache/commons/lang3/StringUtils.html
It is the little things - sigh.
Parameters
- $string : mixed
Return values
booliso8601()
public
static iso8601([mixed $time = "now" ]) : mixed
Parameters
- $time : mixed = "now"
isValidCSSColor()
Validate a CSS color value
public
static isValidCSSColor(mixed $color) : mixed
Parameters
- $color : mixed
json_decode()
Build an "always works" json_decode()
public
static json_decode(mixed $string) : object
This is needed because in PHP 8.2 json_decode() demands a string (i.e. can't handle false, etc)
Parameters
- $string : mixed
Return values
objectline_out()
public
static line_out(mixed $output) : mixed
Parameters
- $output : mixed
lmsDie()
public
static lmsDie([mixed $message = false ]) : mixed
Parameters
- $message : mixed = false
lti_sha256()
public
static lti_sha256(mixed $val) : mixed
Parameters
- $val : mixed
parse_rest_path()
Get the controller for the current request
public
static parse_rest_path([mixed $uri = false ][, mixed $SERVER_SCRIPT_NAME = false ]) : mixed
executing script: /py4e/koseu.php input: /py4e/lessons/intro?x=2 output: (/py4e, lessons, intro)
input: /py4e/lessons/intro/?x=2 output: /py4e/lessons/intro
Parameters
- $uri : mixed = false
- $SERVER_SCRIPT_NAME : mixed = false
print_stack_trace()
public
static print_stack_trace() : mixed
reconstruct_query()
public
static reconstruct_query(mixed $baseurl[, mixed $newparms = false ]) : mixed
Parameters
- $baseurl : mixed
- $newparms : mixed = false
remove_relative_path()
Remove any relative elements from a path
public
static remove_relative_path(mixed $path) : mixed
Before After a/b/c a/b/c a/b/c/ a/b/c/ a/./c/ a/c/ a/../c/ c/
Parameters
- $path : mixed
rest_path()
Return a rest-path
public
static rest_path([mixed $uri = false ][, mixed $SERVER_SCRIPT_NAME = false ]) : mixed
This knows the current working folder we are in. The model is that a folder is an "application" with many possible controllers and within each controller there are actions followed by parameters.
for example, assume these files exist in a folder:
../tsugi/mod/zap/index.php
../tsugi/mod/zap/rows.php
../tsugi/mod/zap/store.php
If this URL were called with .htaccess and tsugi.php set up, rows.php would be run and this would be the output:
http://localhost:8888/tsugi/mod/zap/rows/add/1/2
base_url: http://localhost:8888
parent: /tsugi/mod/zap
current: /tsugi/mod/zap/rows
controller: rows
action: add
parameters: (1, 2)
full: /tsugi/mod/zap/rows/add/1/2
As a special case, when the index.php runs (not through tsugi.php) The results are as follows:
http://localhost:8888/tsugi/mod/zap/
base_url: http://localhost:8888
parent: /tsugi/mod/zap
current: /tsugi/mod/zap
controller: empty string
action: empty string
parameters: empty array
full: /tsugi/mod/zap
Parameters
- $uri : mixed = false
- $SERVER_SCRIPT_NAME : mixed = false
route_get_local_path()
public
static route_get_local_path(mixed $dir) : mixed
Parameters
- $dir : mixed
safe_array()
public
static safe_array(mixed $inp) : mixed
Parameters
- $inp : mixed
safe_href()
public
static safe_href(mixed $string) : mixed
Parameters
- $string : mixed
safe_var_dump()
public
static safe_var_dump(mixed $x) : mixed
Parameters
- $x : mixed
session_empty()
Empty the session (clear all session data).
public
static session_empty() : mixed
Use when you need to reset the session - easy to forget, so it lives here.
session_safe_id()
public
static session_safe_id() : mixed
startsWith()
public
static startsWith(mixed $haystack, mixed $needle) : mixed
Parameters
- $haystack : mixed
- $needle : mixed
strlen()
Build a PHP 7 strlen for PHP 8.2 and later
public
static strlen(mixed $string) : int
This is needed because in PHP 8.2 strlen() demands a string (i.e. can't handle false, etc)
Parameters
- $string : mixed
Return values
intsuccess_out()
public
static success_out(mixed $output) : mixed
Parameters
- $output : mixed
youtubeWatchUrl()
Build a YouTube watch URL, appending playlist when youtube_playlist is configured.
public
static youtubeWatchUrl(mixed $videoId) : mixed
Parameters
- $videoId : mixed