PHP Class Fluent

Author: Damian Mooyman ([email protected])
Inheritance: extends Object, implements TemplateGlobalProvider
Show file Open project: tractorcow/silverstripe-fluent Class Usage Examples

Protected Properties

Property Type Description
$_search_dapter FluentSearchAdapter Cached search adapter
$force_is_frontend boolean If set, "is_frontend" can be forced to return a value
$last_set_locale Indicates the last locale set via Cookies, in order to prevent excessive Cookie setting

Public Methods

Method Description
alias ( string $locale ) : string Determine the alias to use for a specific locale
any_match ( string $value, array $patterns ) : boolean Helper function to check if the value given is present in any of the patterns.
current_locale ( boolean $persist = true ) : string Gets the current locale
db_field_for_locale ( string $field, string $locale ) Determine the DB field name to use for the given base field
default_locale ( mixed $domain = null ) : string Retrieves the default locale
detect_browser_locale ( mixed $domain = null ) : string Determines the locale best matching the given list of browser locales
disable_default_prefix ( ) : boolean Check if default locale should have prefix disabled
domain_for_locale ( string $locale ) Determine the home domain for this locale
domains ( ) : array Retrieves any configured domains, assuming the site is running in domain mode.
get_persist_locale ( string $key = null ) : string | null Gets the locale currently set within either the session or cookie.
get_request_locale ( ) : string Gets the locale requested directly in the request, either via route, post, or query parameters
get_template_global_variables ( ) : array
init ( ) Initialise routes
install_locale ( boolean $persist = true ) Installs the current locale into i18n
isFieldModified ( DataObject $object, FormField $field, string | null $locale = null ) : boolean Given a field on an object and optionally a locale, compare its locale value against the default locale value to determine if the value is changed at the given locale.
is_domain_mode ( ) : boolean Determine if the website is in domain segmentation mode
is_frontend ( boolean $ignoreController = false ) : boolean Determines behaviour of locale filter in this request, by detecting whether to present an admin view of the site, or a frontend view.
is_locale ( string $locale ) : boolean Determine if a locale code is within the range of configured locales
locale_baseurl ( string $locale = null ) : string Determine the baseurl within a specified $locale.
locale_names ( ) : array Retrieves the list of locale names as an associative array
locale_native_name ( string $locale ) : string Fetch a native language string from the i18n class via a passed locale in the format "XX_xx". In the event a match cannot be found in any framework resource, an empty string is returned.
locales ( mixed $domain = null ) : array Retrieves the list of locales
regenerate_routes ( ) Forces regeneration of all locale routes
search_adapter ( ) : FluentSearchAdapter Retrieves a search adapter for the current database adapter
set_force_is_frontend ( boolean $force = true ) Sets whether to force "is_frontend" to return true or false. This can be used for situations where the frontend controllers are not involved, e.g. API modules, unit testing or retrieving data programmatically.
set_persist_locale ( string $locale, string $key = null ) Specify the locale to persist between sessions, or to use for the locale outside of locale-routed pages (such as in unit tests, custom controllers, etc).
with_locale ( string $locale, callable $callback ) : mixed Executes a callback with a locale to temporarily emulate.

Method Details

alias() public static method

Determine the alias to use for a specific locale
public static alias ( string $locale ) : string
$locale string Locale in language_Country format
return string Locale in its original form, or its alias if one exists

any_match() public static method

This function is case sensitive by default.
public static any_match ( string $value, array $patterns ) : boolean
$value string A string value to check against, potentially with parameters (E.g. 'Varchar(1023)')
$patterns array A list of strings, some of which may be regular expressions
return boolean True if this $value is present in any of the $patterns

current_locale() public static method

Gets the current locale
public static current_locale ( boolean $persist = true ) : string
$persist boolean Attempt to persist any detected locale within session / cookies
return string i18n locale code

db_field_for_locale() public static method

Determine the DB field name to use for the given base field
public static db_field_for_locale ( string $field, string $locale )
$field string DB field name
$locale string Locale to use

default_locale() public static method

Retrieves the default locale
public static default_locale ( mixed $domain = null ) : string
$domain mixed Domain to determine the default locale for. If null, the global default will be returned. If true, then the current domain will be used.
return string

detect_browser_locale() public static method

Determines the locale best matching the given list of browser locales
public static detect_browser_locale ( mixed $domain = null ) : string
$domain mixed Domain to determine the locales for. If null, the global list be returned. If true, then the current domain will be used.
return string The matching locale, or null if none could be determined

disable_default_prefix() public static method

Check if default locale should have prefix disabled
public static disable_default_prefix ( ) : boolean
return boolean

