Skip to content

Commit

Permalink
Merge pull request #16 from TomHAnderson/feature/wildcard-hydrator
Browse files Browse the repository at this point in the history
Added wildcard hydrator to manager
  • Loading branch information
TomHAnderson committed Feb 1, 2022
2 parents 6448b89 + 6c78635 commit 6c53b6f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/HydratorManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ public function extract(mixed $class, ?string $overrideHydrator = null): Collect
}

if (! $overrideHydrator && ! isset($this->classHydrators[get_class($class)])) {
throw new Exception\NoHydrator(get_class($class));
if (! isset($this->classHydrators['*']) || ! $this->classHydrators['*']) {
throw new Exception\NoHydrator(get_class($class));
}

$overrideHydrator = $this->classHydrators['*'];
}

// Use DI for hydrators
Expand Down
16 changes: 16 additions & 0 deletions test/HydratorManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ApiSkeletons\Laravel\HAL\Exception\NoHydrator;
use ApiSkeletons\Laravel\HAL\Resource;
use ApiSkeletonsTest\Laravel\HAL\Hydrator\DiHydrator;
use ApiSkeletonsTest\Laravel\HAL\Hydrator\WildcardHydrator;
use ApiSkeletonsTest\Laravel\HAL\Model\User;
use Illuminate\Foundation\Application;
use Illuminate\Pagination\LengthAwarePaginator;
Expand Down Expand Up @@ -78,6 +79,21 @@ public function testNoHydratorInExtract(): void
$hydratorManager->extract($invalid);
}

public function testWildcardHydrator(): void
{
$hydratorManager = new WildcardHydratorManager();
$user = new User();
$user->id = 1;
$user->name = 'Test';
$user->email = 'testing@test.net';

$resource = $hydratorManager->extract($user);
$this->assertInstanceOf(Resource::class, $resource);

$array = $resource->toArray();
$this->assertEquals(User::class, $array['class']);
}

public function testExtractCollection(): void
{
$hydratorManager = new HydratorManager();
Expand Down
31 changes: 31 additions & 0 deletions test/src/Hydrator/WildcardHydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace ApiSkeletonsTest\Laravel\HAL\Hydrator;

use ApiSkeletons\Laravel\HAL\Hydrator;
use ApiSkeletons\Laravel\HAL\Resource;
use Illuminate\Foundation\Application;

use function get_class;

final class WildcardHydrator extends Hydrator
{
private Application $app;

public function __construct(Application $app)
{
$this->app = $app;
}

/** {@inheritdoc} */
public function extract($class): Resource
{
$data = [
'class' => get_class($class),
];

return $this->hydratorManager->resource($data);
}
}
15 changes: 15 additions & 0 deletions test/src/WildcardHydratorManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace ApiSkeletonsTest\Laravel\HAL;

use ApiSkeletons\Laravel\HAL\HydratorManager as HalHydratorManager;

final class WildcardHydratorManager extends HalHydratorManager
{
/** {@inheritdoc} */
protected array $classHydrators = [
'*' => Hydrator\WildcardHydrator::class,
];
}

0 comments on commit 6c53b6f

Please sign in to comment.