Skip to content
This repository
Browse code

BUGFIX: Ensure that DataList::filter() doesn't modify the original ob…

…ject.
  • Loading branch information...
commit e19ab7aa5acf39b84f4f7b4f45300617c97acf79 1 parent 0821f02
Sam Minnée authored May 30, 2012

Showing 1 changed file with 17 additions and 9 deletions. Show diff stats Hide diff stats

  1. 26  model/DataList.php
26  model/DataList.php
@@ -220,18 +220,26 @@ public function sort() {
220 220
 	 * @return DataList
221 221
 	 */
222 222
 	public function filter() {
223  
-		$numberFuncArgs = count(func_get_args());
224  
-		$whereArguments = array();
225  
-		if($numberFuncArgs == 1 && is_array(func_get_arg(0))) {
226  
-			$whereArguments = func_get_arg(0);
227  
-		} elseif($numberFuncArgs == 2) {
228  
-			$whereArguments[func_get_arg(0)] = func_get_arg(1);
229  
-		} else {
230  
-			throw new InvalidArgumentException('Incorrect number of arguments passed to filter()');
  223
+		// Validate and process arguments
  224
+		$arguments = func_get_args();
  225
+		switch(sizeof($arguments)) {
  226
+			case 1: $filters = $arguments[0]; break;
  227
+			case 2: $filters = array($arguments[0] => $arguments[1]); break;
  228
+			default:
  229
+				throw new InvalidArgumentException('Incorrect number of arguments passed to filter()');
231 230
 		}
  231
+		
  232
+		$clone = clone $this;
  233
+		$clone->addFilter($filters);
  234
+		return $clone;
  235
+	}
232 236
 
  237
+	/**
  238
+	 * Modify this DataList, adding a filter
  239
+	 */
  240
+	public function addFilter($filterArray) {
233 241
 		$SQL_Statements = array();
234  
-		foreach($whereArguments as $field => $value) {
  242
+		foreach($filterArray as $field => $value) {
235 243
 			if(is_array($value)) {
236 244
 				$customQuery = 'IN (\''.implode('\',\'',Convert::raw2sql($value)).'\')';
237 245
 			} else {

0 notes on commit e19ab7a

Please sign in to comment.
Something went wrong with that request. Please try again.