Skip to content

Commit

Permalink
Merge pull request #127 from YazeedAlsaif/main
Browse files Browse the repository at this point in the history
feat: Add method to check if a model has never had a specific status
  • Loading branch information
freekmurze authored May 27, 2024
2 parents b3956cb + a1f8f01 commit 68f594a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
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

0 comments on commit 68f594a

Please sign in to comment.