ElevenRank name. * * @since 1.0.0 * * @var array */ private $names; /** * Optional function for names that carry an argument, such as a custom field. It receives the * name and returns an ElevenRank variable, or null. * * @since 1.0.0 * * @var callable|null */ private $extra; /** * Constructor. * * @since 1.0.0 * * @param string $pattern Regular expression with the variable's name in group 1. * @param array $names Direct mappings. * @param callable|null $extra Mapping for names that carry an argument. */ public function __construct( string $pattern, array $names, ?callable $extra = null ) { $this->pattern = $pattern; $this->names = $names; $this->extra = $extra; } /** * Converts a template. * * @since 1.0.0 * * @param string $template Template in the source plugin's syntax. * @return array{text: string, unsupported: string[]} */ public function convert( string $template ): array { $unsupported = array(); $text = (string) preg_replace_callback( $this->pattern, function ( $found ) use ( &$unsupported ) { $name = $found[1]; if ( isset( $this->names[ $name ] ) ) { return '%' . $this->names[ $name ] . '%'; } if ( null !== $this->extra ) { $mapped = call_user_func( $this->extra, $name ); if ( is_string( $mapped ) && '' !== $mapped ) { return $mapped; } } $unsupported[ $name ] = $name; return ''; }, $template ); $text = trim( (string) preg_replace( '/[ \t]{2,}/', ' ', $text ) ); return array( 'text' => $text, 'unsupported' => array_values( $unsupported ), ); } }