20 protected $regex =
null;
26 protected $display =
null;
31 protected $pre =
null;
36 protected $post =
null;
71 if ( is_string( $params ) ) {
72 $this->regex = $params;
73 } elseif ( is_array( $params ) ) {
75 $this->regex = $params[
'regex'] ??
null;
76 $this->display = $params[
'display'] ??
null;
77 $this->pre = $params[
'pre'] ??
null;
78 $this->post = $params[
'post'] ??
null;
81 if ( $this->regex ===
null ) {
82 throw new InvalidArgumentException(
83 'Invalid configuration for the RegexInsertablesSuggester. Did not find a regex specified'
87 if ( $this->display !==
null && $this->pre ===
null ) {
90 $this->pre = $this->display;
96 preg_match_all( $this->regex, $text, $matches, PREG_SET_ORDER );
98 return array_map( [ $this,
'mapInsertables' ], $matches );
101 protected function mapInsertables( array $match ) {
102 if ( $this->display ===
null ) {
103 return new Insertable( $match[0], $match[0] );
108 foreach ( $match as $key => $value ) {
109 if ( !is_int( $key ) ) {
110 $tmpKey =
'$' . $key;
111 $replacements[ $tmpKey ] = $value;
115 $displayVal = strtr( $this->display, $replacements );
116 $preVal = strtr( $this->pre, $replacements );
118 if ( $this->post !==
null ) {
119 $postVal = strtr( $this->post, $replacements );
122 return new Insertable( $displayVal, $preVal, $postVal );