Passed
Push — master ( 97fd61...3b8f83 )
by Roeland
09:19 queued 10s
created
lib/private/Files/Cache/QuerySearchHelper.php 2 patches
Indentation   +179 added lines, -179 removed lines patch added patch discarded remove patch
@@ -34,183 +34,183 @@
 block discarded – undo
34 34
  * Tools for transforming search queries into database queries
35 35
  */
36 36
 class QuerySearchHelper {
37
-	static protected $searchOperatorMap = [
38
-		ISearchComparison::COMPARE_LIKE => 'iLike',
39
-		ISearchComparison::COMPARE_EQUAL => 'eq',
40
-		ISearchComparison::COMPARE_GREATER_THAN => 'gt',
41
-		ISearchComparison::COMPARE_GREATER_THAN_EQUAL => 'gte',
42
-		ISearchComparison::COMPARE_LESS_THAN => 'lt',
43
-		ISearchComparison::COMPARE_LESS_THAN_EQUAL => 'lte'
44
-	];
45
-
46
-	static protected $searchOperatorNegativeMap = [
47
-		ISearchComparison::COMPARE_LIKE => 'notLike',
48
-		ISearchComparison::COMPARE_EQUAL => 'neq',
49
-		ISearchComparison::COMPARE_GREATER_THAN => 'lte',
50
-		ISearchComparison::COMPARE_GREATER_THAN_EQUAL => 'lt',
51
-		ISearchComparison::COMPARE_LESS_THAN => 'gte',
52
-		ISearchComparison::COMPARE_LESS_THAN_EQUAL => 'lt'
53
-	];
54
-
55
-	const TAG_FAVORITE = '_$!<Favorite>!$_';
56
-
57
-	/** @var IMimeTypeLoader */
58
-	private $mimetypeLoader;
59
-
60
-	/**
61
-	 * QuerySearchUtil constructor.
62
-	 *
63
-	 * @param IMimeTypeLoader $mimetypeLoader
64
-	 */
65
-	public function __construct(IMimeTypeLoader $mimetypeLoader) {
66
-		$this->mimetypeLoader = $mimetypeLoader;
67
-	}
68
-
69
-	/**
70
-	 * Whether or not the tag tables should be joined to complete the search
71
-	 *
72
-	 * @param ISearchOperator $operator
73
-	 * @return boolean
74
-	 */
75
-	public function shouldJoinTags(ISearchOperator $operator) {
76
-		if ($operator instanceof ISearchBinaryOperator) {
77
-			return array_reduce($operator->getArguments(), function ($shouldJoin, ISearchOperator $operator) {
78
-				return $shouldJoin || $this->shouldJoinTags($operator);
79
-			}, false);
80
-		} else if ($operator instanceof ISearchComparison) {
81
-			return $operator->getField() === 'tagname' || $operator->getField() === 'favorite';
82
-		}
83
-		return false;
84
-	}
85
-
86
-	/**
87
-	 * @param IQueryBuilder $builder
88
-	 * @param ISearchOperator $operator
89
-	 */
90
-	public function searchOperatorArrayToDBExprArray(IQueryBuilder $builder, array $operators) {
91
-		return array_map(function ($operator) use ($builder) {
92
-			return $this->searchOperatorToDBExpr($builder, $operator);
93
-		}, $operators);
94
-	}
95
-
96
-	public function searchOperatorToDBExpr(IQueryBuilder $builder, ISearchOperator $operator) {
97
-		$expr = $builder->expr();
98
-		if ($operator instanceof ISearchBinaryOperator) {
99
-			switch ($operator->getType()) {
100
-				case ISearchBinaryOperator::OPERATOR_NOT:
101
-					$negativeOperator = $operator->getArguments()[0];
102
-					if ($negativeOperator instanceof ISearchComparison) {
103
-						return $this->searchComparisonToDBExpr($builder, $negativeOperator, self::$searchOperatorNegativeMap);
104
-					} else {
105
-						throw new \InvalidArgumentException('Binary operators inside "not" is not supported');
106
-					}
107
-				case ISearchBinaryOperator::OPERATOR_AND:
108
-					return call_user_func_array([$expr, 'andX'], $this->searchOperatorArrayToDBExprArray($builder, $operator->getArguments()));
109
-				case ISearchBinaryOperator::OPERATOR_OR:
110
-					return call_user_func_array([$expr, 'orX'], $this->searchOperatorArrayToDBExprArray($builder, $operator->getArguments()));
111
-				default:
112
-					throw new \InvalidArgumentException('Invalid operator type: ' . $operator->getType());
113
-			}
114
-		} else if ($operator instanceof ISearchComparison) {
115
-			return $this->searchComparisonToDBExpr($builder, $operator, self::$searchOperatorMap);
116
-		} else {
117
-			throw new \InvalidArgumentException('Invalid operator type: ' . get_class($operator));
118
-		}
119
-	}
120
-
121
-	private function searchComparisonToDBExpr(IQueryBuilder $builder, ISearchComparison $comparison, array $operatorMap) {
122
-		$this->validateComparison($comparison);
123
-
124
-		list($field, $value, $type) = $this->getOperatorFieldAndValue($comparison);
125
-		if (isset($operatorMap[$type])) {
126
-			$queryOperator = $operatorMap[$type];
127
-			return $builder->expr()->$queryOperator($field, $this->getParameterForValue($builder, $value));
128
-		} else {
129
-			throw new \InvalidArgumentException('Invalid operator type: ' . $comparison->getType());
130
-		}
131
-	}
132
-
133
-	private function getOperatorFieldAndValue(ISearchComparison $operator) {
134
-		$field = $operator->getField();
135
-		$value = $operator->getValue();
136
-		$type = $operator->getType();
137
-		if ($field === 'mimetype') {
138
-			if ($operator->getType() === ISearchComparison::COMPARE_EQUAL) {
139
-				$value = (int)$this->mimetypeLoader->getId($value);
140
-			} else if ($operator->getType() === ISearchComparison::COMPARE_LIKE) {
141
-				// transform "mimetype='foo/%'" to "mimepart='foo'"
142
-				if (preg_match('|(.+)/%|', $value, $matches)) {
143
-					$field = 'mimepart';
144
-					$value = (int)$this->mimetypeLoader->getId($matches[1]);
145
-					$type = ISearchComparison::COMPARE_EQUAL;
146
-				} else if (strpos($value, '%') !== false) {
147
-					throw new \InvalidArgumentException('Unsupported query value for mimetype: ' . $value . ', only values in the format "mime/type" or "mime/%" are supported');
148
-				} else {
149
-					$field = 'mimetype';
150
-					$value = (int)$this->mimetypeLoader->getId($value);
151
-					$type = ISearchComparison::COMPARE_EQUAL;
152
-				}
153
-			}
154
-		} else if ($field === 'favorite') {
155
-			$field = 'tag.category';
156
-			$value = self::TAG_FAVORITE;
157
-		} else if ($field === 'tagname') {
158
-			$field = 'tag.category';
159
-		}
160
-		return [$field, $value, $type];
161
-	}
162
-
163
-	private function validateComparison(ISearchComparison $operator) {
164
-		$types = [
165
-			'mimetype' => 'string',
166
-			'mtime' => 'integer',
167
-			'name' => 'string',
168
-			'size' => 'integer',
169
-			'tagname' => 'string',
170
-			'favorite' => 'boolean',
171
-			'fileid' => 'integer'
172
-		];
173
-		$comparisons = [
174
-			'mimetype' => ['eq', 'like'],
175
-			'mtime' => ['eq', 'gt', 'lt', 'gte', 'lte'],
176
-			'name' => ['eq', 'like'],
177
-			'size' => ['eq', 'gt', 'lt', 'gte', 'lte'],
178
-			'tagname' => ['eq', 'like'],
179
-			'favorite' => ['eq'],
180
-			'fileid' => ['eq']
181
-		];
182
-
183
-		if (!isset($types[$operator->getField()])) {
184
-			throw new \InvalidArgumentException('Unsupported comparison field ' . $operator->getField());
185
-		}
186
-		$type = $types[$operator->getField()];
187
-		if (gettype($operator->getValue()) !== $type) {
188
-			throw new \InvalidArgumentException('Invalid type for field ' . $operator->getField());
189
-		}
190
-		if (!in_array($operator->getType(), $comparisons[$operator->getField()])) {
191
-			throw new \InvalidArgumentException('Unsupported comparison for field  ' . $operator->getField() . ': ' . $operator->getType());
192
-		}
193
-	}
194
-
195
-	private function getParameterForValue(IQueryBuilder $builder, $value) {
196
-		if ($value instanceof \DateTime) {
197
-			$value = $value->getTimestamp();
198
-		}
199
-		if (is_numeric($value)) {
200
-			$type = IQueryBuilder::PARAM_INT;
201
-		} else {
202
-			$type = IQueryBuilder::PARAM_STR;
203
-		}
204
-		return $builder->createNamedParameter($value, $type);
205
-	}
206
-
207
-	/**
208
-	 * @param IQueryBuilder $query
209
-	 * @param ISearchOrder[] $orders
210
-	 */
211
-	public function addSearchOrdersToQuery(IQueryBuilder $query, array $orders) {
212
-		foreach ($orders as $order) {
213
-			$query->addOrderBy($order->getField(), $order->getDirection());
214
-		}
215
-	}
37
+    static protected $searchOperatorMap = [
38
+        ISearchComparison::COMPARE_LIKE => 'iLike',
39
+        ISearchComparison::COMPARE_EQUAL => 'eq',
40
+        ISearchComparison::COMPARE_GREATER_THAN => 'gt',
41
+        ISearchComparison::COMPARE_GREATER_THAN_EQUAL => 'gte',
42
+        ISearchComparison::COMPARE_LESS_THAN => 'lt',
43
+        ISearchComparison::COMPARE_LESS_THAN_EQUAL => 'lte'
44
+    ];
45
+
46
+    static protected $searchOperatorNegativeMap = [
47
+        ISearchComparison::COMPARE_LIKE => 'notLike',
48
+        ISearchComparison::COMPARE_EQUAL => 'neq',
49
+        ISearchComparison::COMPARE_GREATER_THAN => 'lte',
50
+        ISearchComparison::COMPARE_GREATER_THAN_EQUAL => 'lt',
51
+        ISearchComparison::COMPARE_LESS_THAN => 'gte',
52
+        ISearchComparison::COMPARE_LESS_THAN_EQUAL => 'lt'
53
+    ];
54
+
55
+    const TAG_FAVORITE = '_$!<Favorite>!$_';
56
+
57
+    /** @var IMimeTypeLoader */
58
+    private $mimetypeLoader;
59
+
60
+    /**
61
+     * QuerySearchUtil constructor.
62
+     *
63
+     * @param IMimeTypeLoader $mimetypeLoader
64
+     */
65
+    public function __construct(IMimeTypeLoader $mimetypeLoader) {
66
+        $this->mimetypeLoader = $mimetypeLoader;
67
+    }
68
+
69
+    /**
70
+     * Whether or not the tag tables should be joined to complete the search
71
+     *
72
+     * @param ISearchOperator $operator
73
+     * @return boolean
74
+     */
75
+    public function shouldJoinTags(ISearchOperator $operator) {
76
+        if ($operator instanceof ISearchBinaryOperator) {
77
+            return array_reduce($operator->getArguments(), function ($shouldJoin, ISearchOperator $operator) {
78
+                return $shouldJoin || $this->shouldJoinTags($operator);
79
+            }, false);
80
+        } else if ($operator instanceof ISearchComparison) {
81
+            return $operator->getField() === 'tagname' || $operator->getField() === 'favorite';
82
+        }
83
+        return false;
84
+    }
85
+
86
+    /**
87
+     * @param IQueryBuilder $builder
88
+     * @param ISearchOperator $operator
89
+     */
90
+    public function searchOperatorArrayToDBExprArray(IQueryBuilder $builder, array $operators) {
91
+        return array_map(function ($operator) use ($builder) {
92
+            return $this->searchOperatorToDBExpr($builder, $operator);
93
+        }, $operators);
94
+    }
95
+
96
+    public function searchOperatorToDBExpr(IQueryBuilder $builder, ISearchOperator $operator) {
97
+        $expr = $builder->expr();
98
+        if ($operator instanceof ISearchBinaryOperator) {
99
+            switch ($operator->getType()) {
100
+                case ISearchBinaryOperator::OPERATOR_NOT:
101
+                    $negativeOperator = $operator->getArguments()[0];
102
+                    if ($negativeOperator instanceof ISearchComparison) {
103
+                        return $this->searchComparisonToDBExpr($builder, $negativeOperator, self::$searchOperatorNegativeMap);
104
+                    } else {
105
+                        throw new \InvalidArgumentException('Binary operators inside "not" is not supported');
106
+                    }
107
+                case ISearchBinaryOperator::OPERATOR_AND:
108
+                    return call_user_func_array([$expr, 'andX'], $this->searchOperatorArrayToDBExprArray($builder, $operator->getArguments()));
109
+                case ISearchBinaryOperator::OPERATOR_OR:
110
+                    return call_user_func_array([$expr, 'orX'], $this->searchOperatorArrayToDBExprArray($builder, $operator->getArguments()));
111
+                default:
112
+                    throw new \InvalidArgumentException('Invalid operator type: ' . $operator->getType());
113
+            }
114
+        } else if ($operator instanceof ISearchComparison) {
115
+            return $this->searchComparisonToDBExpr($builder, $operator, self::$searchOperatorMap);
116
+        } else {
117
+            throw new \InvalidArgumentException('Invalid operator type: ' . get_class($operator));
118
+        }
119
+    }
120
+
121
+    private function searchComparisonToDBExpr(IQueryBuilder $builder, ISearchComparison $comparison, array $operatorMap) {
122
+        $this->validateComparison($comparison);
123
+
124
+        list($field, $value, $type) = $this->getOperatorFieldAndValue($comparison);
125
+        if (isset($operatorMap[$type])) {
126
+            $queryOperator = $operatorMap[$type];
127
+            return $builder->expr()->$queryOperator($field, $this->getParameterForValue($builder, $value));
128
+        } else {
129
+            throw new \InvalidArgumentException('Invalid operator type: ' . $comparison->getType());
130
+        }
131
+    }
132
+
133
+    private function getOperatorFieldAndValue(ISearchComparison $operator) {
134
+        $field = $operator->getField();
135
+        $value = $operator->getValue();
136
+        $type = $operator->getType();
137
+        if ($field === 'mimetype') {
138
+            if ($operator->getType() === ISearchComparison::COMPARE_EQUAL) {
139
+                $value = (int)$this->mimetypeLoader->getId($value);
140
+            } else if ($operator->getType() === ISearchComparison::COMPARE_LIKE) {
141
+                // transform "mimetype='foo/%'" to "mimepart='foo'"
142
+                if (preg_match('|(.+)/%|', $value, $matches)) {
143
+                    $field = 'mimepart';
144
+                    $value = (int)$this->mimetypeLoader->getId($matches[1]);
145
+                    $type = ISearchComparison::COMPARE_EQUAL;
146
+                } else if (strpos($value, '%') !== false) {
147
+                    throw new \InvalidArgumentException('Unsupported query value for mimetype: ' . $value . ', only values in the format "mime/type" or "mime/%" are supported');
148
+                } else {
149
+                    $field = 'mimetype';
150
+                    $value = (int)$this->mimetypeLoader->getId($value);
151
+                    $type = ISearchComparison::COMPARE_EQUAL;
152
+                }
153
+            }
154
+        } else if ($field === 'favorite') {
155
+            $field = 'tag.category';
156
+            $value = self::TAG_FAVORITE;
157
+        } else if ($field === 'tagname') {
158
+            $field = 'tag.category';
159
+        }
160
+        return [$field, $value, $type];
161
+    }
162
+
163
+    private function validateComparison(ISearchComparison $operator) {
164
+        $types = [
165
+            'mimetype' => 'string',
166
+            'mtime' => 'integer',
167
+            'name' => 'string',
168
+            'size' => 'integer',
169
+            'tagname' => 'string',
170
+            'favorite' => 'boolean',
171
+            'fileid' => 'integer'
172
+        ];
173
+        $comparisons = [
174
+            'mimetype' => ['eq', 'like'],
175
+            'mtime' => ['eq', 'gt', 'lt', 'gte', 'lte'],
176
+            'name' => ['eq', 'like'],
177
+            'size' => ['eq', 'gt', 'lt', 'gte', 'lte'],
178
+            'tagname' => ['eq', 'like'],
179
+            'favorite' => ['eq'],
180
+            'fileid' => ['eq']
181
+        ];
182
+
183
+        if (!isset($types[$operator->getField()])) {
184
+            throw new \InvalidArgumentException('Unsupported comparison field ' . $operator->getField());
185
+        }
186
+        $type = $types[$operator->getField()];
187
+        if (gettype($operator->getValue()) !== $type) {
188
+            throw new \InvalidArgumentException('Invalid type for field ' . $operator->getField());
189
+        }
190
+        if (!in_array($operator->getType(), $comparisons[$operator->getField()])) {
191
+            throw new \InvalidArgumentException('Unsupported comparison for field  ' . $operator->getField() . ': ' . $operator->getType());
192
+        }
193
+    }
194
+
195
+    private function getParameterForValue(IQueryBuilder $builder, $value) {
196
+        if ($value instanceof \DateTime) {
197
+            $value = $value->getTimestamp();
198
+        }
199
+        if (is_numeric($value)) {
200
+            $type = IQueryBuilder::PARAM_INT;
201
+        } else {
202
+            $type = IQueryBuilder::PARAM_STR;
203
+        }
204
+        return $builder->createNamedParameter($value, $type);
205
+    }
206
+
207
+    /**
208
+     * @param IQueryBuilder $query
209
+     * @param ISearchOrder[] $orders
210
+     */
211
+    public function addSearchOrdersToQuery(IQueryBuilder $query, array $orders) {
212
+        foreach ($orders as $order) {
213
+            $query->addOrderBy($order->getField(), $order->getDirection());
214
+        }
215
+    }
216 216
 }
