Skip to content

Commit

Permalink
XPathConvertor: updated to use RecursiveParser
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshyPHP committed Jul 22, 2019
1 parent 27f7090 commit a3e52ec
Show file tree
Hide file tree
Showing 15 changed files with 255 additions and 628 deletions.
138 changes: 67 additions & 71 deletions docs/testdox.txt
Expand Up @@ -5973,77 +5973,6 @@ s9e\TextFormatter\Tests\Plugins\Preg\Parser
[x] Parsing+rendering tests with data set #2
[x] Parsing+rendering tests with data set #3

s9e\TextFormatter\Tests\Configurator\RendererGenerators\PHP\XPathConvertor
[x] convertXPath() tests with data set #0
[x] convertXPath() tests with data set #1
[x] convertXPath() tests with data set #2
[x] convertXPath() tests with data set #3
[x] convertXPath() tests with data set #4
[x] convertXPath() tests with data set #5
[x] convertXPath() tests with data set #6
[x] convertXPath() tests with data set #7
[x] convertXPath() tests with data set #8
[x] convertXPath() tests with data set #9
[x] convertXPath() tests with data set #10
[x] convertXPath() tests with data set #11
[x] convertXPath() tests with data set #12
[x] convertXPath() tests with data set #13
[x] convertXPath() tests with data set #14
[x] convertXPath() tests with data set #15
[x] convertXPath() tests with data set #16
[x] convertXPath() tests with data set #17
[x] convertXPath() tests with data set #18
[x] convertXPath() tests with data set #19
[x] convertXPath() tests with data set #20
[x] convertXPath() tests with data set #21
[x] convertXPath() tests with data set #22
[x] convertXPath() tests with data set #23
[x] convertXPath() tests with data set #24
[x] convertXPath() tests with data set #25
[x] convertXPath() tests with data set #26
[x] convertXPath() tests with data set #27
[x] convertXPath() tests with data set #28
[x] convertXPath() tests with data set #29
[x] convertXPath() tests with data set #30
[x] convertXPath() tests with data set #31
[x] convertCondition() tests with data set #0
[x] convertCondition() tests with data set #1
[x] convertCondition() tests with data set #2
[x] convertCondition() tests with data set #3
[x] convertCondition() tests with data set #4
[x] convertCondition() tests with data set #5
[x] convertCondition() tests with data set #6
[x] convertCondition() tests with data set #7
[x] convertCondition() tests with data set #8
[x] convertCondition() tests with data set #9
[x] convertCondition() tests with data set #10
[x] convertCondition() tests with data set #11
[x] convertCondition() tests with data set #12
[x] convertCondition() tests with data set #13
[x] convertCondition() tests with data set #14
[x] convertCondition() tests with data set #15
[x] convertCondition() tests with data set #16
[x] convertCondition() tests with data set #17
[x] convertCondition() tests with data set #18
[x] convertCondition() tests with data set #19
[x] convertCondition() tests with data set #20
[x] convertCondition() tests with data set #21
[x] convertCondition() tests with data set #22
[x] convertCondition() tests with data set #23
[x] convertCondition() tests with data set #24
[x] convertCondition() tests with data set #25
[x] convertCondition() tests with data set #26
[x] convertCondition() tests with data set #27
[x] convertCondition() tests with data set #28
[x] convertCondition() tests with data set #29
[x] convertCondition() tests with data set #30
[x] convertCondition() tests with data set #31
[x] convertCondition() tests with data set #32

s9e\TextFormatter\Tests\Configurator\RendererGenerators\PHP\XPathConvertor\Runner
[x] convert() throws an exception if the expression cannot be converted
[x] Convertors can be set in the constructor

s9e\TextFormatter\Tests\Configurator\RendererGenerators\PHP\XPathConvertor\Convertors\BooleanFunctions
[x] with data set #0
[x] with data set #1
Expand Down Expand Up @@ -6140,6 +6069,73 @@ s9e\TextFormatter\Tests\Configurator\RendererGenerators\PHP\XPathConvertor\Conve
[x] with data set #8
[x] with data set #9

