-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from prohalexey/node_processors
Totally refactored. Splitted treeProcessor into several different processors
- Loading branch information
Showing
84 changed files
with
1,518 additions
and
1,081 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
language: php | ||
|
||
php: | ||
- 7.0 | ||
- 7.1 | ||
- 7.2 | ||
|
||
before_script: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TheChoice\Builder; | ||
|
||
use TheChoice\Factory\NodeConditionFactory; | ||
use TheChoice\Factory\NodeCollectionFactory; | ||
use TheChoice\Factory\NodeContextFactory; | ||
use TheChoice\Factory\NodeTreeFactory; | ||
use TheChoice\Factory\NodeValueFactory; | ||
use Psr\Container\ContainerInterface; | ||
|
||
use TheChoice\Contract\OperatorFactoryInterface; | ||
use TheChoice\Contract\BuilderInterface; | ||
use TheChoice\Exception\InvalidArgumentException; | ||
use TheChoice\Exception\LogicException; | ||
use TheChoice\Node\Root; | ||
use TheChoice\NodeFactory\NodeFactoryInterface; | ||
use TheChoice\NodeFactory\NodeFactoryResolverInterface; | ||
|
||
class ArrayBuilder implements BuilderInterface | ||
{ | ||
private $_nodesCount = 0; | ||
protected $rootNode; | ||
|
||
private $_nodeTreeFactory; | ||
private $_nodeConditionFactory; | ||
private $_nodeCollectionFactory; | ||
private $_nodeContextFactory; | ||
private $_nodeValueFactory; | ||
protected $nodesCount = 0; | ||
|
||
private $_tree; | ||
protected $container; | ||
|
||
public function __construct(OperatorFactoryInterface $operatorFactory) | ||
public function __construct(ContainerInterface $container) | ||
{ | ||
$this->_nodeTreeFactory = new NodeTreeFactory(); | ||
$this->_nodeConditionFactory = new NodeConditionFactory(); | ||
$this->_nodeCollectionFactory = new NodeCollectionFactory(); | ||
$this->_nodeContextFactory = new NodeContextFactory($operatorFactory); | ||
$this->_nodeValueFactory = new NodeValueFactory(); | ||
$this->container = $container; | ||
} | ||
|
||
public function build(&$structure) | ||
{ | ||
if (!array_key_exists('node', $structure)) { | ||
throw new \InvalidArgumentException('The "node" property is absent!'); | ||
throw new InvalidArgumentException('The "node" property is absent!'); | ||
} | ||
|
||
$this->_nodesCount++; | ||
$this->nodesCount++; | ||
|
||
if ($structure['node'] === 'tree') { | ||
if ($this->_nodesCount !== 1) { | ||
throw new \LogicException('Node of type "Tree" must be a root node!'); | ||
} | ||
/** | ||
* Workaround for short syntax if the root node is omitted | ||
*/ | ||
if ($this->nodesCount === 1 && $structure['node'] !== Root::getNodeName()) { | ||
$structure = [ | ||
'node' => Root::getNodeName(), | ||
'rules' => $structure | ||
]; | ||
|
||
$this->_tree = $this->_nodeTreeFactory->build($this, $structure); | ||
$this->_tree->setNodes($this->_nodeTreeFactory->buildNodes($this, $structure)); | ||
return $this->_tree; | ||
} | ||
$this->nodesCount--; | ||
|
||
if ($structure['node'] === 'condition') { | ||
$node = $this->_nodeConditionFactory->build($this, $structure); | ||
} elseif ($structure['node'] === 'collection') { | ||
$node = $this->_nodeCollectionFactory->build($this, $structure); | ||
} elseif ($structure['node'] === 'context') { | ||
$node = $this->_nodeContextFactory->build($this, $structure); | ||
} elseif ($structure['node'] === 'value') { | ||
$node = $this->_nodeValueFactory->build($this, $structure); | ||
} else { | ||
throw new \InvalidArgumentException(sprintf('Unknown node type "%s"', $structure['node'])); | ||
return $this->build($structure); | ||
} | ||
|
||
if (null !== $this->_tree) { | ||
$node->setTree($this->_tree); | ||
if ($this->nodesCount !== 1 && $structure['node'] === Root::getNodeName()) { | ||
throw new LogicException('The node "Root" cannot be not root node!'); | ||
} | ||
|
||
return $node; | ||
$nodeFactoryResolver = $this->container->get(NodeFactoryResolverInterface::class); | ||
$nodeFactoryType = $nodeFactoryResolver->resolve($structure['node']); | ||
|
||
/** @var NodeFactoryInterface $nodeFactory */ | ||
$nodeFactory = $this->container->get($nodeFactoryType); | ||
return $nodeFactory->build($this, $structure); | ||
} | ||
|
||
public function getContainer(): ContainerInterface | ||
{ | ||
return $this->container; | ||
} | ||
|
||
public function setRoot(Root $rootNode): BuilderInterface | ||
{ | ||
$this->rootNode = $rootNode; | ||
return $this; | ||
} | ||
|
||
public function getRoot(): Root | ||
{ | ||
return $this->rootNode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace TheChoice\Builder; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use TheChoice\Node\Root; | ||
|
||
interface BuilderInterface | ||
{ | ||
public function build(&$structure); | ||
|
||
public function getContainer(): ContainerInterface; | ||
|
||
public function setRoot(Root $rootNode): BuilderInterface; | ||
|
||
public function getRoot(): Root; | ||
} |
Oops, something went wrong.