kahlan\util\Text::clean PHP Method

clean() public static method

Cleans up a Text::insert() formatted string with given $options depending on the 'clean' option. The goal of this function is to replace all whitespace and unneeded mark-up around place-holders that did not get replaced by Text::insert().
public static clean ( string $str, array $options = [] ) : string
$str string The string to clean.
$options array Available options are: - `'before'`: characters marking the start of targeted substring. - `'after'`: characters marking the end of targeted substring. - `'escape'`: The character or string used to escape the before character or string (defaults to `'\\'`). - `'gap'`: Regular expression matching gaps. - `'word'`: Regular expression matching words. - `'replacement'`: String to use for cleaned substrings (defaults to `''`).
return string The cleaned string.
    public static function clean($str, $options = [])
    {
        $options += ['before' => '{:', 'after' => '}', 'escape' => '\\', 'word' => '[\\w,.]+', 'gap' => '(\\s*(?:(?:and|or|,)\\s*)?)', 'replacement' => ''];
        extract($options);
        $begin = $escape ? '(?<!' . preg_quote($escape) . ')' . preg_quote($before) : preg_quote($before);
        $end = preg_quote($options['after']);
        $callback = function ($matches) use($replacement) {
            if (isset($matches[2]) && isset($matches[3]) && trim($matches[2]) === trim($matches[3])) {
                if (trim($matches[2]) || $matches[2] && $matches[3]) {
                    return $matches[2] . $replacement;
                }
            }
            return $replacement;
        };
        $str = preg_replace_callback('/(' . $gap . $before . $word . $after . $gap . ')+/', $callback, $str);
        if ($escape) {
            $str = preg_replace('/' . preg_quote($escape) . preg_quote($before) . '/', $before, $str);
        }
        return $str;
    }

Usage Example

Example #1
0
     it("cleans placeholder and adjacent `'and'`", function () {
         $result = Text::clean('{:a} and 2 and 3');
         $this->expect($result)->toBe('2 and 3');
         $result = Text::clean('2 and {:a} and 3');
         $this->expect($result)->toBe('2 and 3');
         $result = Text::clean('{:a} and {:b} and 3');
         $this->expect($result)->toBe('3');
         $result = Text::clean('{:a} and 3 and {:b}');
         $this->expect($result)->toBe('3');
         $result = Text::clean('{:a} and {:b} and {:c}');
         $this->expect($result)->toBe('');
     });
     it("cleans placeholder and adjacent comma and `'and'`", function () {
         $result = Text::clean('{:a}, 2 and 3');
         $this->expect($result)->toBe('2 and 3');
         $result = Text::clean('{:a}, 2 and {:c}');
         $this->expect($result)->toBe('2');
     });
 });
 describe("::toString()", function () {
     it("exports an empty array", function () {
         $dump = Text::toString([]);
         $this->expect($dump)->toBe("[]");
     });
     it("exports an object", function () {
         $dump = Text::toString(new stdClass());
         $this->expect($dump)->toBe("`stdClass`");
     });
     it("exports an object supporting __toString()", function () {
         $stub = Double::instance();
         allow($stub)->toReceive('__toString')->andReturn('jedi');