Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
25 / 25 |
|
100.00% |
20 / 20 |
CRAP | |
100.00% |
1 / 1 |
HtmlExpressionBuilder | |
100.00% |
25 / 25 |
|
100.00% |
20 / 20 |
22 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
getSupportedTokenType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
parameter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
string | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isNull | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
eq | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
neq | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
gt | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
gte | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
lt | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
lte | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
in | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
notIn | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
contains | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
notContains | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
andX | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
nandX | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
orX | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
norX | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
xorX | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Symftony\Xpression\Expr; |
6 | |
7 | use Symftony\Xpression\Lexer; |
8 | |
9 | class HtmlExpressionBuilder implements ExpressionBuilderInterface |
10 | { |
11 | /** |
12 | * This callable is use to delegate the html generation to a third party |
13 | * eg: $comparisonHtmlBuilder($field, $operator, $value) |
14 | * it must return the html code. |
15 | * |
16 | * @var callable |
17 | */ |
18 | private $comparisonHtmlBuilder; |
19 | |
20 | /** |
21 | * This callable is use to delegate the html generation to a third party |
22 | * eg: $compositeHtmlBuilder(array $expressions, $type) |
23 | * it must return the html code. |
24 | * |
25 | * @var callable |
26 | */ |
27 | private $compositeHtmlBuilder; |
28 | |
29 | public function __construct(callable $comparisonHtmlBuilder = null, callable $compositeHtmlBuilder = null) |
30 | { |
31 | $this->comparisonHtmlBuilder = $comparisonHtmlBuilder ?: static fn ($field, $operator, $value) => sprintf('<div>%s %s %s</div>', $field, $operator, $value); |
32 | $this->compositeHtmlBuilder = $compositeHtmlBuilder ?: static fn (array $expressions, $type) => str_replace( |
33 | ['{type}', '{expressions}'], |
34 | [$type, implode('', $expressions)], |
35 | '<fieldset><legend>{type}</legend>{expressions}</fieldset>' |
36 | ); |
37 | } |
38 | |
39 | public function getSupportedTokenType(): int |
40 | { |
41 | return Lexer::T_ALL; |
42 | } |
43 | |
44 | public function parameter(mixed $value, bool $isValue = false): mixed |
45 | { |
46 | return $value; |
47 | } |
48 | |
49 | public function string(mixed $value): mixed |
50 | { |
51 | return '"'.$value.'"'; |
52 | } |
53 | |
54 | public function isNull(string $field): mixed |
55 | { |
56 | return ($this->comparisonHtmlBuilder)($field, 'is', 'null'); |
57 | } |
58 | |
59 | public function eq(string $field, mixed $value): mixed |
60 | { |
61 | return ($this->comparisonHtmlBuilder)($field, '=', $value); |
62 | } |
63 | |
64 | public function neq(string $field, mixed $value): mixed |
65 | { |
66 | return ($this->comparisonHtmlBuilder)($field, '≠', $value); |
67 | } |
68 | |
69 | public function gt(string $field, mixed $value): mixed |
70 | { |
71 | return ($this->comparisonHtmlBuilder)($field, '>', $value); |
72 | } |
73 | |
74 | public function gte(string $field, mixed $value): mixed |
75 | { |
76 | return ($this->comparisonHtmlBuilder)($field, '≥', $value); |
77 | } |
78 | |
79 | public function lt(string $field, mixed $value): mixed |
80 | { |
81 | return ($this->comparisonHtmlBuilder)($field, '<', $value); |
82 | } |
83 | |
84 | public function lte(string $field, mixed $value): mixed |
85 | { |
86 | return ($this->comparisonHtmlBuilder)($field, '≤', $value); |
87 | } |
88 | |
89 | public function in(string $field, array $values): mixed |
90 | { |
91 | return ($this->comparisonHtmlBuilder)($field, 'value in', implode(', ', $values)); |
92 | } |
93 | |
94 | public function notIn(string $field, array $values): mixed |
95 | { |
96 | return ($this->comparisonHtmlBuilder)($field, 'value not in', implode(', ', $values)); |
97 | } |
98 | |
99 | public function contains(string $field, mixed $value): mixed |
100 | { |
101 | return ($this->comparisonHtmlBuilder)($field, 'contains', $value); |
102 | } |
103 | |
104 | public function notContains(string $field, mixed $value): mixed |
105 | { |
106 | return ($this->comparisonHtmlBuilder)($field, 'notContains', $value); |
107 | } |
108 | |
109 | public function andX(array $expressions): mixed |
110 | { |
111 | return ($this->compositeHtmlBuilder)($expressions, 'and'); |
112 | } |
113 | |
114 | public function nandX(array $expressions): mixed |
115 | { |
116 | return ($this->compositeHtmlBuilder)($expressions, 'not-and'); |
117 | } |
118 | |
119 | public function orX(array $expressions): mixed |
120 | { |
121 | return ($this->compositeHtmlBuilder)($expressions, 'or'); |
122 | } |
123 | |
124 | public function norX(array $expressions): mixed |
125 | { |
126 | return ($this->compositeHtmlBuilder)($expressions, 'not-or'); |
127 | } |
128 | |
129 | public function xorX(array $expressions): mixed |
130 | { |
131 | return ($this->compositeHtmlBuilder)($expressions, 'exclusive-or'); |
132 | } |
133 | } |