domain_for_locale() public static method

Determine the home domain for this locale
public static domain_for_locale ( string $locale )
$locale string

domains() public static method

Retrieves any configured domains, assuming the site is running in domain mode.
public static domains ( ) : array
return array List of domains and their respective configuration information

get_persist_locale() public static method

Gets the locale currently set within either the session or cookie.
public static get_persist_locale ( string $key = null ) : string | null
$key string ID to retrieve persistant locale from. Will automatically detect if omitted. Either Fluent::config()->persist_id or Fluent::config()->persist_id_cms.
return string | null The locale, if available

get_request_locale() public static method

Gets the locale requested directly in the request, either via route, post, or query parameters
public static get_request_locale ( ) : string
return string The locale, if available

get_template_global_variables() public static method

public static get_template_global_variables ( ) : array
return array

init() public static method

Initialise routes
public static init ( )

install_locale() public static method

Installs the current locale into i18n
public static install_locale ( boolean $persist = true )
$persist boolean Attempt to persist any detected locale within session / cookies

isFieldModified() public static method

Given a field on an object and optionally a locale, compare its locale value against the default locale value to determine if the value is changed at the given locale.
public static isFieldModified ( DataObject $object, FormField $field, string | null $locale = null ) : boolean
$object DataObject
$field FormField
$locale string | null Optional: if not provided, will be gathered from the request
return boolean

is_domain_mode() public static method

Determine if the website is in domain segmentation mode
public static is_domain_mode ( ) : boolean
return boolean

is_frontend() public static method

If viewing in the CMS items filtered by locale will always be visible, but in the frontend will be filtered as expected. For the sake of unit tests Fluent assumes a frontend execution environment.
public static is_frontend ( boolean $ignoreController = false ) : boolean
$ignoreController boolean Flag to indicate whether the current controller should be ignored, and detection should be performed by inspecting the URL. Used for testing. Defaults to false.
return boolean Flag indicating if the translation should act on the frontend

is_locale() public static method

Determine if a locale code is within the range of configured locales
public static is_locale ( string $locale ) : boolean
$locale string
return boolean True if this locale is valid

locale_baseurl() public static method

Determine the baseurl within a specified $locale.
public static locale_baseurl ( string $locale = null ) : string
$locale string Locale, or null to use current locale
return string

locale_names() public static method

Retrieves the list of locale names as an associative array
public static locale_names ( ) : array
return array List of locale names mapped by locale code

locale_native_name() public static method

Fetch a native language string from the i18n class via a passed locale in the format "XX_xx". In the event a match cannot be found in any framework resource, an empty string is returned.
public static locale_native_name ( string $locale ) : string
$locale string e.g. "pt_BR"
return string The native language string for that locale e.g. "português (Brazil)"

locales() public static method

Retrieves the list of locales
public static locales ( mixed $domain = null ) : array
$domain mixed Domain to determine the locales for. If null, the global list be returned. If true, then the current domain will be used.
return array List of locales

regenerate_routes() public static method

Forces regeneration of all locale routes
public static regenerate_routes ( )

search_adapter() public static method

Retrieves a search adapter for the current database adapter
public static search_adapter ( ) : FluentSearchAdapter
return FluentSearchAdapter

set_force_is_frontend() public static method

Sets whether to force "is_frontend" to return true or false. This can be used for situations where the frontend controllers are not involved, e.g. API modules, unit testing or retrieving data programmatically.
public static set_force_is_frontend ( boolean $force = true )
$force boolean

set_persist_locale() public static method

Not to be confused with the temporary locale assigned with {@see \Fluent::with_locale} .
public static set_persist_locale ( string $locale, string $key = null )
$locale string Locale to assign
$key string ID to set the locale against. Will automatically detect if omitted. Either Fluent:::config()->persist_id or Fluent::::config()->persist_id_cms.

with_locale() public static method

Warning: Existing DataObjects will contain fields in the actual locale if already lazily loaded, or if used within the callback, will populate itself with the overriding locale. The inverse will occur once the callback is complete. The best practice is to consider this a sandbox, and re-requery all objects required, discarding these afterwards.
public static with_locale ( string $locale, callable $callback ) : mixed
$locale string The locale to set
$callback callable The callback
return mixed The returned value from the $callback

Property Details

$_search_dapter protected static property

Cached search adapter
protected static FluentSearchAdapter $_search_dapter
return FluentSearchAdapter

$force_is_frontend protected static property

If set, "is_frontend" can be forced to return a value
protected static bool $force_is_frontend
return boolean

$last_set_locale protected static property

Indicates the last locale set via Cookies, in order to prevent excessive Cookie setting
protected static $last_set_locale