From 9a77a62be6592028be3a3a354912377126e3f63e Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Tue, 18 Jul 2017 16:27:32 +0200 Subject: [PATCH 1/2] extract to separate method --- .../Eloquent/Concerns/HasAttributes.php | 72 +++++++++---------- 1 file changed, 34 insertions(+), 38 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php index 64f7c1a3c7a4..ac59f42d1944 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php @@ -951,28 +951,9 @@ public function syncChanges() */ public function isDirty($attributes = null) { - $dirty = $this->getDirty(); - - // If no specific attributes were provided, we will just see if the dirty array - // already contains any attributes. If it does we will just return that this - // count is greater than zero. Else, we need to check specific attributes. - if (is_null($attributes)) { - return count($dirty) > 0; - } - - $attributes = is_array($attributes) - ? $attributes : func_get_args(); - - // Here we will spin through every attribute and see if this is in the array of - // dirty attributes. If it is, we will return true and if we make it through - // all of the attributes for the entire array we will return false at end. - foreach ($attributes as $attribute) { - if (array_key_exists($attribute, $dirty)) { - return true; - } - } - - return false; + return $this->hasChanges($this->getDirty(), + is_array($attributes) ? $attributes : func_get_args() + ); } /** @@ -994,22 +975,9 @@ public function isClean($attributes = null) */ public function wasChanged($attributes = null) { - $changes = $this->getChanges(); - - if (is_null($attributes)) { - return count($changes) > 0; - } - - $attributes = is_array($attributes) - ? $attributes : func_get_args(); - - foreach ($attributes as $attribute) { - if (array_key_exists($attribute, $changes)) { - return true; - } - } - - return false; + return $this->hasChanges($this->getChanges(), + is_array($attributes) ? $attributes : func_get_args() + ); } /** @@ -1140,4 +1108,32 @@ protected static function getMutatorMethods($class) return $matches[1]; } + + /** + * Determine if the given attributes were changed. + * + * @param array $changes + * @param array|string|null $attributes + * @return bool + */ + protected function hasChanges($changes, $attributes = null) + { + // If no specific attributes were provided, we will just see if the dirty array + // already contains any attributes. If it does we will just return that this + // count is greater than zero. Else, we need to check specific attributes. + if (empty($attributes)) { + return count($changes) > 0; + } + + // Here we will spin through every attribute and see if this is in the array of + // dirty attributes. If it is, we will return true and if we make it through + // all of the attributes for the entire array we will return false at end. + foreach (array_wrap($attributes) as $attribute) { + if (array_key_exists($attribute, $changes)) { + return true; + } + } + + return false; + } } From 040f31c09f386cf89cfdc1f03bdd8101b39e32eb Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Tue, 18 Jul 2017 17:10:42 +0200 Subject: [PATCH 2/2] user ArrWrap --- src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php index ac59f42d1944..67a921096844 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php @@ -1128,7 +1128,7 @@ protected function hasChanges($changes, $attributes = null) // Here we will spin through every attribute and see if this is in the array of // dirty attributes. If it is, we will return true and if we make it through // all of the attributes for the entire array we will return false at end. - foreach (array_wrap($attributes) as $attribute) { + foreach (Arr::wrap($attributes) as $attribute) { if (array_key_exists($attribute, $changes)) { return true; }