Please login to merge, or discard this patch.
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -74,7 +74,7 @@  discard block
 block discarded – undo
74 74
 	 */
75 75
 	public function shouldJoinTags(ISearchOperator $operator) {
76 76
 		if ($operator instanceof ISearchBinaryOperator) {
77
-			return array_reduce($operator->getArguments(), function ($shouldJoin, ISearchOperator $operator) {
77
+			return array_reduce($operator->getArguments(), function($shouldJoin, ISearchOperator $operator) {
78 78
 				return $shouldJoin || $this->shouldJoinTags($operator);
79 79
 			}, false);
80 80
 		} else if ($operator instanceof ISearchComparison) {
@@ -88,7 +88,7 @@  discard block
 block discarded – undo
88 88
 	 * @param ISearchOperator $operator
89 89
 	 */
90 90
 	public function searchOperatorArrayToDBExprArray(IQueryBuilder $builder, array $operators) {
91
-		return array_map(function ($operator) use ($builder) {
91
+		return array_map(function($operator) use ($builder) {
92 92
 			return $this->searchOperatorToDBExpr($builder, $operator);
93 93
 		}, $operators);
94 94
 	}
@@ -109,12 +109,12 @@  discard block
 block discarded – undo
109 109
 				case ISearchBinaryOperator::OPERATOR_OR:
110 110
 					return call_user_func_array([$expr, 'orX'], $this->searchOperatorArrayToDBExprArray($builder, $operator->getArguments()));
111 111
 				default:
112
-					throw new \InvalidArgumentException('Invalid operator type: ' . $operator->getType());
112
+					throw new \InvalidArgumentException('Invalid operator type: '.$operator->getType());
113 113
 			}
114 114
 		} else if ($operator instanceof ISearchComparison) {
115 115
 			return $this->searchComparisonToDBExpr($builder, $operator, self::$searchOperatorMap);
116 116
 		} else {
117
-			throw new \InvalidArgumentException('Invalid operator type: ' . get_class($operator));
117
+			throw new \InvalidArgumentException('Invalid operator type: '.get_class($operator));
118 118
 		}
119 119
 	}
120 120
 
@@ -126,7 +126,7 @@  discard block
 block discarded – undo
126 126
 			$queryOperator = $operatorMap[$type];
127 127
 			return $builder->expr()->$queryOperator($field, $this->getParameterForValue($builder, $value));
128 128
 		} else {
129
-			throw new \InvalidArgumentException('Invalid operator type: ' . $comparison->getType());
129
+			throw new \InvalidArgumentException('Invalid operator type: '.$comparison->getType());
130 130
 		}
131 131
 	}
132 132
 
@@ -136,18 +136,18 @@  discard block
 block discarded – undo
136 136
 		$type = $operator->getType();
137 137
 		if ($field === 'mimetype') {
138 138
 			if ($operator->getType() === ISearchComparison::COMPARE_EQUAL) {
139
-				$value = (int)$this->mimetypeLoader->getId($value);
139
+				$value = (int) $this->mimetypeLoader->getId($value);
140 140
 			} else if ($operator->getType() === ISearchComparison::COMPARE_LIKE) {
141 141
 				// transform "mimetype='foo/%'" to "mimepart='foo'"
142 142
 				if (preg_match('|(.+)/%|', $value, $matches)) {
143 143
 					$field = 'mimepart';
144
-					$value = (int)$this->mimetypeLoader->getId($matches[1]);
144
+					$value = (int) $this->mimetypeLoader->getId($matches[1]);
145 145
 					$type = ISearchComparison::COMPARE_EQUAL;
146 146
 				} else if (strpos($value, '%') !== false) {
147
-					throw new \InvalidArgumentException('Unsupported query value for mimetype: ' . $value . ', only values in the format "mime/type" or "mime/%" are supported');
147
+					throw new \InvalidArgumentException('Unsupported query value for mimetype: '.$value.', only values in the format "mime/type" or "mime/%" are supported');
148 148
 				} else {
149 149
 					$field = 'mimetype';
150
-					$value = (int)$this->mimetypeLoader->getId($value);
150
+					$value = (int) $this->mimetypeLoader->getId($value);
151 151
 					$type = ISearchComparison::COMPARE_EQUAL;
152 152
 				}
153 153
 			}
@@ -181,14 +181,14 @@  discard block
 block discarded – undo
181 181
 		];
182 182
 
183 183
 		if (!isset($types[$operator->getField()])) {
184
-			throw new \InvalidArgumentException('Unsupported comparison field ' . $operator->getField());
184
+			throw new \InvalidArgumentException('Unsupported comparison field '.$operator->getField());
185 185
 		}
186 186
 		$type = $types[$operator->getField()];
187 187
 		if (gettype($operator->getValue()) !== $type) {
188
-			throw new \InvalidArgumentException('Invalid type for field ' . $operator->getField());
188
+			throw new \InvalidArgumentException('Invalid type for field '.$operator->getField());
189 189
 		}
190 190
 		if (!in_array($operator->getType(), $comparisons[$operator->getField()])) {
191
-			throw new \InvalidArgumentException('Unsupported comparison for field  ' . $operator->getField() . ': ' . $operator->getType());
191
+			throw new \InvalidArgumentException('Unsupported comparison for field  '.$operator->getField().': '.$operator->getType());
192 192
 		}
193 193
 	}
194 194
 
Please login to merge, or discard this patch.