Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
PFArrayMapTemplate | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
90 | |
0.00% |
0 / 1 |
run | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
90 |
1 | <?php |
2 | |
3 | /** |
4 | * '#arraymaptemplate' is called as: |
5 | * |
6 | * {{#arraymaptemplate:value|template|delimiter|new_delimiter}} |
7 | * |
8 | * This function makes the same template call for every section of a |
9 | * delimited string; each such section, as dictated by the 'delimiter' |
10 | * value, is passed as a first parameter to the template specified. |
11 | * Finally, the transformed strings are joined together using the |
12 | * 'new_delimiter' string. Both 'delimiter' and 'new_delimiter' |
13 | * default to commas. |
14 | * |
15 | * Example: to take a semicolon-delimited list, and call a template |
16 | * named 'Beautify' on each element in the list, you could call the |
17 | * following: |
18 | * |
19 | * {{#arraymaptemplate:blue;red;yellow|Beautify|;|;}} |
20 | */ |
21 | |
22 | class PFArrayMapTemplate { |
23 | public static function run( Parser $parser, $frame, $args ) { |
24 | // Set variables. |
25 | $value = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; |
26 | $template = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ''; |
27 | $delimiter = isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : ','; |
28 | $new_delimiter = isset( $args[3] ) ? trim( $frame->expand( $args[3] ) ) : ', '; |
29 | // Unstrip some. |
30 | $delimiter = $parser->getStripState()->unstripNoWiki( $delimiter ); |
31 | // let '\n' represent newlines |
32 | $delimiter = str_replace( '\n', "\n", $delimiter ); |
33 | $new_delimiter = str_replace( '\n', "\n", $new_delimiter ); |
34 | |
35 | if ( $delimiter == '' ) { |
36 | $values_array = preg_split( '/(.)/u', $value, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE ); |
37 | } else { |
38 | $values_array = explode( $delimiter, $value ); |
39 | } |
40 | |
41 | $results_array = []; |
42 | foreach ( $values_array as $old_value ) { |
43 | $old_value = trim( $old_value ); |
44 | if ( $old_value == '' ) { |
45 | continue; |
46 | } |
47 | $bracketed_value = $frame->virtualBracketedImplode( '{{', '|', '}}', |
48 | $template, '1=' . $old_value ); |
49 | // Special handling if preprocessor class is set to |
50 | // 'Preprocessor_Hash'. |
51 | if ( $bracketed_value instanceof PPNode_Hash_Array ) { |
52 | $bracketed_value = $bracketed_value->value; |
53 | } |
54 | $results_array[] = $parser->replaceVariables( |
55 | implode( '', $bracketed_value ), $frame ); |
56 | } |
57 | return implode( $new_delimiter, $results_array ); |
58 | } |
59 | } |