|
Tripal
|
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) | |
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.
|
protected |
Determine the first token in a '|' delimited string of tokens that has defined value in the $values array.
| string | $token_string | One or more tokens delimited by "|" |
| array | $values | Associative array of key value pairs where keys correspond to the tokens. |
| Drupal\tripal\Services\TripalTokenParser::replaceTokens | ( | string | $tokenized_string, |
| array | $token_values, | ||
| bool | $strict = FALSE |
||
| ) |
Replaces tokens within a single tokenized string with corresponding values.
| string | $tokenized_string | A 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_values | Values to use for token replacement. Key is token name without square brackets, value is replacement value for the token. |
| bool | $strict | If FALSE (default), tokens with no defined value are removed. If TRUE, these tokens are left in the returned string |
| Drupal\tripal\Services\TripalTokenParser::replaceTokensArray | ( | array | $tokenized_strings, |
| array | $token_values, | ||
| bool | $strict = FALSE |
||
| ) |
Replaces tokens within multiple tokenized strings with corresponding values.
| array | $tokenized_strings | An 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_values | Values to use for token replacement. Key is token name without square brackets, value is replacement value for the token. |
| bool | $strict | If FALSE (default), tokens with no defined value are removed. If TRUE, these tokens are left in the returned string |
| 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.
| string | $tokenized_string | A string containing tokens to validate. |
| array | $valid_tokens | An array whose keys are valid tokens, without brackets. Array values are not used here. |