Browse Source

Merge pull request #648 from cviebrock/array-helpers

add array_except() and array_only(), mimicking Input::except() and Input::only() but for any array
Taylor Otwell 12 years ago
parent
commit
cded744ac9
2 changed files with 27 additions and 3 deletions
  1. 24 0
      laravel/helpers.php
  2. 3 3
      laravel/input.php

+ 24 - 0
laravel/helpers.php

@@ -245,6 +245,30 @@ function array_pluck($array, $key)
 	}, $array);
 }
 
+/**
+ * Get a subset of the items from the given array.
+ *
+ * @param  array  $array
+ * @param  array  $keys
+ * @return array
+ */
+function array_only($array, $keys)
+{
+	return array_intersect_key( $array, array_flip((array) $keys) );
+}
+
+/**
+ * Get all of the given array except for a specified array of items.
+ *
+ * @param  array  $array
+ * @param  array  $keys
+ * @return array
+ */
+function array_except($array, $keys)
+{
+	return array_diff_key( $array, array_flip((array) $keys) );
+}
+
 /**
  * Transform Eloquent models to a JSON object.
  *

+ 3 - 3
laravel/input.php

@@ -127,7 +127,7 @@ class Input {
 	 */
 	public static function only($keys)
 	{
- 		return array_intersect_key(static::get(), array_flip((array) $keys));
+ 		return array_only(static::get(), $keys);
 	}
 
 	/**
@@ -146,7 +146,7 @@ class Input {
 	 */
 	public static function except($keys)
 	{
-		return array_diff_key(static::get(), array_flip((array) $keys));
+		return array_except(static::get(), $keys);
 	}
 
 	/**
@@ -207,7 +207,7 @@ class Input {
 	{
 		return ! is_null(static::file("{$key}.tmp_name"));
 	}
-	
+
 	/**
 	 * Move an uploaded file to permanent storage.
 	 *