-
-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a304de
commit 7da46d8
Showing
18 changed files
with
432 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Statamic\Data; | ||
|
||
use Statamic\Support\Arr; | ||
|
||
trait HasDirtyState | ||
{ | ||
protected array $original = []; | ||
|
||
abstract public function getCurrentDirtyStateAttributes(): array; | ||
|
||
/** | ||
* Determine if the item or any of the given attribute(s) have been modified. | ||
* | ||
* @param null|string|array $attributes | ||
*/ | ||
public function isDirty($attributes = null): bool | ||
{ | ||
$currentValues = $this->getCurrentDirtyStateAttributes(); | ||
$originalValues = $this->getOriginal(); | ||
|
||
if (! $attributes) { | ||
return json_encode($currentValues) !== json_encode($originalValues); | ||
} | ||
|
||
return collect($attributes)->contains(function ($property) use ($currentValues, $originalValues) { | ||
// In an asset, the data key holds all the data. It would be a breaking | ||
// change to make it the root level, so we'll support both for now. | ||
if (! array_key_exists($property, $currentValues)) { | ||
$property = 'data.'.$property; | ||
} | ||
|
||
return data_get($currentValues, $property) !== data_get($originalValues, $property); | ||
}); | ||
} | ||
|
||
/** | ||
* Determine if the item or all the given attribute(s) have remained the same. | ||
* | ||
* @param null|string|array $attributes | ||
*/ | ||
public function isClean($attributes = null): bool | ||
{ | ||
return ! $this->isDirty($attributes); | ||
} | ||
|
||
public function syncOriginal(): static | ||
{ | ||
$this->original = $this->getCurrentDirtyStateAttributes(); | ||
|
||
return $this; | ||
} | ||
|
||
public function getOriginal($key = null, $fallback = null) | ||
{ | ||
return Arr::get($this->original, $key, $fallback); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.