1
|
|
|
<?php |
2
|
|
|
namespace JClaveau\LogicalFilter\Rule; |
3
|
|
|
use JClaveau\LogicalFilter\FilteredValue; |
4
|
|
|
use JClaveau\LogicalFilter\FilteredKey; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Atomic rules are those who cannot be simplified (so already are): |
8
|
|
|
* + null |
9
|
|
|
* + not null |
10
|
|
|
* + equal |
11
|
|
|
* + above |
12
|
|
|
* + below |
13
|
|
|
* Atomic rules are related to a field. |
14
|
|
|
*/ |
15
|
|
|
abstract class AbstractAtomicRule extends AbstractRule |
16
|
|
|
{ |
17
|
|
|
use Trait_RuleWithField; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param array $options + show_instance=false Display the operator of the rule or its instance id |
21
|
|
|
* |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
public function toArray(array $options=[]) |
25
|
|
|
{ |
26
|
|
|
$default_options = [ |
27
|
|
|
'show_instance' => false, |
28
|
|
|
]; |
29
|
|
|
foreach ($default_options as $default_option => &$default_value) { |
30
|
|
|
if (!isset($options[ $default_option ])) |
31
|
|
|
$options[ $default_option ] = $default_value; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
if (!$options['show_instance'] && !empty($this->cache['array'])) |
36
|
|
|
return $this->cache['array']; |
37
|
|
|
|
38
|
|
|
$class = get_class($this); |
39
|
|
|
|
40
|
|
|
$array = [ |
41
|
|
|
$this->getField(), |
42
|
|
|
$options['show_instance'] ? $this->getInstanceId() : $class::operator, |
43
|
|
|
$this->getValues(), |
|
|
|
|
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
if (!$options['show_instance']) |
47
|
|
|
return $this->cache['array'] = $array; |
48
|
|
|
else |
49
|
|
|
return $array; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
*/ |
54
|
|
|
public function toString(array $options=[]) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
if (!empty($this->cache['string'])) |
57
|
|
|
return $this->cache['string']; |
58
|
|
|
|
59
|
|
|
$class = get_class($this); |
60
|
|
|
$operator = $class::operator; |
61
|
|
|
|
62
|
|
|
$stringified_value = var_export($this->getValues(), true); |
63
|
|
|
|
64
|
|
|
$field = $this->getField(); |
65
|
|
|
|
66
|
|
|
if ($field instanceof FilteredValue || $field instanceof FilteredKey) |
|
|
|
|
67
|
|
|
$field = "$field"; |
68
|
|
|
elseif ($field instanceof \Closure) |
|
|
|
|
69
|
|
|
throw new \Exception("Closures dump not implemented"); |
70
|
|
|
else |
71
|
|
|
$field = "'$field'"; |
72
|
|
|
|
73
|
|
|
return $this->cache['string'] = "[$field, '$operator', $stringified_value]"; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/**/ |
77
|
|
|
} |
78
|
|
|
|