Skip to content

Commit

Permalink
feat: array helper::map-to (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt authored Oct 1, 2024
1 parent 1572122 commit e4bb059
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 18 deletions.
15 changes: 15 additions & 0 deletions src/Tempest/Support/src/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
use Generator;
use Iterator;
use Serializable;
use function Tempest\map;

/**
* @template TValueType
* @implements ArrayAccess<array-key, TValueType>
*/
final class ArrayHelper implements Iterator, ArrayAccess, Serializable, Countable
{
use IsIterable;
Expand Down Expand Up @@ -269,4 +274,14 @@ public function toArray(): array
{
return $this->array;
}

/**
* @template T
* @param class-string<T> $to
* @return self<T>
*/
public function mapTo(string $to): self
{
return new self(map($this->array)->collection()->to($to));
}
}
4 changes: 2 additions & 2 deletions tests/Integration/Core/EnvHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

use PHPUnit\Framework\Attributes\DataProvider;
use function Tempest\env;
use Tests\Tempest\Integration\IntegrationTestCase;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;

/**
* @internal
*/
final class EnvHelperTest extends IntegrationTestCase
final class EnvHelperTest extends FrameworkIntegrationTestCase
{
public function test_env_fallback_value(): void
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/EventBus/EventIntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
use function Tempest\event;
use Tempest\EventBus\Tests\Fixtures\MyEventHandler;
use Tests\Tempest\Fixtures\Events\ItHappened;
use Tests\Tempest\Integration\IntegrationTestCase;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;

/**
* @internal
*/
final class EventIntegrationTestCase extends IntegrationTestCase
final class EventIntegrationTestCase extends FrameworkIntegrationTestCase
{
public function test_event(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

use function Tempest\map;
use Tempest\Mapper\MapTo;
use Tests\Tempest\Integration\IntegrationTestCase;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;

/**
* @internal
*/
final class ArrayToJsonMapperTestCase extends IntegrationTestCase
final class ArrayToJsonMapperTestCase extends FrameworkIntegrationTestCase
{
public function test_mapper(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Tempest\Http\Method;
use function Tempest\map;
use Tempest\Mapper\Exceptions\MissingValuesException;
use Tests\Tempest\Integration\IntegrationTestCase;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectA;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectWithBuiltInCasters;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectWithDefaultValues;
Expand All @@ -22,7 +22,7 @@
/**
* @internal
*/
final class ArrayToObjectMapperTestCase extends IntegrationTestCase
final class ArrayToObjectMapperTestCase extends FrameworkIntegrationTestCase
{
public function test_missing_values(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

use function Tempest\map;
use Tempest\Mapper\MapTo;
use Tests\Tempest\Integration\IntegrationTestCase;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;

/**
* @internal
*/
final class JsonToArrayMapperTestCase extends IntegrationTestCase
final class JsonToArrayMapperTestCase extends FrameworkIntegrationTestCase
{
public function test_mapper(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

use function Tempest\map;
use Tempest\Mapper\Mappers\JsonToObjectMapper;
use Tests\Tempest\Integration\IntegrationTestCase;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectA;

/**
* @internal
*/
final class JsonToObjectMapperTestCase extends IntegrationTestCase
final class JsonToObjectMapperTestCase extends FrameworkIntegrationTestCase
{
public function test_json_to_object(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

use function Tempest\map;
use Tempest\Mapper\MapTo;
use Tests\Tempest\Integration\IntegrationTestCase;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectA;

/**
* @internal
*/
final class ObjectToArrayMapperTestCase extends IntegrationTestCase
final class ObjectToArrayMapperTestCase extends FrameworkIntegrationTestCase
{
public function test_object_to_array(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

use function Tempest\map;
use Tempest\Mapper\MapTo;
use Tests\Tempest\Integration\IntegrationTestCase;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectA;

/**
* @internal
*/
final class ObjectToJsonMapperTestCase extends IntegrationTestCase
final class ObjectToJsonMapperTestCase extends FrameworkIntegrationTestCase
{
public function test_object_to_json(): void
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Mapper/ObjectFactoryTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
use Tempest\Mapper\Mappers\ArrayToObjectMapper;
use Tempest\Mapper\Mappers\ObjectToArrayMapper;
use Tempest\Mapper\ObjectFactory;
use Tests\Tempest\Integration\IntegrationTestCase;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
use Tests\Tempest\Integration\Mapper\Fixtures\ObjectA;

/**
* @internal
*/
final class ObjectFactoryTestCase extends IntegrationTestCase
final class ObjectFactoryTestCase extends FrameworkIntegrationTestCase
{
public function test_single_object(): void
{
Expand Down
22 changes: 22 additions & 0 deletions tests/Integration/Support/ArrayHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Tests\Tempest\Integration\Support;

use function Tempest\Support\arr;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
use Tests\Tempest\Integration\Support\Fixtures\TestObject;

/**
* @internal
*/
final class ArrayHelperTest extends FrameworkIntegrationTestCase
{
public function test_map_to(): void
{
$array = arr([['name' => 'test']])->mapTo(TestObject::class);

$this->assertInstanceOf(TestObject::class, $array[0]);
}
}
13 changes: 13 additions & 0 deletions tests/Integration/Support/Fixtures/TestObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Tests\Tempest\Integration\Support\Fixtures;

final readonly class TestObject
{
public function __construct(
public string $name,
) {
}
}

0 comments on commit e4bb059

Please sign in to comment.