diff --git a/src/Tempest/Support/src/ArrayHelper.php b/src/Tempest/Support/src/ArrayHelper.php index 6fa8da15e..073c14ede 100644 --- a/src/Tempest/Support/src/ArrayHelper.php +++ b/src/Tempest/Support/src/ArrayHelper.php @@ -10,7 +10,12 @@ use Generator; use Iterator; use Serializable; +use function Tempest\map; +/** + * @template TValueType + * @implements ArrayAccess + */ final class ArrayHelper implements Iterator, ArrayAccess, Serializable, Countable { use IsIterable; @@ -269,4 +274,14 @@ public function toArray(): array { return $this->array; } + + /** + * @template T + * @param class-string $to + * @return self + */ + public function mapTo(string $to): self + { + return new self(map($this->array)->collection()->to($to)); + } } diff --git a/tests/Integration/Core/EnvHelperTest.php b/tests/Integration/Core/EnvHelperTest.php index a2c10a154..74e764cda 100644 --- a/tests/Integration/Core/EnvHelperTest.php +++ b/tests/Integration/Core/EnvHelperTest.php @@ -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 { diff --git a/tests/Integration/EventBus/EventIntegrationTestCase.php b/tests/Integration/EventBus/EventIntegrationTestCase.php index fd57e599e..6100afb6e 100644 --- a/tests/Integration/EventBus/EventIntegrationTestCase.php +++ b/tests/Integration/EventBus/EventIntegrationTestCase.php @@ -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 { diff --git a/tests/Integration/Mapper/Mappers/ArrayToJsonMapperTestCase.php b/tests/Integration/Mapper/Mappers/ArrayToJsonMapperTestCase.php index 7278d749d..adb4ba0ad 100644 --- a/tests/Integration/Mapper/Mappers/ArrayToJsonMapperTestCase.php +++ b/tests/Integration/Mapper/Mappers/ArrayToJsonMapperTestCase.php @@ -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 { diff --git a/tests/Integration/Mapper/Mappers/ArrayToObjectMapperTestCase.php b/tests/Integration/Mapper/Mappers/ArrayToObjectMapperTestCase.php index 8ac81314a..621a0c0dd 100644 --- a/tests/Integration/Mapper/Mappers/ArrayToObjectMapperTestCase.php +++ b/tests/Integration/Mapper/Mappers/ArrayToObjectMapperTestCase.php @@ -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; @@ -22,7 +22,7 @@ /** * @internal */ -final class ArrayToObjectMapperTestCase extends IntegrationTestCase +final class ArrayToObjectMapperTestCase extends FrameworkIntegrationTestCase { public function test_missing_values(): void { diff --git a/tests/Integration/Mapper/Mappers/JsonToArrayMapperTestCase.php b/tests/Integration/Mapper/Mappers/JsonToArrayMapperTestCase.php index 77e6968cd..438ab57b0 100644 --- a/tests/Integration/Mapper/Mappers/JsonToArrayMapperTestCase.php +++ b/tests/Integration/Mapper/Mappers/JsonToArrayMapperTestCase.php @@ -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 { diff --git a/tests/Integration/Mapper/Mappers/JsonToObjectMapperTestCase.php b/tests/Integration/Mapper/Mappers/JsonToObjectMapperTestCase.php index 580bcd54e..4b1130ad8 100644 --- a/tests/Integration/Mapper/Mappers/JsonToObjectMapperTestCase.php +++ b/tests/Integration/Mapper/Mappers/JsonToObjectMapperTestCase.php @@ -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 { diff --git a/tests/Integration/Mapper/Mappers/ObjectToArrayMapperTestCase.php b/tests/Integration/Mapper/Mappers/ObjectToArrayMapperTestCase.php index 9b13fca31..2bc7dfe5c 100644 --- a/tests/Integration/Mapper/Mappers/ObjectToArrayMapperTestCase.php +++ b/tests/Integration/Mapper/Mappers/ObjectToArrayMapperTestCase.php @@ -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 { diff --git a/tests/Integration/Mapper/Mappers/ObjectToJsonMapperTestCase.php b/tests/Integration/Mapper/Mappers/ObjectToJsonMapperTestCase.php index 712cc4e4f..6f13142d1 100644 --- a/tests/Integration/Mapper/Mappers/ObjectToJsonMapperTestCase.php +++ b/tests/Integration/Mapper/Mappers/ObjectToJsonMapperTestCase.php @@ -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 { diff --git a/tests/Integration/Mapper/ObjectFactoryTestCase.php b/tests/Integration/Mapper/ObjectFactoryTestCase.php index 0cf2738f8..38f0dc724 100644 --- a/tests/Integration/Mapper/ObjectFactoryTestCase.php +++ b/tests/Integration/Mapper/ObjectFactoryTestCase.php @@ -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 { diff --git a/tests/Integration/Support/ArrayHelperTest.php b/tests/Integration/Support/ArrayHelperTest.php new file mode 100644 index 000000000..e50ba2399 --- /dev/null +++ b/tests/Integration/Support/ArrayHelperTest.php @@ -0,0 +1,22 @@ + 'test']])->mapTo(TestObject::class); + + $this->assertInstanceOf(TestObject::class, $array[0]); + } +} diff --git a/tests/Integration/Support/Fixtures/TestObject.php b/tests/Integration/Support/Fixtures/TestObject.php new file mode 100644 index 000000000..1b4bb51e8 --- /dev/null +++ b/tests/Integration/Support/Fixtures/TestObject.php @@ -0,0 +1,13 @@ +