Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
58.33% |
7 / 12 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
NegatedNode | |
58.33% |
7 / 12 |
|
66.67% |
4 / 6 |
12.63 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
toArray | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
getChild | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
validNegationType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
3 | |||
getNegationType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
accept | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace CirrusSearch\Parser\AST; |
4 | |
5 | use CirrusSearch\Parser\AST\Visitor\Visitor; |
6 | use Wikimedia\Assert\Assert; |
7 | |
8 | /** |
9 | * Negated node |
10 | * NOTE: may not appear in any resulting query AST, |
11 | * NegatedNode are always removed and attached to a BooleanClause |
12 | * as MUST_NOT. |
13 | */ |
14 | class NegatedNode extends ParsedNode { |
15 | |
16 | /** |
17 | * @var ParsedNode |
18 | */ |
19 | private $child; |
20 | |
21 | /** |
22 | * @var string type of negation used |
23 | */ |
24 | private $negationType; |
25 | |
26 | /** |
27 | * @param int $startOffset |
28 | * @param int $endOffset |
29 | * @param ParsedNode $child |
30 | * @param string $negationType |
31 | */ |
32 | public function __construct( $startOffset, $endOffset, ParsedNode $child, $negationType ) { |
33 | Assert::parameter( self::validNegationType( $negationType ), 'negationType', 'Invalid negation type provided' ); |
34 | parent::__construct( $startOffset, $endOffset ); |
35 | $this->child = $child; |
36 | $this->negationType = $negationType; |
37 | } |
38 | |
39 | /** |
40 | * @return array |
41 | */ |
42 | public function toArray() { |
43 | return [ 'not' => array_merge( parent::baseParams(), [ |
44 | 'negation_type' => $this->negationType, |
45 | 'child' => $this->child->toArray(), |
46 | ] ) ]; |
47 | } |
48 | |
49 | /** |
50 | * @return ParsedNode |
51 | */ |
52 | public function getChild() { |
53 | return $this->child; |
54 | } |
55 | |
56 | /** |
57 | * Check whether this negation type is valid |
58 | * @param string $negationType |
59 | * @return bool |
60 | */ |
61 | public static function validNegationType( $negationType ) { |
62 | return $negationType === '!' || $negationType === '-' || $negationType === 'NOT'; |
63 | } |
64 | |
65 | /** |
66 | * @return string type of negation used (NOT, ! or -) |
67 | */ |
68 | public function getNegationType() { |
69 | return $this->negationType; |
70 | } |
71 | |
72 | /** |
73 | * @param Visitor $visitor |
74 | */ |
75 | public function accept( Visitor $visitor ) { |
76 | $visitor->visitNegatedNode( $this ); |
77 | } |
78 | } |