Skip to content

Commit

Permalink
feat: Add method to check if a model has never had a specific status
Browse files Browse the repository at this point in the history
- Added `hasNeverHadStatus($name): bool` method to determine if the model has never had a status with the given name.
- Included PHPDoc comments for both `hasEverHadStatus($name): bool` and `hasNeverHadStatus($name): bool` methods.
- Added tests for `hasNeverHadStatus` method:
  - Test to return `false` if the specific status is found.
  - Test to return `true` if the specific status is not found.

This contribution introduces the inverse of the `hasEverHadStatus` function and improves documentation.
  • Loading branch information
YazeedAlsaif committed May 23, 2024
1 parent b3956cb commit 3fcf6b9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/HasStatuses.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,42 @@ 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.
*
* @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
{
$statuses = $this->relationLoaded('statuses') ? $this->statuses : $this->statuses();

return $statuses->where('name', $name)->count() === 0;
}

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 3fcf6b9

Please sign in to comment.