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