diff --git a/README.md b/README.md index 9818cb3..1ee2dc5 100644 --- a/README.md +++ b/README.md @@ -215,6 +215,14 @@ You can check if a specific status has been set on the model at any time by usin $model->hasEverHadStatus('status 1'); ``` +### Check if status has never been assigned + +You can check if a specific status has never been set on the model at any time by using the `hasNeverHadStatus` method: + +```php +$model->hasNeverHadStatus('status 1'); +``` + ### Delete status from model You can delete any given status that has been set on the model at any time by using the `deleteStatus` method: diff --git a/src/HasStatuses.php b/src/HasStatuses.php index bc48659..a75953e 100644 --- a/src/HasStatuses.php +++ b/src/HasStatuses.php @@ -55,6 +55,17 @@ public function latestStatus(...$names): ?Status return $statuses->whereIn('name', $names)->first(); } + /** + * Check if the model has ever had a status with the given name. + * + * This method determines whether the current model instance has ever had a status + * with the specified name. + * + * @param string $name The name of the status to check for. + * + * @return bool Returns true if the model has ever had the status with the given name, + * otherwise returns false. + */ public function hasEverHadStatus($name): bool { $statuses = $this->relationLoaded('statuses') ? $this->statuses : $this->statuses(); @@ -62,6 +73,22 @@ public function hasEverHadStatus($name): bool return $statuses->where('name', $name)->count() > 0; } + /** + * Check if the model has never had a status with the given name. + * + * This method determines whether the current model instance has never had a status + * with the specified name by negating the result of hasEverHadStatus. + * + * @param string $name The name of the status to check for. + * + * @return bool Returns true if the model has never had the status with the given name, + * otherwise returns false. + */ + public function hasNeverHadStatus($name): bool + { + return !$this->hasEverHadStatus($name); + } + public function deleteStatus(...$names) { $names = is_array($names) ? Arr::flatten($names) : func_get_args(); diff --git a/tests/HasStatusesTest.php b/tests/HasStatusesTest.php index a127789..075630a 100644 --- a/tests/HasStatusesTest.php +++ b/tests/HasStatusesTest.php @@ -109,6 +109,16 @@ ->expect(fn () => $this->testModel->hasEverHadStatus('status 2')) ->toBeFalse(); +it('will return `false` if specific status is found') + ->tap(fn () => $this->testModel->setStatus('status 1')) + ->expect(fn () => $this->testModel->hasNeverHadStatus('status 1')) + ->toBeFalse(); + +it('will return `true` if specific status is not found') + ->tap(fn () => $this->testModel->setStatus('status 1')) + ->expect(fn () => $this->testModel->hasNeverHadStatus('status 2')) + ->toBeTrue(); + it('can delete a specific status', function () { $this->testModel->setStatus('status to delete');