1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright © 2018 Thomas Klein, All rights reserved. |
4
|
|
|
* See LICENSE bundled with this library for license details. |
5
|
|
|
*/ |
6
|
|
|
declare(strict_types=1); |
7
|
|
|
|
8
|
|
|
namespace LogicTree; |
9
|
|
|
|
10
|
|
|
use LogicTree\Node\NodeInterface; |
11
|
|
|
use LogicTree\Operator\OperatorInterface; |
12
|
|
|
use LogicTree\Operator\OperatorPool; |
13
|
|
|
use LogicTree\Service\ConditionManager; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class LogicTreeFacade |
17
|
|
|
*/ |
18
|
|
|
class LogicTreeFacade |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Combine Conditions Manager |
22
|
|
|
* |
23
|
|
|
* @var \LogicTree\Service\ConditionManager |
24
|
|
|
*/ |
25
|
|
|
private $conditionManager; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Operator Pool |
29
|
|
|
* |
30
|
|
|
* @var \LogicTree\Operator\OperatorPool |
31
|
|
|
*/ |
32
|
|
|
private $operatorPool; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* LogicTreeFacade constructor |
36
|
|
|
* |
37
|
|
|
* @param \LogicTree\Service\ConditionManager|null $conditionManager |
38
|
|
|
*/ |
39
|
|
|
public function __construct(?ConditionManager $conditionManager = null) |
40
|
|
|
{ |
41
|
|
|
$this->operatorPool = new OperatorPool(); |
42
|
|
|
$this->conditionManager = $conditionManager ?? new ConditionManager($this->operatorPool); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Add a new Operator to the interpreter |
47
|
|
|
* |
48
|
|
|
* @param string $type |
49
|
|
|
* @param string $operatorCode |
50
|
|
|
* @param \LogicTree\Operator\OperatorInterface $operator |
51
|
|
|
* @return \LogicTree\LogicTreeFacade |
52
|
|
|
*/ |
53
|
|
|
public function addOperator(string $type, string $operatorCode, OperatorInterface $operator): LogicTreeFacade |
54
|
|
|
{ |
55
|
|
|
$this->operatorPool->addOperator($type, $operatorCode, $operator); |
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Create a new data source |
61
|
|
|
* |
62
|
|
|
* @param iterable $data |
63
|
|
|
* @return \LogicTree\DataSource |
64
|
|
|
*/ |
65
|
|
|
public function createDataSource(iterable $data): DataSource |
66
|
|
|
{ |
67
|
|
|
return new DataSource($data); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Execute the logic tree structure conditions |
72
|
|
|
* |
73
|
|
|
* @param \LogicTree\Node\NodeInterface $node |
74
|
|
|
* @param \LogicTree\DataSource $dataSource |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function executeCombineConditions(NodeInterface $node, DataSource $dataSource): bool |
78
|
|
|
{ |
79
|
|
|
return $this->conditionManager->execute($node, $dataSource); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Execute the logic tree structure conditions |
84
|
|
|
* |
85
|
|
|
* @param string $format |
86
|
|
|
* @param mixed $node |
87
|
|
|
* @param iterable $dataSource |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function executeCombineConditionsFormat(string $format, $node, iterable $dataSource): bool |
91
|
|
|
{ |
92
|
|
|
return $this->conditionManager->execute( |
93
|
|
|
$this->convertFormat($format, $node), |
94
|
|
|
$this->createDataSource($dataSource) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Transform an array conditions to a combine conditions object |
100
|
|
|
* |
101
|
|
|
* @param string $format |
102
|
|
|
* @param mixed $node |
103
|
|
|
* @return \LogicTree\Node\NodeInterface |
104
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
105
|
|
|
*/ |
106
|
|
|
public function convertFormat( |
107
|
|
|
/** @scrutinizer ignore-unused */ string $format, |
108
|
|
|
/** @scrutinizer ignore-unused */ $node |
109
|
|
|
): NodeInterface { |
110
|
|
|
//todo implement method. |
111
|
|
|
throw new \LogicException('Not implemented yet!'); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|