s9e\TextFormatter\Tests\Configurator\RendererGenerators\PHP\XPathConvertor
[x] convertXPath() tests with data set #0
[x] convertXPath() tests with data set #1
[x] convertXPath() tests with data set #2
[x] convertXPath() tests with data set #3
[x] convertXPath() tests with data set #4
[x] convertXPath() tests with data set #5
[x] convertXPath() tests with data set #6
[x] convertXPath() tests with data set #7
[x] convertXPath() tests with data set #8
[x] convertXPath() tests with data set #9
[x] convertXPath() tests with data set #10
[x] convertXPath() tests with data set #11
[x] convertXPath() tests with data set #12
[x] convertXPath() tests with data set #13
[x] convertXPath() tests with data set #14
[x] convertXPath() tests with data set #15
[x] convertXPath() tests with data set #16
[x] convertXPath() tests with data set #17
[x] convertXPath() tests with data set #18
[x] convertXPath() tests with data set #19
[x] convertXPath() tests with data set #20
[x] convertXPath() tests with data set #21
[x] convertXPath() tests with data set #22
[x] convertXPath() tests with data set #23
[x] convertXPath() tests with data set #24
[x] convertXPath() tests with data set #25
[x] convertXPath() tests with data set #26
[x] convertXPath() tests with data set #27
[x] convertXPath() tests with data set #28
[x] convertXPath() tests with data set #29
[x] convertXPath() tests with data set #30
[x] convertXPath() tests with data set #31
[x] convertCondition() tests with data set #0
[x] convertCondition() tests with data set #1
[x] convertCondition() tests with data set #2
[x] convertCondition() tests with data set #3
[x] convertCondition() tests with data set #4
[x] convertCondition() tests with data set #5
[x] convertCondition() tests with data set #6
[x] convertCondition() tests with data set #7
[x] convertCondition() tests with data set #8
[x] convertCondition() tests with data set #9
[x] convertCondition() tests with data set #10
[x] convertCondition() tests with data set #11
[x] convertCondition() tests with data set #12
[x] convertCondition() tests with data set #13
[x] convertCondition() tests with data set #14
[x] convertCondition() tests with data set #15
[x] convertCondition() tests with data set #16
[x] convertCondition() tests with data set #17
[x] convertCondition() tests with data set #18
[x] convertCondition() tests with data set #19
[x] convertCondition() tests with data set #20
[x] convertCondition() tests with data set #21
[x] convertCondition() tests with data set #22
[x] convertCondition() tests with data set #23
[x] convertCondition() tests with data set #24
[x] convertCondition() tests with data set #25
[x] convertCondition() tests with data set #26
[x] convertCondition() tests with data set #27
[x] convertCondition() tests with data set #28
[x] convertCondition() tests with data set #29
[x] convertCondition() tests with data set #30
[x] convertCondition() tests with data set #31
[x] convertCondition() tests with data set #32

s9e\TextFormatter\Tests\Configurator\RendererGenerators\PHP\BranchOutputOptimizer
[x] optimize() tests with data set #0
[x] optimize() tests with data set #1
Expand Down
3 changes: 1 addition & 2 deletions phpunit.xml
Expand Up @@ -285,8 +285,6 @@
<file>tests/Plugins/Preg/ConfiguratorTest.php</file>
<file>tests/Plugins/Preg/ParserTest.php</file>

<file>tests/Configurator/RendererGenerators/PHP/XPathConvertorTest.php</file>
<file>tests/Configurator/RendererGenerators/PHP/XPathConvertor/RunnerTest.php</file>
<file>tests/Configurator/RendererGenerators/PHP/XPathConvertor/Convertors/BooleanFunctionsTest.php</file>
<file>tests/Configurator/RendererGenerators/PHP/XPathConvertor/Convertors/BooleanOperatorsTest.php</file>
<file>tests/Configurator/RendererGenerators/PHP/XPathConvertor/Convertors/ComparisonsTest.php</file>
Expand All @@ -295,6 +293,7 @@
<file>tests/Configurator/RendererGenerators/PHP/XPathConvertor/Convertors/MultiByteStringManipulationTest.php</file>
<file>tests/Configurator/RendererGenerators/PHP/XPathConvertor/Convertors/SingleByteStringFunctionsTest.php</file>
<file>tests/Configurator/RendererGenerators/PHP/XPathConvertor/Convertors/SingleByteStringManipulationTest.php</file>
<file>tests/Configurator/RendererGenerators/PHP/XPathConvertorTest.php</file>
<file>tests/Configurator/RendererGenerators/PHP/BranchOutputOptimizerTest.php</file>
<file>tests/Configurator/RendererGenerators/PHP/ControlStructuresOptimizerTest.php</file>
<file>tests/Configurator/RendererGenerators/PHP/OptimizerTest.php</file>
Expand Down
48 changes: 41 additions & 7 deletions src/Configurator/RendererGenerators/PHP/XPathConvertor.php
Expand Up @@ -8,21 +8,29 @@
namespace s9e\TextFormatter\Configurator\RendererGenerators\PHP;

