1 | <?php namespace Cornford\Logical; |
||
9 | class Logical extends LogicalAbstract implements LogicalInterface { |
||
10 | |||
11 | /** |
||
12 | * The temporary logic method. |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $tempLogic; |
||
17 | |||
18 | /** |
||
19 | * The temporary logic method. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $tempMethod; |
||
24 | |||
25 | /** |
||
26 | * The temporary logic expected. |
||
27 | * |
||
28 | * @var string|array |
||
29 | */ |
||
30 | protected $tempExpected; |
||
31 | |||
32 | /** |
||
33 | * The temporary logic field. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $tempField; |
||
38 | |||
39 | /** |
||
40 | * The temporary results. |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $tempResults; |
||
45 | |||
46 | /** |
||
47 | * Decode the logic string into an array of logical statements. |
||
48 | * |
||
49 | * @return boolean |
||
50 | */ |
||
51 | protected function decodeLogicStatements() |
||
52 | { |
||
53 | if (!$this->getLogic()) { |
||
54 | return true; |
||
55 | } |
||
56 | |||
57 | $orStatements = preg_split("/.OR./", $this->getLogic()); |
||
58 | |||
59 | foreach ($orStatements as $orKey => $orStatement) { |
||
60 | $andStatements = preg_split("/.AND./", $orStatement); |
||
61 | |||
62 | foreach ($andStatements as $andStatement) { |
||
63 | $items = preg_split("/\./", $andStatement, 2); |
||
64 | $decodedStatement = []; |
||
65 | |||
66 | foreach ($items as $item) { |
||
67 | $matches = null; |
||
68 | if (stristr($item, 'where')) { |
||
69 | if (!preg_match("/\((?<field>.*)\)/", $item, $matches)) { |
||
70 | return false; |
||
71 | } |
||
72 | |||
73 | $this->tempField = trim(trim($matches['field'], '\''), '"'); |
||
74 | |||
75 | continue; |
||
76 | } |
||
77 | |||
78 | if (!preg_match("/^(?<method>[a-zA-Z]{1,})\((?<expected>[\'\"]*.*[\'\"]*)\)/", $item, $matches)) { |
||
79 | return false; |
||
80 | } else { |
||
81 | $this->tempMethod = trim(trim($matches['method'], '\''), '"'); |
||
82 | $this->tempExpected = trim(trim($matches['expected'], '\''), '"'); |
||
83 | } |
||
84 | |||
85 | if (stristr($this->tempExpected, ',') && !stristr($this->tempExpected, '{') && !stristr($this->tempExpected, '}')) { |
||
86 | $this->tempExpected = explode(', ', $this->tempExpected); |
||
87 | |||
88 | if (is_array($this->tempExpected)) { |
||
89 | foreach ($this->tempExpected as &$expected) { |
||
90 | $expected = trim(trim($expected, '\''), '"'); |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | |||
95 | if (!is_array($this->tempExpected) && stristr($this->tempExpected, '{') && stristr($this->tempExpected, '}')) { |
||
96 | $this->tempExpected = eval(str_replace('{', 'return (', str_replace('}', ');', $this->tempExpected))); |
||
97 | } elseif(is_array($this->tempExpected)) { |
||
98 | foreach ($this->tempExpected as &$expected) { |
||
99 | if (stristr($expected, '{') && stristr($expected, '}')) { |
||
100 | $expected = eval(str_replace('{', 'return (', str_replace('}', ');', $expected))); |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | |||
105 | $decodedStatement[$orKey]['method'] = $this->tempMethod; |
||
106 | $decodedStatement[$orKey]['expected'] = $this->tempExpected; |
||
107 | $decodedStatement[$orKey]['field'] = $this->tempField; |
||
108 | } |
||
109 | |||
110 | $this->setDecodedLogicStatement($decodedStatement); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | return true; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Execute a logic statement method on an input value against an expected value. |
||
119 | * |
||
120 | * @param string $method |
||
121 | * @param string|integer|boolean $input |
||
122 | * @param string|integer|boolean $expected |
||
123 | * |
||
124 | * @throws LogicalExecutionException |
||
125 | * |
||
126 | * @return boolean |
||
127 | */ |
||
128 | protected function executeLogicStatement($method, $input, $expected = null) |
||
147 | |||
148 | /** |
||
149 | * Decodes logic input into statements and executes each statement removing un-matching results. |
||
150 | * |
||
151 | * @throws LogicalDecodingException |
||
152 | * @throws LogicalFieldValueException |
||
153 | * |
||
154 | * @return self |
||
155 | */ |
||
156 | public function execute() |
||
196 | |||
197 | } |