Tripal
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | List of all members
Drupal\tripal\Services\TripalTokenParser Class Reference

Public Member Functions

 replaceTokensArray (array $tokenized_strings, array $token_values, bool $strict=FALSE)
 
 replaceTokens (string $tokenized_string, array $token_values, bool $strict=FALSE)
 
 validateTokens (string $tokenized_string, array $valid_tokens)
 

Protected Member Functions

 firstMatchedToken (string $token_string, array $values)
 

Detailed Description

The TripalTokenParser class replaces tokens in strings with a corresponding value. Tokens are marked by square brackets, and the square brackets may be 1 or 2 levels deep. This allows having a prefix or suffix that is only included when the token has a defined value.

A simple token string example: "The title is [Title]." An example of a token that may or may not be defined "Issue": "[Journal]. [Volume][([Issue])]" If defined the replaced string could be "Science. 123(45)" but if Issue is not defined then the parentheses are not included "Science. 123"

In some cases the token may have several possible values, and this can be designated by a pipe "|" delimited list. For example: "[ [Journal Name|Series Name|Journal Abbreviation].]" The first of these that has a defined value will be used for the substitution, or if none are defined, the token is removed.

Any prefix or suffix in a 2-level token is checked against the token value, and if already present, is not added a second time. For example, for a token "[[Title].]" and a title value of "Cool Science.", then the replaced value will not include both periods, only one is retained.

Member Function Documentation

◆ firstMatchedToken()

Drupal\tripal\Services\TripalTokenParser::firstMatchedToken ( string  $token_string,
array  $values 
)
protected

Determine the first token in a '|' delimited string of tokens that has defined value in the $values array.

Parameters
string$token_stringOne or more tokens delimited by "|"
array$valuesAssociative array of key value pairs where keys correspond to the tokens.
Returns
string The first matching token that is present as a key in $values and is not null. Returns an empty string if none of the tokens have defined values.

◆ replaceTokens()

Drupal\tripal\Services\TripalTokenParser::replaceTokens ( string  $tokenized_string,
array  $token_values,
bool  $strict = FALSE 
)

Replaces tokens within a single tokenized string with corresponding values.

Parameters
string$tokenized_stringA string with tokens contained within one or two levels of square brackets. Multiple candidate tokens are separated by pipe character. For example [ [Journal Name|Journal Abbreviation].] In this latter case, parsing left to right, the first token that has a defined value will be used for replacement.
array$token_valuesValues to use for token replacement. Key is token name without square brackets, value is replacement value for the token.
bool$strictIf FALSE (default), tokens with no defined value are removed. If TRUE, these tokens are left in the returned string
Returns
string The passed $tokenized_string with tokens replaced with corresponding values, or the token is removed if no token replacement value is defined.

◆ replaceTokensArray()

Drupal\tripal\Services\TripalTokenParser::replaceTokensArray ( array  $tokenized_strings,
array  $token_values,
bool  $strict = FALSE 
)

Replaces tokens within multiple tokenized strings with corresponding values.

Parameters
array$tokenized_stringsAn array of strings with tokens contained within one or two levels of square brackets. Multiple candidate tokens are separated by pipe character. For example [ [Journal Name|Journal Abbreviation].] In this latter case, parsing left to right, the first token that has a defined value will be used for replacement.
array$token_valuesValues to use for token replacement. Key is token name without square brackets, value is replacement value for the token.
bool$strictIf FALSE (default), tokens with no defined value are removed. If TRUE, these tokens are left in the returned string
Returns
array An array with all of the strings from the input $tokenized_strings array but with tokens replaced with appropriate values, or removed if no token value is availiable.

◆ validateTokens()

Drupal\tripal\Services\TripalTokenParser::validateTokens ( string  $tokenized_string,
array  $valid_tokens 
)

Finds any tokens in the passed string that are not valid. This can be used to validate user input on a form.

Parameters
string$tokenized_stringA string containing tokens to validate.
array$valid_tokensAn array whose keys are valid tokens, without brackets. Array values are not used here.
Returns
array A list of all tokens not present in the $valid_tokens array. An empty array indicates that validation has passed.

The documentation for this class was generated from the following file: