Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Extract to own method #20130

Merged
merged 2 commits into from
Jul 18, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 34 additions & 38 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}

/**
Expand All @@ -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()
);
}

/**
Expand Down Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\Illuminate\Support\Arr::wrap ? 😉

if (array_key_exists($attribute, $changes)) {
return true;
}
}

return false;
}
}