prefix = $this->prefix ?? '';
$this->suffix = $this->suffix ?? '';
// functions which return true if escape prefix/suffix need to be added
$matcher = function ( string $re, bool $invert ): callable {
return ( function ( string $context ) use ( $re, $invert ): bool {
return ( preg_match( $re, $context ) ) ? !$invert : $invert;
} );
};
$false = function ( string $context ): bool {
return false;
};
$this->prefixMatcher =
( $args['goodPrefix'] ?? false ) ?
$matcher( $args['goodPrefix'], true ) :
( ( $args['badPrefix'] ?? false ) ?
$matcher( $args['badPrefix'], false ) : $false );
$this->suffixMatcher =
( $args['goodSuffix'] ?? false ) ?
$matcher( $args['goodSuffix'], true ) :
( ( $args['badSuffix'] ?? false ) ?
$matcher( $args['badSuffix'], false ) : $false );
}
/** @inheritDoc */
public function escape( State $state ): Result {
$result = new Result( $this->text );
if ( call_user_func( $this->prefixMatcher, $state->leftContext ) ) {
$result->prefix = $this->prefix;
}
if ( call_user_func( $this->suffixMatcher, $state->rightContext ) ) {
$result->suffix = $this->suffix;
}
return $result;
}
}