Skip to content

Commit

Permalink
Merge pull request #90 from kevariable/2x
Browse files Browse the repository at this point in the history
Hydrate method and Data mapper with configurable
  • Loading branch information
kevariable authored Mar 29, 2023
2 parents 6a259b7 + 13fac95 commit 15f1f31
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 24 deletions.
21 changes: 21 additions & 0 deletions src/Actions/DataMapperAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace KoalaFacade\DiamondConsole\Actions;

use CuyZ\Valinor\Mapper\Source\Source;
use CuyZ\Valinor\MapperBuilder;
use KoalaFacade\DiamondConsole\Contracts\DataMapper;
use KoalaFacade\DiamondConsole\Foundation\Action;

readonly class DataMapperAction extends Action implements DataMapper
{
public function execute(string $signature, array $data): mixed
{
return (new MapperBuilder)
->mapper()
->map(
signature: $signature,
source: Source::array(data: $data)->camelCaseKeys()
);
}
}
16 changes: 16 additions & 0 deletions src/Contracts/DataMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace KoalaFacade\DiamondConsole\Contracts;

use CuyZ\Valinor\Mapper\MappingError;

interface DataMapper
{
/**
* @param array<array-key, mixed> $data
* @param class-string | string $signature
*
* @throws MappingError
*/
public function execute(string $signature, array $data): mixed;
}
4 changes: 4 additions & 0 deletions src/DiamondConsoleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Illuminate\Support\Arr;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use KoalaFacade\DiamondConsole\Actions\DataMapperAction;
use KoalaFacade\DiamondConsole\Contracts\DataMapper;
use Symfony\Component\Finder\SplFileInfo;

class DiamondConsoleServiceProvider extends ServiceProvider
Expand All @@ -20,6 +22,8 @@ public function boot(): void
public function register(): void
{
$this->mergeConfigFrom(path: __DIR__ . '/../config/diamond.php', key: 'diamond');

$this->app->singletonIf(abstract: DataMapper::class, concrete: DataMapperAction::class);
}

protected function registerPublishers(): void
Expand Down
2 changes: 1 addition & 1 deletion src/Foundation/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace KoalaFacade\DiamondConsole\Foundation;

readonly abstract class Action
abstract readonly class Action
{
/**
* Resolve an action class
Expand Down
21 changes: 8 additions & 13 deletions src/Foundation/DataTransferObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ protected function toExcludedPropertiesOnUpdate(): array
return [];
}

/**
* Resolve result array-key of toArray method from behaviour
*/
protected function resolveArrayKey(string $key): string
{
return Str::snake(value: $key);
}

/**
* The method that will resolve the inheritance properties
* naming to snake case that can fit with database column naming
Expand All @@ -59,18 +67,8 @@ public function toArray(): array
->toArray();
}

/**
* Resolve result array-key of toArray method from behaviour
*/
protected function resolveArrayKey(string $key): string
{
return Str::snake(value: $key);
}

/**
* Die and dump the current Data.
*
* @return never
*/
public function dd(): never
{
Expand All @@ -79,9 +77,6 @@ public function dd(): never

/**
* Abilities to orchestrate the Data
*
* @param mixed ...$values
* @return static
*/
public function recycle(mixed ...$values): static
{
Expand Down
36 changes: 30 additions & 6 deletions src/Foundation/DataTransferObject/HasResolvable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use KoalaFacade\DiamondConsole\Contracts\DataMapper;

trait HasResolvable
{
Expand Down Expand Up @@ -47,15 +48,12 @@ public static function resolveFrom(FormRequest | Model | array $abstract): stati
* @param array<TKey, TValue> $data
*
* @throws MappingError
*
* @deprecated use hydrate(array $data) instead
*/
public static function resolve(array $data): static
{
/** @var static $instance */
$instance = (new MapperBuilder())
->mapper()
->map(signature: static::class, source: static::resolveTheArrayKeyForm(data: $data));

return $instance;
return static::hydrate($data);
}

/**
Expand Down Expand Up @@ -92,6 +90,8 @@ public static function resolveFromModel(Model $model): static
}

/**
* @deprecated
*
* Resolve all array key form according the config
*
* @template TArrayKey of array-key
Expand Down Expand Up @@ -128,4 +128,28 @@ protected static function resolveArrayKeyOfInput(string $key): string
{
return Str::camel(value: $key);
}

/**
* @param array<TKey, TValue> $data
*
* Hydrate incoming data to resolve unstructured data
*
* @throws MappingError
*
* @template TKey of array-key
* @template TValue
*/
public static function hydrate(array $data): static
{
/** @var DataMapper $dataMapper */
$dataMapper = resolve(name: DataMapper::class);

/** @var static $instance */
$instance = $dataMapper->execute(
signature: static::class,
data: $data
);

return $instance;
}
}
42 changes: 38 additions & 4 deletions tests/Unit/DataTransferObjects/DataTransferObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Composer\InstalledVersions;
use Illuminate\Support\Arr;
use KoalaFacade\DiamondConsole\Contracts\DataMapper;
use Tests\Unit\DataTransferObjects\Fixtures\GenderEnum;
use Tests\Unit\DataTransferObjects\Fixtures\RoleData;
use Tests\Unit\DataTransferObjects\Fixtures\UserData;
Expand Down Expand Up @@ -84,7 +85,7 @@
)
->tap(callable: function () {
$data = UserData::resolve(data: [
'name' => 'Kevin'
'name' => 'Kevin',
]);

$addresses = [
Expand All @@ -108,7 +109,7 @@
)
->tap(callable: function () {
$data = UserData::resolve(data: [
'name' => 'Kevin'
'name' => 'Kevin',
]);

$addresses = [
Expand All @@ -132,7 +133,7 @@
)
->tap(callable: function () {
$data = UserData::resolve(data: [
'name' => 'Kevin'
'name' => 'Kevin',
]);

$roleData = new RoleData(name: 'Maintainer');
Expand Down Expand Up @@ -160,4 +161,37 @@
->tap(
callback: fn (UserData $data) => expect($roleData->name)->toBe($roleData->name)
);
});
});

it(description: 'can hydrate data')
->group('unit', 'dto')
->tap(callable: function () {
$data = UserData::hydrate(data: [
'gender' => GenderEnum::Female,
]);

expect(value: $data->gender)->toBe(expected: GenderEnum::Female);
});

it(description: 'can change hydrate data mapper implementation')
->group('unit', 'dto')
->tap(callable: function () {
app()->instance(
abstract: DataMapper::class,
instance: new class implements DataMapper
{
public function execute(string $signature, array $data): mixed
{
return new $signature(
gender: $data['gender']
);
}
}
);

$data = UserData::hydrate(data: [
'gender' => GenderEnum::Female,
]);

expect(value: $data->gender)->toBe(expected: GenderEnum::Female);
});

0 comments on commit 15f1f31

Please sign in to comment.