eZ\Publish\Core\Persistence\TransformationProcessor\DefinitionBased\Parser::parseString PHP Method

parseString() public method

Parse the given string into an AST.
public parseString ( string $string ) : array
$string string
return array
    public function parseString($string)
    {
        $tokens = $this->tokenize($string);
        $tokens = array_filter($tokens, function ($token) {
            return !($token['type'] === TransformationProcessor::T_WHITESPACE || $token['type'] === TransformationProcessor::T_COMMENT);
        });
        $ast = array();
        $section = null;
        while ($token = array_shift($tokens)) {
            if ($token['type'] === TransformationProcessor::T_SECTION) {
                $section = $token['data']['section'];
                continue;
            }
            if ($section === null) {
                throw new RuntimeException('Expected section.');
            }
            $ast[$section][] = $token;
        }
        return $ast;
    }

Usage Example

 public function testCompileModuloTranspose()
 {
     $parser = new Persistence\TransformationProcessor\DefinitionBased\Parser(self::getInstallationDir());
     $compiler = new Persistence\TransformationProcessor\PcreCompiler(new Persistence\Utf8Converter());
     $rules = $compiler->compile($parser->parseString("transpose_modulo_test:\n" . "U+00e0 - U+00e6 % 02 - 01"));
     $this->assertSame('ßááããååçè', $this->applyTransformations($rules, 'àáâãäåæçè'));
 }