generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from spatie/request-data
Add a WithData trait
- Loading branch information
Showing
6 changed files
with
188 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Exceptions; | ||
|
||
use Exception; | ||
|
||
class InvalidDataClass extends Exception | ||
{ | ||
public static function create(?string $class) | ||
{ | ||
$message = $class === null | ||
? 'Could not create a Data object, no data class was given' | ||
: "Could not create a Data object, `{$class}` does not implement `Data`"; | ||
|
||
return new self($message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData; | ||
|
||
use Spatie\LaravelData\Exceptions\InvalidDataClass; | ||
use Spatie\LaravelData\Resolvers\DataFromSomethingResolver; | ||
|
||
trait WithData | ||
{ | ||
public function getData(): Data | ||
{ | ||
$dataClass = match (true) { | ||
/** @psalm-suppress UndefinedThisPropertyFetch */ | ||
property_exists($this, 'dataClass') => $this->dataClass, | ||
method_exists($this, 'dataClass') => $this->dataClass(), | ||
default => null, | ||
}; | ||
|
||
if (! is_a($dataClass, Data::class, true)) { | ||
throw InvalidDataClass::create($dataClass); | ||
} | ||
|
||
return resolve(DataFromSomethingResolver::class)->execute( | ||
$dataClass, | ||
$this | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters