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 branch 'mlshvdv-fix/request-resolver-mapped-output-names'
- Loading branch information
Showing
11 changed files
with
391 additions
and
18 deletions.
There are no files selected for viewing
File renamed without changes.
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,53 @@ | ||
--- | ||
title: Mapping rules | ||
weight: 13 | ||
--- | ||
|
||
It is possible to map the names properties going in and out of your data objects using: `MapOutputName`, `MapInputName` | ||
and `MapName` attributes. But sometimes it can be quite hard to follow where which name can be used. Let's go through | ||
some case: | ||
|
||
In the data object: | ||
|
||
```php | ||
class UserData extends Data | ||
{ | ||
public function __construct( | ||
#[MapName('favorite_song')] // name mapping | ||
public Lazy|SongData $song, | ||
#[RequiredWith('song')] // In validation rules, use the original name | ||
public string $title, | ||
) { | ||
} | ||
|
||
public static function allowedRequestExcept(): ?array | ||
{ | ||
return [ | ||
'song' // Use the original name when defining includes, excludes, excepts and only | ||
]; | ||
} | ||
|
||
// ... | ||
} | ||
``` | ||
|
||
When creating a data object: | ||
|
||
```php | ||
UserData::from([ | ||
'favorite_song' => ..., // You can use the mapped or original name here | ||
'title' => 'some title' | ||
]); | ||
``` | ||
|
||
When adding an include, exclude, except or only: | ||
|
||
```php | ||
UserData::from(User::first())->except('song'); // Always use the original name here | ||
``` | ||
|
||
Within a request query, you can use the mapped or original name: | ||
|
||
``` | ||
https://spatie.be/my-account?except[]=favorite_song | ||
``` |
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,67 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Support; | ||
|
||
class PartialTreesMapping | ||
{ | ||
/** | ||
* @param array<string, \Spatie\LaravelData\Support\PartialTreesMapping> $children | ||
*/ | ||
public function __construct( | ||
readonly public string $original, | ||
readonly public string $mapped, | ||
readonly public array $children = [], | ||
) { | ||
} | ||
|
||
public function getChild(string $mapped): ?PartialTreesMapping | ||
{ | ||
foreach ($this->children as $child) { | ||
if ($child->mapped === $mapped || $child->original === $mapped) { | ||
return $child; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static function fromRootDataClass(DataClass $dataClass): self | ||
{ | ||
return self::fromDataClass('root', 'root', $dataClass); | ||
} | ||
|
||
public static function fromDataClass( | ||
string $original, | ||
string $mapped, | ||
DataClass $dataClass | ||
): self { | ||
$children = []; | ||
|
||
$dataClass->properties->each(function (DataProperty $dataProperty) use (&$children) { | ||
if ($dataProperty->type->isDataObject || $dataProperty->type->isDataCollectable) { | ||
$children[] = self::fromDataClass( | ||
$dataProperty->name, | ||
$dataProperty->outputMappedName ?? $dataProperty->name, | ||
app(DataConfig::class)->getDataClass($dataProperty->type->dataClass), | ||
); | ||
|
||
return; | ||
} | ||
|
||
if ($dataProperty->outputMappedName === null) { | ||
return; | ||
} | ||
|
||
$children[] = new self( | ||
$dataProperty->name, | ||
$dataProperty->outputMappedName, | ||
); | ||
}); | ||
|
||
return new self( | ||
$original, | ||
$mapped, | ||
$children | ||
); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Tests\Fakes; | ||
|
||
use Spatie\LaravelData\Attributes\MapOutputName; | ||
use Spatie\LaravelData\Data; | ||
|
||
class SimpleChildDataWithMappedOutputName extends Data | ||
{ | ||
public function __construct( | ||
public int $id, | ||
#[MapOutputName('child_amount')] | ||
public float $amount | ||
) { | ||
} | ||
|
||
public static function allowedRequestExcept(): ?array | ||
{ | ||
return ['amount']; | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Tests\Fakes; | ||
|
||
use Spatie\LaravelData\Attributes\MapName; | ||
use Spatie\LaravelData\Attributes\MapOutputName; | ||
use Spatie\LaravelData\Data; | ||
use Spatie\LaravelData\Mappers\SnakeCaseMapper; | ||
|
||
#[MapName(SnakeCaseMapper::class)] | ||
class SimpleDataWithMappedOutputName extends Data | ||
{ | ||
public function __construct( | ||
public int $id, | ||
#[MapOutputName('paid_amount')] | ||
public float $amount, | ||
public string $anyString, | ||
public SimpleChildDataWithMappedOutputName $child | ||
) { | ||
} | ||
|
||
public static function allowedRequestExcept(): ?array | ||
{ | ||
return [ | ||
'amount', | ||
'anyString', | ||
'child', | ||
]; | ||
} | ||
} |
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.