1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Trait_RuleWithField |
4
|
|
|
* |
5
|
|
|
* @package php-logical-filter |
6
|
|
|
* @author Jean Claveau |
7
|
|
|
*/ |
8
|
|
|
namespace JClaveau\LogicalFilter\Rule; |
9
|
|
|
|
10
|
|
|
trait Trait_RuleWithField |
11
|
|
|
{ |
12
|
|
|
/** @var string $field The field to apply the rule on */ |
13
|
|
|
protected $field; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @return string $field |
17
|
|
|
*/ |
18
|
301 |
|
final public function getField() |
19
|
|
|
{ |
20
|
301 |
|
return $this->field; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return string $field |
|
|
|
|
25
|
|
|
*/ |
26
|
9 |
|
final public function setField( $field ) |
27
|
|
|
{ |
28
|
9 |
|
if ( ! is_scalar($field)) { |
29
|
|
|
throw new \InvalidArgumentEXception( |
30
|
|
|
"\$field property of a logical rule must be a scalar contrary to: " |
31
|
|
|
.var_export($field, true) |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
9 |
|
if ($this->field != $field) { |
36
|
4 |
|
$this->field = $field; |
37
|
4 |
|
$this->flushCache(); |
|
|
|
|
38
|
4 |
|
} |
39
|
|
|
|
40
|
9 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Changes the field property of the rule. |
45
|
|
|
* |
46
|
|
|
* @param array|callable Associative array of renamings or callable |
47
|
|
|
* that would rename the fields. |
48
|
|
|
* |
49
|
|
|
* @return AbstractAtomicRule $this |
|
|
|
|
50
|
|
|
*/ |
51
|
|
|
final public function renameField($renamings) |
52
|
|
|
{ |
53
|
|
|
$this->renameFields_andReturnIsChanged($renamings); |
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Changes the field property of the rule. |
59
|
|
|
* |
60
|
|
|
* @param array|callable Associative array of renamings or callable |
61
|
|
|
* that would rename the fields. |
62
|
|
|
* |
63
|
|
|
* @return bool Whether or not the field changed |
64
|
|
|
*/ |
65
|
1 |
|
final public function renameFields_andReturnIsChanged($renamings) |
66
|
|
|
{ |
67
|
1 |
|
$old_field = $this->field; |
68
|
|
|
|
69
|
1 |
|
if (is_callable($renamings)) { |
70
|
1 |
|
$this->setField( call_user_func($renamings, $this->field) ); |
71
|
1 |
|
} |
72
|
1 |
|
elseif (is_array($renamings)) { |
73
|
1 |
|
if (isset($renamings[$this->field])) { |
74
|
1 |
|
$this->setField( $renamings[$this->field] ); |
75
|
1 |
|
} |
76
|
1 |
|
} |
77
|
|
|
else { |
78
|
1 |
|
throw new \InvalidArgumentException( |
79
|
|
|
"\$renamings MUST be a callable or an associative array " |
80
|
1 |
|
."instead of: " . var_export($renamings, true) |
81
|
1 |
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
if ($old_field != $this->field) { |
85
|
1 |
|
$this->flushCache(); |
|
|
|
|
86
|
1 |
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// TODO remove this forced cache flushing ONLY when carefully |
90
|
|
|
// unit tested |
91
|
1 |
|
$this->flushCache(); |
|
|
|
|
92
|
1 |
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/**/ |
96
|
|
|
} |
97
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.