Skip to content

Commit

Permalink
Merge pull request #1090 from grantholle/default-property-value
Browse files Browse the repository at this point in the history
Add a default value to getExtraProperty
  • Loading branch information
Gummibeer authored Sep 22, 2022
2 parents c8e4aa6 + 86fd1d2 commit f9487d7
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Contracts/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function subject(): MorphTo;

public function causer(): MorphTo;

public function getExtraProperty(string $propertyName): mixed;
public function getExtraProperty(string $propertyName, mixed $defaultValue): mixed;

public function changes(): Collection;

Expand Down
4 changes: 2 additions & 2 deletions src/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public function causer(): MorphTo
return $this->morphTo();
}

public function getExtraProperty(string $propertyName): mixed
public function getExtraProperty(string $propertyName, mixed $defaultValue = null): mixed
{
return Arr::get($this->properties->toArray(), $propertyName);
return Arr::get($this->properties->toArray(), $propertyName, $defaultValue);
}

public function changes(): Collection
Expand Down
1 change: 1 addition & 0 deletions tests/ActivityLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@

expect($firstActivity->properties)->toBeInstanceOf(Collection::class);
expect($firstActivity->getExtraProperty('key'))->toEqual('value');
expect($firstActivity->getExtraProperty('non_existant', 'default value'))->toEqual('default value');
});

it('can translate a given causer id to an object', function () {
Expand Down
4 changes: 2 additions & 2 deletions tests/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public function causer(): MorphTo
return $this->morphTo();
}

public function getExtraProperty(string $propertyName): mixed
public function getExtraProperty(string $propertyName, mixed $defaultValue = null): mixed
{
return Arr::get($this->properties->toArray(), $propertyName);
return Arr::get($this->properties->toArray(), $propertyName, $defaultValue);
}

public function changes(): Collection
Expand Down
6 changes: 3 additions & 3 deletions tests/Models/AnotherInvalidActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public function causer(): MorphTo
* Get the extra properties with the given name.
*
* @param string $propertyName
*
* @param mixed $defaultValue
* @return mixed
*/
public function getExtraProperty(string $propertyName): mixed
public function getExtraProperty(string $propertyName, mixed $defaultValue = null): mixed
{
return Arr::get($this->properties->toArray(), $propertyName);
return Arr::get($this->properties->toArray(), $propertyName, $defaultValue);
}

public function changes(): Collection
Expand Down

0 comments on commit f9487d7

Please sign in to comment.