use RuntimeException;
use s9e\TextFormatter\Configurator\RendererGenerators\PHP\XPathConvertor\Runner;
use s9e\TextFormatter\Configurator\RecursiveParser;
use s9e\TextFormatter\Configurator\RendererGenerators\PHP\XPathConvertor\Convertors\BooleanFunctions;
use s9e\TextFormatter\Configurator\RendererGenerators\PHP\XPathConvertor\Convertors\BooleanOperators;
use s9e\TextFormatter\Configurator\RendererGenerators\PHP\XPathConvertor\Convertors\Comparisons;
use s9e\TextFormatter\Configurator\RendererGenerators\PHP\XPathConvertor\Convertors\Core;
use s9e\TextFormatter\Configurator\RendererGenerators\PHP\XPathConvertor\Convertors\Math;
use s9e\TextFormatter\Configurator\RendererGenerators\PHP\XPathConvertor\Convertors\MultiByteStringManipulation;
use s9e\TextFormatter\Configurator\RendererGenerators\PHP\XPathConvertor\Convertors\SingleByteStringFunctions;
use s9e\TextFormatter\Configurator\RendererGenerators\PHP\XPathConvertor\Convertors\SingleByteStringManipulation;

class XPathConvertor
{
/**
* @var Runner
* @var RecursiveParser
*/
protected $runner;
protected $parser;

/**
* Constructor
*/
public function __construct(Runner $runner = null)
public function __construct(RecursiveParser $parser = null)
{
$this->runner = $runner ?: new Runner;
$this->parser = $parser ?: $this->getDefaultParser();
}

/**
Expand Down Expand Up @@ -52,7 +60,7 @@ public function convertCondition($expr)

try
{
return $this->runner->convert($expr);
return $this->parser->parse($expr)['value'];
}
catch (RuntimeException $e)
{
Expand Down Expand Up @@ -80,7 +88,7 @@ public function convertXPath($expr)
$expr = trim($expr);
try
{
return $this->runner->convert($expr);
return $this->parser->parse($expr)['value'];
}
catch (RuntimeException $e)
{
Expand Down Expand Up @@ -151,6 +159,32 @@ protected function exportXPathParam($param)
return '$this->getParamAsXPath(' . var_export($paramName, true) . ')';
}

/**
* Generate and return the a parser with the default set of matchers
*
* @return RecursiveParser
*/
protected function getDefaultParser()
{
$parser = new RecursiveParser;
$matchers = [];
$matchers[] = new SingleByteStringFunctions($parser);
$matchers[] = new BooleanFunctions($parser);
$matchers[] = new BooleanOperators($parser);
$matchers[] = new Comparisons($parser);
$matchers[] = new Core($parser);
$matchers[] = new Math($parser);
if (extension_loaded('mbstring'))
{
$matchers[] = new MultiByteStringManipulation($parser);
}
$matchers[] = new SingleByteStringManipulation($parser);

$parser->setMatchers($matchers);

return $parser;
}

/**
* Tokenize an XPath expression for use in PHP
*
Expand Down
Expand Up @@ -7,51 +7,10 @@
*/
namespace s9e\TextFormatter\Configurator\RendererGenerators\PHP\XPathConvertor\Convertors;

use s9e\TextFormatter\Configurator\RendererGenerators\PHP\XPathConvertor\Runner;
use s9e\TextFormatter\Configurator\RecursiveParser\AbstractRecursiveMatcher;

abstract class AbstractConvertor
abstract class AbstractConvertor extends AbstractRecursiveMatcher
{
/**
* @var Runner
*/
protected $runner;

/**
* Constructor
*
* @param Runner $runner
* @return void
*/
public function __construct(Runner $runner)
{
$this->runner = $runner;
}

/**
* Return the name of the group each regexp belongs to
*
* @return array Regexp's name as key, regexp's group as value
*/
abstract public function getRegexpGroups();

/**
* Return the regexps associated with this convertor
*
* @return array Regexp's name as key, regexp as value
*/
abstract public function getRegexps();

/**
* Convert given XPath expression to PHP
*
* @param string $expr
* @return string
*/
protected function convert($expr)
{
return $this->runner->convert($expr);
}

/**
* Retrieve the attribute name from an attribute expression
*
Expand Down

0 comments on commit a3e52ec

Please sign in to comment.