Skip to content

Commit

Permalink
union types
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Sep 21, 2024
1 parent 4539af9 commit ef01c81
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
29 changes: 25 additions & 4 deletions docs/mapper/02-object.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ populate a specific target property.
Example:

```php
use Rekalogika\Mapper\PropertyMapper\AsPropertyMapper;
use Rekalogika\Mapper\Attribute\AsPropertyMapper;

class UserMapper
{
Expand All @@ -88,7 +88,7 @@ If you have many properties to manually map, you can put the `AsPropertyMapper`
attribute on the class, and it will apply to all methods in the class. Example:

```php
use Rekalogika\Mapper\PropertyMapper\AsPropertyMapper;
use Rekalogika\Mapper\Attribute\AsPropertyMapper;

#[AsPropertyMapper(targetClass: UserDto::class)]
class UserMapper
Expand Down Expand Up @@ -120,7 +120,7 @@ mapper will use the method name, stripping the leading 'map' and lowercasing
the first letter.

```php
use Rekalogika\Mapper\PropertyMapper\AsPropertyMapper;
use Rekalogika\Mapper\Attribute\AsPropertyMapper;

#[AsPropertyMapper(targetClass: UserDto::class)]
class UserMapper
Expand Down Expand Up @@ -157,7 +157,7 @@ must be the source object.
```php
use Rekalogika\Mapper\Context\Context;
use Rekalogika\Mapper\MainTransformerInterface;
use Rekalogika\Mapper\PropertyMapper\AsPropertyMapper;
use Rekalogika\Mapper\Attribute\AsPropertyMapper;
use Rekalogika\Mapper\SubMapper\SubMapperInterface;

#[AsPropertyMapper(targetClass: UserDto::class)]
Expand All @@ -177,6 +177,27 @@ class UserMapper
}
```

### Source Union Types

Union types on the source side are supported.

```php
use Rekalogika\Mapper\Attribute\AsPropertyMapper;

class AnimalMapper
{
#[AsPropertyMapper(
targetClass: AnimalDto::class,
property: 'name',
)]
// highlight-next-line
public function mapName(Cat|Dog $animal): string
{
return $animal->getName();
}
}
```

### Manual Wiring

If you don't use autowiring, autoconfiguration, or don't want to use attributes,
Expand Down
18 changes: 18 additions & 0 deletions docs/mapper/06-object-mapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ class MoneyObjectMapper
}
```

## Source Union Types

Union types on the source side are supported.

```php
use Rekalogika\Mapper\Attribute\AsObjectMapper;

class AnimalMapper
{
#[AsObjectMapper()]
// highlight-next-line
public function mapCatOrDogToAnimalDto(Cat|Dog $animal): AnimalDto
{
return new AnimalDto($animal);
}
}
```

Read more about the sub mapper in the [SubMapper](submapper) chapter.

## Using a Lazy-Loading Proxy
Expand Down

0 comments on commit ef01c81

Please sign in to comment.