22
22
/**
23
23
* Filters the collection by boolean values.
24
24
*
25
+ * Filters collection on equality of boolean properties. The value is specified
26
+ * as one of ( "true" | "false" | "1" | "0" ) in the query.
27
+ *
28
+ * For each property passed, if the resource does not have such property or if
29
+ * the value is not one of ( "true" | "false" | "1" | "0" ) the property is ignored.
30
+ *
25
31
* @author Amrouche Hamza <[email protected] >
26
32
* @author Teoh Han Hui <[email protected] >
27
33
*/
28
34
class BooleanFilter extends AbstractFilter
29
35
{
30
- private $ requestStack ;
31
-
32
36
public function __construct (ManagerRegistry $ managerRegistry , RequestStack $ requestStack , LoggerInterface $ logger = null , array $ properties = null )
33
37
{
34
- parent ::__construct ($ managerRegistry , $ logger , $ properties );
35
-
36
- $ this ->requestStack = $ requestStack ;
37
- }
38
-
39
- /**
40
- * {@inheritdoc}
41
- *
42
- * Filters collection on equality of boolean properties. The value is specified as one of
43
- * ( "true" | "false" | "1" | "0" ) in the query.
44
- *
45
- * For each property passed, if the resource does not have such property or if the value is not one of
46
- * ( "true" | "false" | "1" | "0" ) the property is ignored.
47
- */
48
- public function apply (QueryBuilder $ queryBuilder , QueryNameGeneratorInterface $ queryNameGenerator , string $ resourceClass , string $ operationName = null )
49
- {
50
- $ request = $ this ->requestStack ->getCurrentRequest ();
51
- if (null === $ request ) {
52
- return ;
53
- }
54
-
55
- $ properties = $ this ->extractProperties ($ request );
56
-
57
- foreach ($ properties as $ property => $ value ) {
58
- if (
59
- !$ this ->isPropertyEnabled ($ property ) ||
60
- !$ this ->isPropertyMapped ($ property , $ resourceClass ) ||
61
- !$ this ->isBooleanField ($ property , $ resourceClass )
62
- ) {
63
- continue ;
64
- }
65
-
66
- if (in_array ($ value , ['true ' , '1 ' ], true )) {
67
- $ value = true ;
68
- } elseif (in_array ($ value , ['false ' , '0 ' ], true )) {
69
- $ value = false ;
70
- } else {
71
- $ this ->logger ->notice ('Invalid filter ignored ' , [
72
- 'exception ' => new InvalidArgumentException (sprintf ('Invalid boolean value for "%s" property, expected one of ( "%s" ) ' , $ property , implode ('" | " ' , [
73
- 'true ' ,
74
- 'false ' ,
75
- '1 ' ,
76
- '0 ' ,
77
- ]))),
78
- ]);
79
-
80
- continue ;
81
- }
82
-
83
- $ alias = 'o ' ;
84
- $ field = $ property ;
85
-
86
- if ($ this ->isPropertyNested ($ property )) {
87
- list ($ alias , $ field ) = $ this ->addJoinsForNestedProperty ($ property , $ alias , $ queryBuilder , $ queryNameGenerator );
88
- }
89
- $ valueParameter = $ queryNameGenerator ->generateParameterName ($ field );
90
-
91
- $ queryBuilder
92
- ->andWhere (sprintf ('%s.%s = :%s ' , $ alias , $ field , $ valueParameter ))
93
- ->setParameter ($ valueParameter , $ value );
94
- }
38
+ parent ::__construct ($ managerRegistry , $ requestStack , $ logger , $ properties );
95
39
}
96
40
97
41
/**
@@ -121,6 +65,49 @@ public function getDescription(string $resourceClass) : array
121
65
return $ description ;
122
66
}
123
67
68
+ /**
69
+ * {@inheritdoc}
70
+ */
71
+ protected function filterProperty (string $ property , $ value , QueryBuilder $ queryBuilder , QueryNameGeneratorInterface $ queryNameGenerator , string $ resourceClass , string $ operationName = null )
72
+ {
73
+ if (
74
+ !$ this ->isPropertyEnabled ($ property ) ||
75
+ !$ this ->isPropertyMapped ($ property , $ resourceClass ) ||
76
+ !$ this ->isBooleanField ($ property , $ resourceClass )
77
+ ) {
78
+ return ;
79
+ }
80
+
81
+ if (in_array ($ value , ['true ' , '1 ' ], true )) {
82
+ $ value = true ;
83
+ } elseif (in_array ($ value , ['false ' , '0 ' ], true )) {
84
+ $ value = false ;
85
+ } else {
86
+ $ this ->logger ->notice ('Invalid filter ignored ' , [
87
+ 'exception ' => new InvalidArgumentException (sprintf ('Invalid boolean value for "%s" property, expected one of ( "%s" ) ' , $ property , implode ('" | " ' , [
88
+ 'true ' ,
89
+ 'false ' ,
90
+ '1 ' ,
91
+ '0 ' ,
92
+ ]))),
93
+ ]);
94
+
95
+ return ;
96
+ }
97
+
98
+ $ alias = 'o ' ;
99
+ $ field = $ property ;
100
+
101
+ if ($ this ->isPropertyNested ($ property )) {
102
+ list ($ alias , $ field ) = $ this ->addJoinsForNestedProperty ($ property , $ alias , $ queryBuilder , $ queryNameGenerator );
103
+ }
104
+ $ valueParameter = $ queryNameGenerator ->generateParameterName ($ field );
105
+
106
+ $ queryBuilder
107
+ ->andWhere (sprintf ('%s.%s = :%s ' , $ alias , $ field , $ valueParameter ))
108
+ ->setParameter ($ valueParameter , $ value );
109
+ }
110
+
124
111
/**
125
112
* Determines whether the given property refers to a boolean field.
126
113
*
@@ -129,7 +116,7 @@ public function getDescription(string $resourceClass) : array
129
116
*
130
117
* @return bool
131
118
*/
132
- private function isBooleanField (string $ property , string $ resourceClass ) : bool
119
+ protected function isBooleanField (string $ property , string $ resourceClass ) : bool
133
120
{
134
121
$ propertyParts = $ this ->splitPropertyParts ($ property );
135
122
$ metadata = $ this ->getNestedMetadata ($ resourceClass , $ propertyParts ['associations ' ]);
0 commit comments