libSmalldb  v0.7-66-gb94d22a
Static Public Member Functions | List of all members
Utils Class Reference

Description

Utilities.

Small useful functions.

Todo:
Refactor this to standalone classes or libraries.

Static Public Member Functions

static template_format ($template, $values, $escaping_function='htmlspecialchars')
 Format data using template string with placeholders. More...
 
static filename_format ($template, $values)
 Helper method to invoke template_format() without HTML escaping. More...
 
static write_json_file ($filename, $json_array, array $whitelist=null, $json_options=null)
 Encode array to JSON using json_encode, but insert PHP snippet to protect sensitive data. More...
 
static parse_json_file ($filename)
 JSON version of parse_ini_file(). More...
 
static parse_json_string ($json_str, $filename=null)
 Decode JSON string. More...
 

Member Function Documentation

◆ template_format()

static template_format (   $template,
  $values,
  $escaping_function = 'htmlspecialchars' 
)
static

Format data using template string with placeholders.

The placeholders to replace are in the following format: {name} or {name:function} or {name:function:format_string}.

The function is formatting function (like sprintf) and format string is the first parameter of the formatting function.

Available formatting functions:

  • sprintf
  • strftime
  • floor
  • ceil
  • frac
  • frac_str
  • intval
  • floatval

All except strftime accept same formatting string as sprintf. The strftime accepts strftime format string.

Example of the template:
Hello {user}, now is {time:strftime:%H} o'clock and the
temperature is {temp:sprintf:%0.2f} degree Celsius.

◆ filename_format()

static filename_format (   $template,
  $values 
)
static

Helper method to invoke template_format() without HTML escaping.

◆ write_json_file()

static write_json_file (   $filename,
  $json_array,
array  $whitelist = null,
  $json_options = null 
)
static

Encode array to JSON using json_encode, but insert PHP snippet to protect sensitive data.

If $filename is set, JSON will be written to given file. Otherwise you are expected to store returned string into *.json.php file.

Stop snippet: When JSON file is evaluated as PHP, stop snippet will interrupt evaluation without breaking JSON syntax, only underscore key is appended (and overwritten if exists).

To make sure that whitelisted keys does not contain PHP tags, all occurrences of '<?' are replaced with '<_?' in whitelisted values.

Default $json_options are:

  • PHP >= 5.4: JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
  • PHP < 5.4: JSON_NUMERIC_CHECK

Options JSON_HEX_TAG and JSON_HEX_APOS are disabled, becouse they break PHP snippet.

◆ parse_json_file()

static parse_json_file (   $filename)
static

JSON version of parse_ini_file().

Throws JsonException on error.

◆ parse_json_string()

static parse_json_string (   $json_str,
  $filename = null 
)
static

Decode JSON string.

Parameters
$json_strString to parse.
$filenameIf set the exception message will contain $filename.