Skip to content

Commit f9487d7

Browse files
authored
Merge pull request spatie#1090 from grantholle/default-property-value
Add a default value to getExtraProperty
2 parents c8e4aa6 + 86fd1d2 commit f9487d7

File tree

5 files changed

+9
-8
lines changed

5 files changed

+9
-8
lines changed

src/Contracts/Activity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function subject(): MorphTo;
1313

1414
public function causer(): MorphTo;
1515

16-
public function getExtraProperty(string $propertyName): mixed;
16+
public function getExtraProperty(string $propertyName, mixed $defaultValue): mixed;
1717

1818
public function changes(): Collection;
1919

src/Models/Activity.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ public function causer(): MorphTo
7171
return $this->morphTo();
7272
}
7373

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

7979
public function changes(): Collection

tests/ActivityLoggerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157

158158
expect($firstActivity->properties)->toBeInstanceOf(Collection::class);
159159
expect($firstActivity->getExtraProperty('key'))->toEqual('value');
160+
expect($firstActivity->getExtraProperty('non_existant', 'default value'))->toEqual('default value');
160161
});
161162

162163
it('can translate a given causer id to an object', function () {

tests/Models/Activity.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public function causer(): MorphTo
4040
return $this->morphTo();
4141
}
4242

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

4848
public function changes(): Collection

tests/Models/AnotherInvalidActivity.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ public function causer(): MorphTo
4242
* Get the extra properties with the given name.
4343
*
4444
* @param string $propertyName
45-
*
45+
* @param mixed $defaultValue
4646
* @return mixed
4747
*/
48-
public function getExtraProperty(string $propertyName): mixed
48+
public function getExtraProperty(string $propertyName, mixed $defaultValue = null): mixed
4949
{
50-
return Arr::get($this->properties->toArray(), $propertyName);
50+
return Arr::get($this->properties->toArray(), $propertyName, $defaultValue);
5151
}
5252

5353
public function changes(): Collection

0 commit comments

Comments
 (0)