\TransvisionUtils

Utils class

Various static methods that don't belong yet to a specialized class

Summary

Methods
Properties
Constants
secureText()
checkboxDefaultOption()
checkboxState()
uniqueWords()
getHtmlSelectOptions()
getRepoStrings()
getRepoEntities()
cleanString()
checkAbnormalStringLength()
getOrSet()
getScriptPerformances()
logScriptPerformances()
addPerformancesHTTPHeader()
redYellowGreen()
pluralize()
ago()
redirectToAPI()
APIPromotion()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

secureText()

secureText(string  $string) : string

Sanitize a string or an array of strings for security before template use.

Parameters

string $string

The string we want to sanitize

Returns

string —

Sanitized string for security

checkboxDefaultOption()

checkboxDefaultOption(string  $option, string  $cookie) : string

Helper function to set checkboxes value for the default option in source locale, target locale and repository depending on the cookie

Parameters

string $option

The checkbox

string $cookie

Out cookie

Returns

string —

Checked html attribute if cookie matches $option or false

checkboxState()

checkboxState(string  $str, string  $extra = '') : string

Helper function to set checkboxes value in <input> on main search form Example: <input type="checkbox" id="case_sensitive" value="case_sensitive" <?=Utils::checkboxState($check['case_sensitive'])?>> />

Parameters

string $str

Usually the value of a GET/POST parameter setting a box

string $extra

Optional. Defaults to empty string.

Returns

string —

Checked attribute or empty string.

uniqueWords()

uniqueWords(string  $sentence) : array

Split a sentence in words from longest to shortest, ignoring words shorter than 2 characters.

Parameters

string $sentence

Returns

array —

all the words in the sentence sorted by length

getHtmlSelectOptions()

getHtmlSelectOptions(array  $options, string  $selected, boolean  $nice_labels = false) : string

Generate a list of <option> html tags from an array and mark one as selected

Parameters

array $options

All the values we want in

string $selected

put selected tag on a specific

boolean $nice_labels

Optional. Defaults to False. Use nice labels for the option. Indicates if $options is an associative array with the array value as the text inside the

Returns

string —

html

getRepoStrings()

getRepoStrings(string  $locale, string  $repository) : array

Return an array of strings for a locale from a repository

Parameters

string $locale

Locale we want to have strings for

string $repository

string repository such as central, mozilla_org...

Returns

array —

Localized strings or empty array if no match

getRepoEntities()

getRepoEntities(string  $locale, string  $repository) : array

Return an array of entities for a locale from a repository

Parameters

string $locale

Locale we want to have entities for

string $repository

string repository such as central, mozilla_org...

Returns

array —

Entities or empty array if no match

cleanString()

cleanString(string  $string) : string

Clean up for "noise" a string

Parameters

string $string

the string to clean up

Returns

string —

The cleaned up string

checkAbnormalStringLength()

checkAbnormalStringLength(string  $origin, string  $translated) : string

Compare original and translated strings to check abnormal length.

This is used in search views to warn of strings that look much wider or much shorter than English

Parameters

string $origin

The source string

string $translated

The string we want to compare to

Returns

string —

'large' or 'small' or false if it doesn't look abnormal

getOrSet()

getOrSet(array  $arr, string  $value, string  $fallback) : string

Check if a parameter exists, if not, return fallback value

Parameters

array $arr

Array in which we want to check $value

string $value

Parameter we want to check

string $fallback

Default value

Returns

string —

$value if $value into $arr, $fallback otherwise

getScriptPerformances()

getScriptPerformances() : array

Utility function to return the memory used by a script and the time needed to compute the data.

Returns

array —

[Memory peak in bytes, Memory peak in MB, Computation time]

logScriptPerformances()

logScriptPerformances() : void

Utility function to log to stderr the memory used by a script and the time needed to generate the page.

This is used only when the constants DEBUG and PERF_CHECK are set to True because we don't want to fill our logs with debug data on production.

addPerformancesHTTPHeader()

addPerformancesHTTPHeader() : void

Utility function to log the memory used by a script and the time needed to generate the page as an HTTP header.

redYellowGreen()

redYellowGreen(  $number) : \Transvision\the

Generate a red to green color from a numeric value

Parameters

$number

Returns

\Transvision\the —

RGB values separated by a comma

pluralize()

pluralize(integer  $count, string  $text) : \Transvision\the

Lazy function to handle English plural form

Parameters

integer $count

The value to check

string $text

The word to pluralize

Returns

\Transvision\the —

value concatenated with the word properly pluralized

ago()

ago(\DateTime  $datetime, \DateTime  $ref_time = '') : string

Get the elapsed/remaining time from a DateTime vs. now

Parameters

\DateTime $datetime

The DateTime object to check against current time

\DateTime $ref_time

Reference time to calculate the difference (optional)

Returns

string —

String containing the value concatenated with the pluralized unit

redirectToAPI()

redirectToAPI() : string

Return the current URL with the json GET variable appended This is used on views which also exist in our public API https://github.com/mozfr/transvision/wiki/JSON-API

Returns

string —

URL with 'json' appended as part of the query string

APIPromotion()

APIPromotion(string  $source_locale, string  $target_locale) : string

Return the current URL with the json GET variable appended and also adds locale codes if they are not already provided by redirectToAPI()

Parameters

string $source_locale

Locale code for source

string $target_locale

Locale code for target

Returns

string —

URL with 'json' appended as part of the query string