generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 208
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 #526 from spatie/abstract-eloquent-casts
Add abstract eloquent casts
- Loading branch information
Showing
12 changed files
with
282 additions
and
4 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
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,55 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Support; | ||
|
||
use Spatie\LaravelData\Contracts\BaseData; | ||
|
||
class DataClassMorphMap | ||
{ | ||
/** @var array<string, class-string<BaseData>> */ | ||
protected array $map = []; | ||
|
||
/** @var array< class-string<BaseData>, string> */ | ||
protected array $reversedMap = []; | ||
|
||
|
||
/** | ||
* @param string $alias | ||
* @param class-string<BaseData> $class | ||
*/ | ||
public function add( | ||
string $alias, | ||
string $class | ||
): self { | ||
$this->map[$alias] = $class; | ||
$this->reversedMap[$class] = $alias; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param array<string, class-string<BaseData>> $map | ||
*/ | ||
public function merge(array $map): self | ||
{ | ||
foreach ($map as $alias => $class) { | ||
$this->add($alias, $class); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function getMorphedDataClass(string $alias): ?string | ||
{ | ||
return $this->map[$alias] ?? null; | ||
} | ||
|
||
|
||
/** | ||
* @param class-string<BaseData> $class | ||
*/ | ||
public function getDataClassAlias(string $class): ?string | ||
{ | ||
return $this->reversedMap[$class] ?? null; | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Tests\Fakes\AbstractData; | ||
|
||
use Spatie\LaravelData\Data; | ||
|
||
abstract class AbstractData extends Data | ||
{ | ||
} |
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,11 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Tests\Fakes\AbstractData; | ||
|
||
class AbstractDataA extends AbstractData | ||
{ | ||
public function __construct( | ||
public string $a, | ||
) { | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Tests\Fakes\AbstractData; | ||
|
||
class AbstractDataB extends AbstractData | ||
{ | ||
public function __construct( | ||
public string $b, | ||
) { | ||
} | ||
} |
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
Oops, something went wrong.