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

feat: Add method to check if a model has never had a specific status #127

Merged
merged 3 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 27 additions & 0 deletions src/HasStatuses.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,40 @@ 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();

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();
Expand Down
10 changes: 10 additions & 0 deletions tests/HasStatusesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
Loading