Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 36 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| PFArrayMap | |
0.00% |
0 / 36 |
|
0.00% |
0 / 1 |
240 | |
0.00% |
0 / 1 |
| run | |
0.00% |
0 / 36 |
|
0.00% |
0 / 1 |
240 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * '#arraymap' is called as: |
| 5 | * {{#arraymap:value|delimiter|var|formula|new_delimiter}} |
| 6 | * |
| 7 | * This function applies the same transformation to every section of a |
| 8 | * delimited string; each such section, as dictated by the 'delimiter' |
| 9 | * value, is given the same transformation that the 'var' string is |
| 10 | * given in 'formula'. Finally, the transformed strings are joined |
| 11 | * together using the 'new_delimiter' string. Both 'delimiter' and |
| 12 | * 'new_delimiter' default to commas. |
| 13 | * |
| 14 | * Example: to take a semicolon-delimited list, and make each element |
| 15 | * in the list a link, you could call the following: |
| 16 | * |
| 17 | * {{#arraymap:blue;red;yellow|;|x|[[x]]|;}} |
| 18 | */ |
| 19 | |
| 20 | class PFArrayMap { |
| 21 | public static function run( Parser $parser, $frame, $args ) { |
| 22 | // Set variables. |
| 23 | $value = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; |
| 24 | $delimiter = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ','; |
| 25 | $var = isset( $args[2] ) ? trim( $frame->expand( $args[2], PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES ) ) : 'x'; |
| 26 | $formula = $args[3] ?? 'x'; |
| 27 | $new_delimiter = isset( $args[4] ) ? trim( $frame->expand( $args[4] ) ) : ', '; |
| 28 | $conjunction = isset( $args[5] ) ? trim( $frame->expand( $args[5] ) ) : $new_delimiter; |
| 29 | // Unstrip some. |
| 30 | $delimiter = $parser->getStripState()->unstripNoWiki( $delimiter ); |
| 31 | // Let '\n' represent newlines, and '\s' represent spaces. |
| 32 | $delimiter = str_replace( [ '\n', '\s' ], [ "\n", ' ' ], $delimiter ); |
| 33 | $new_delimiter = str_replace( [ '\n', '\s' ], [ "\n", ' ' ], $new_delimiter ); |
| 34 | $conjunction = str_replace( [ '\n', '\s' ], [ "\n", ' ' ], $conjunction ); |
| 35 | |
| 36 | if ( $delimiter == '' ) { |
| 37 | $values_array = preg_split( '/(.)/u', $value, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE ); |
| 38 | } else { |
| 39 | $values_array = explode( $delimiter, $value ); |
| 40 | } |
| 41 | |
| 42 | $results_array = []; |
| 43 | // Add results to the results array only if the old value was |
| 44 | // non-null, and the new, mapped value is non-null as well. |
| 45 | foreach ( $values_array as $old_value ) { |
| 46 | $old_value = trim( $old_value ); |
| 47 | if ( $old_value == '' ) { |
| 48 | continue; |
| 49 | } |
| 50 | $result_value = $frame->expand( $formula, PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES ); |
| 51 | $result_value = str_replace( $var, $old_value, $result_value ); |
| 52 | $result_value = $parser->preprocessToDom( $result_value, $frame->isTemplate() ? Parser::PTD_FOR_INCLUSION : 0 ); |
| 53 | $result_value = trim( $frame->expand( $result_value ) ); |
| 54 | if ( $result_value == '' ) { |
| 55 | continue; |
| 56 | } |
| 57 | $results_array[] = $result_value; |
| 58 | } |
| 59 | if ( $conjunction != $new_delimiter ) { |
| 60 | $conjunction = " " . trim( $conjunction ) . " "; |
| 61 | } |
| 62 | |
| 63 | $result_text = ""; |
| 64 | $num_values = count( $results_array ); |
| 65 | for ( $i = 0; $i < $num_values; $i++ ) { |
| 66 | if ( $i == 0 ) { |
| 67 | $result_text .= $results_array[$i]; |
| 68 | } elseif ( $i == $num_values - 1 ) { |
| 69 | $result_text .= $conjunction . $results_array[$i]; |
| 70 | } else { |
| 71 | $result_text .= $new_delimiter . $results_array[$i]; |
| 72 | } |
| 73 | } |
| 74 | return $result_text; |
| 75 | } |
| 76 | } |