* @link https://TheTempusProject.com/Core * @license https://opensource.org/licenses/MIT [MIT LICENSE] */ namespace TheTempusProject\Houdini\Classes; use TheTempusProject\Canary\Bin\Canary as Debug; class Filters { public static $filters = [ 'comments' => [ 'name' => 'comments', 'match' => '#/\*.*?\*/#s', 'replace' => '', 'enabled' => true, ], 'commentsTwo' => [ 'name' => 'commentsTwo', 'match' => '#(? '', 'enabled' => true, ], 'formComponents' => [ 'name' => 'formComponents', 'match' => '#{CHECKED:(.*?)=(.*?)}#s', 'replace' => '', 'enabled' => true, ], ]; /** * Iterates through the filters list on $data. Leaving only the internal * contents of enabled filters and removing all traces of disabled filters. * * @param {string} [$data] * @return {string} */ public static function apply( $data, $force = false ) { if ( empty( self::$filters ) ) { return $data; } foreach ( self::$filters as $pattern ) { if ( $pattern['enabled'] || false !== $force ) { if ( ! empty( $pattern['callback'] ) && is_array( $pattern['callback'] ) ) { $result = preg_replace_callback( $pattern['match'], $pattern['callback'], $data ); } else { $result = preg_replace( $pattern['match'], $pattern['replace'], $data ); } $data = trim( $result ); } } return $data; } public static function applyOne( $name, $data, $force = false ) { if ( empty( self::$filters ) || empty( self::$filters[$name] ) ) { return $data; } if ( self::$filters[$name]['enabled'] || false !== $force ) { if ( ! empty( self::$filters[$name]['callback'] ) && is_array( self::$filters[$name]['callback'] ) ) { $result = preg_replace_callback( self::$filters[$name]['match'], self::$filters[$name]['callback'], $data ); } else { $result = preg_replace( self::$filters[$name]['match'], self::$filters[$name]['replace'], $data ); } $data = trim( $result ); } return $data; } /** * Adds a filter. * * @param {string} [$filterName] * @param {string} [$match] * @param {string} [$replace] * @param {bool} [$enabled] - Whether the filter should be enabled or disabled. */ public static function add( $filterName, $match, $replace, $enabled = false, $callback = false ) { if ( isset( self::$filters[$filterName] ) ) { Debug::error( "Filter already exists: $filterName" ); return; } if ( $callback === true ) { self::$filters[ $filterName ] = [ 'name' => $filterName, 'match' => $match, 'callback' => $replace, 'enabled' => $enabled, ]; return; } self::$filters[ $filterName ] = [ 'name' => $filterName, 'match' => $match, 'replace' => $replace, 'enabled' => $enabled, ]; return; } /** * Removes a filter. * * @param {string} [$filterName] * @return {bool} */ public function remove( $filterName ) { if ( !isset( self::$filters[$filterName] ) ) { Debug::error( "Filter does not exist: $filterName" ); return false; } unset( self::$filters[$filterName] ); return true; } /** * Enable a filter. * * @param {string} [$filterName] * @return {bool} */ public function enable( $filterName ) { if ( !isset( self::$filters[$filterName] ) ) { Debug::error( "Filter does not exist: $filterName" ); return false; } self::$filters[$filterName]['enabled'] = true; return true; } /** * Disables a filter. * * @param {string} [$filterName] * @return {bool} */ public function disable( $filterName ) { if ( !isset( self::$filters[$filterName] ) ) { Debug::error( "Filter does not exist: $filterName" ); return false; } self::$filters[$filterName]['enabled'] = false; return true; } }