diff --git a/README.md b/README.md index 8e97b43..022d3f2 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -# MicroMapper: The Tiny, Underwhelming Data Mapper +# MicroMapper: The Tiny, Underwhelming Data Mapper for Symfony! Need to map one object (e.g. a Doctrine entity) to another -object (e.g. a DTO) and love writing the mapping code manually? +object (e.g. a DTO) and *love* writing the mapping code manually? Then this library is for you! Define a "mapper" class: @@ -50,15 +50,15 @@ With MicroMapper, *you* do the heavy lifting. Let's review with a table! | Some of the mapping is automatic | ❌ | ✅ | | Extensible | ✅ | ✅ | | Handles nested objects | ✅ | ✅ | -| Small & Dead-simple | ✅ | (not SO simple | +| Small & Dead-simple | ✅ | (not SO simple) | -## Support Us & Symfony +## Support us & Symfony -Finding this package useful! We're *thrilled* 😍! +Is this package useful! We're *thrilled* 😍! A lot of time & effort from the Symfonycasts team & the Symfony community -goes into creating and maintaining these packages. You can support us and -Symfony by grabbing a subscription to [SymfonyCasts](https://symfonycasts.com)! +goes into creating and maintaining these packages. You can support us + +Symfony (and learn a bucket-load) by grabbing a subscription to [SymfonyCasts](https://symfonycasts.com)! ## Installation diff --git a/src/AsMapper.php b/src/AsMapper.php index 4a221c1..b296964 100644 --- a/src/AsMapper.php +++ b/src/AsMapper.php @@ -1,5 +1,12 @@ + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfonycasts\MicroMapper; /** @@ -15,8 +22,7 @@ class AsMapper public function __construct( private string $from, private string $to, - ) - { + ) { } public function getFrom(): string diff --git a/src/Bundle/DependencyInjection/MicroMapperCompilerPass.php b/src/Bundle/DependencyInjection/MicroMapperCompilerPass.php index de89585..848f861 100644 --- a/src/Bundle/DependencyInjection/MicroMapperCompilerPass.php +++ b/src/Bundle/DependencyInjection/MicroMapperCompilerPass.php @@ -1,5 +1,12 @@ + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfonycasts\MicroMapper\Bundle\DependencyInjection; use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; @@ -22,7 +29,7 @@ public function process(ContainerBuilder $container): void $mapperConfigDefinitions[] = new Definition(MapperConfig::class, [ $tag['from'], $tag['to'], - new ServiceClosureArgument(new Reference($id)) + new ServiceClosureArgument(new Reference($id)), ]); } } diff --git a/src/Bundle/DependencyInjection/MicroMapperExtension.php b/src/Bundle/DependencyInjection/MicroMapperExtension.php index c5969cd..a281d42 100644 --- a/src/Bundle/DependencyInjection/MicroMapperExtension.php +++ b/src/Bundle/DependencyInjection/MicroMapperExtension.php @@ -1,5 +1,12 @@ + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfonycasts\MicroMapper\Bundle\DependencyInjection; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; diff --git a/src/Bundle/config/services.php b/src/Bundle/config/services.php index 1838e9d..4a83978 100644 --- a/src/Bundle/config/services.php +++ b/src/Bundle/config/services.php @@ -1,9 +1,16 @@ + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; use Symfonycasts\MicroMapper\MicroMapper; use Symfonycasts\MicroMapper\MicroMapperInterface; + use function Symfony\Component\DependencyInjection\Loader\Configurator\abstract_arg; return static function (ContainerConfigurator $container): void { diff --git a/src/MapperConfig.php b/src/MapperConfig.php index 8a43f03..fdf7c0f 100644 --- a/src/MapperConfig.php +++ b/src/MapperConfig.php @@ -1,5 +1,12 @@ + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfonycasts\MicroMapper; /** @@ -11,8 +18,7 @@ public function __construct( private string $fromClass, private string $toClass, private \Closure $mapper - ) - { + ) { } public function supports(object $fromObject, string $targetClass): bool diff --git a/src/MapperInterface.php b/src/MapperInterface.php index c7f12c3..6da1a6a 100644 --- a/src/MapperInterface.php +++ b/src/MapperInterface.php @@ -1,5 +1,12 @@ + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfonycasts\MicroMapper; /** diff --git a/src/MicroMapper.php b/src/MicroMapper.php index 2a84c90..22551f1 100644 --- a/src/MicroMapper.php +++ b/src/MicroMapper.php @@ -1,5 +1,12 @@ + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfonycasts\MicroMapper; /** @@ -26,7 +33,7 @@ public function addMapperConfig(MapperConfig $mapperConfig): void public function map(object $from, string $toClass, array $context = []): object { - $this->currentDepth++; + ++$this->currentDepth; if ($this->currentDepth > 50) { throw new \Exception('Max depth reached'); @@ -41,19 +48,16 @@ public function map(object $from, string $toClass, array $context = []): object $this->maxDepth = $context[self::MAX_DEPTH] + $this->currentDepth; } - $shouldFullyPopulate = $this->maxDepth === null || $this->currentDepth < $this->maxDepth; + $shouldFullyPopulate = null === $this->maxDepth || $this->currentDepth < $this->maxDepth; // watch for circular references, but only if we're fully populating // if we are not fully populating, this is already the final depth/level // through the micro mapper. if (isset($this->objectHashes[spl_object_hash($from)]) && $shouldFullyPopulate) { - throw new \Exception(sprintf( - 'Circular reference detected with micro mapper: %s. Try passing [MicroMapperInterface::MAX_DEPTH => 1] when mapping relationships.', - implode(' -> ', array_merge($this->objectHashes, [get_class($from)])) - )); + throw new \Exception(sprintf('Circular reference detected with micro mapper: %s. Try passing [MicroMapperInterface::MAX_DEPTH => 1] when mapping relationships.', implode(' -> ', array_merge($this->objectHashes, [$from::class])))); } - $this->objectHashes[spl_object_hash($from)] = get_class($from); + $this->objectHashes[spl_object_hash($from)] = $from::class; foreach ($this->mapperConfigs as $mapperConfig) { if (!$mapperConfig->supports($from, $toClass)) { @@ -63,17 +67,17 @@ public function map(object $from, string $toClass, array $context = []): object $toObject = $mapperConfig->getMapper()->load($from, $toClass, $context); // avoid fully populated objects if max depth is reached - if ($this->maxDepth === null || $this->currentDepth < $this->maxDepth) { + if (null === $this->maxDepth || $this->currentDepth < $this->maxDepth) { $mapperConfig->getMapper()->populate($from, $toObject, $context); } unset($this->objectHashes[spl_object_hash($from)]); - $this->currentDepth--; + --$this->currentDepth; $this->maxDepth = $previousMaxDepth; return $toObject; } - throw new \Exception(sprintf('No mapper found for %s -> %s', get_class($from), $toClass)); + throw new \Exception(sprintf('No mapper found for %s -> %s', $from::class, $toClass)); } } diff --git a/src/MicroMapperInterface.php b/src/MicroMapperInterface.php index b73fba0..abf3320 100644 --- a/src/MicroMapperInterface.php +++ b/src/MicroMapperInterface.php @@ -1,5 +1,12 @@ + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfonycasts\MicroMapper; /** diff --git a/src/SymfonycastsMicroMapperBundle.php b/src/SymfonycastsMicroMapperBundle.php index e6839c5..7b01ffe 100644 --- a/src/SymfonycastsMicroMapperBundle.php +++ b/src/SymfonycastsMicroMapperBundle.php @@ -1,5 +1,12 @@ + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfonycasts\MicroMapper; use Symfony\Component\DependencyInjection\ContainerBuilder; diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index 53c79b0..830b4a8 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -1,5 +1,12 @@ + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfonycasts\MicroMapper\Tests; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; @@ -21,7 +28,7 @@ public function testBundleIntegration() $region->dinosaurs = [$dinosaur1]; $microMapper = self::getContainer()->get('public.micro_mapper'); - assert($microMapper instanceof MicroMapperInterface); + \assert($microMapper instanceof MicroMapperInterface); $dto = $microMapper->map($region, DinoRegionDto::class); $this->assertInstanceOf(DinoRegionDto::class, $dto); $this->assertSame(1, $dto->id); diff --git a/tests/MapperConfigTest.php b/tests/MapperConfigTest.php index ed9ff2e..435fec6 100644 --- a/tests/MapperConfigTest.php +++ b/tests/MapperConfigTest.php @@ -1,5 +1,12 @@ + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfonycasts\MicroMapper\Tests; use PHPUnit\Framework\TestCase; @@ -15,7 +22,7 @@ public function testSupports() $config = new MapperConfig( fromClass: DinoRegion::class, toClass: DinoRegionDto::class, - mapper: fn() => $this->createMock(MapperInterface::class), + mapper: fn () => $this->createMock(MapperInterface::class), ); $this->assertTrue($config->supports(new DinoRegion(), DinoRegionDto::class)); @@ -30,7 +37,7 @@ public function testGetMapper() $config = new MapperConfig( fromClass: DinoRegion::class, toClass: DinoRegionDto::class, - mapper: fn() => $mockMapper, + mapper: fn () => $mockMapper, ); $this->assertSame($mockMapper, $config->getMapper()); diff --git a/tests/MicroMapperTest.php b/tests/MicroMapperTest.php index 49d787a..69cc396 100644 --- a/tests/MicroMapperTest.php +++ b/tests/MicroMapperTest.php @@ -1,5 +1,12 @@ + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfonycasts\MicroMapper\Tests; use PHPUnit\Framework\TestCase; @@ -58,12 +65,12 @@ private function createMapper(): MicroMapperInterface $microMapper->addMapperConfig(new MapperConfig( DinoRegion::class, DinoRegionDto::class, - fn() => new DinoRegionToDtoMapper($microMapper) + fn () => new DinoRegionToDtoMapper($microMapper) )); $microMapper->addMapperConfig(new MapperConfig( Dinosaur::class, DinosaurDto::class, - fn() => new DinosaurToDtoMapper($microMapper) + fn () => new DinosaurToDtoMapper($microMapper) )); return $microMapper; diff --git a/tests/fixtures/DinoRegion.php b/tests/fixtures/DinoRegion.php index 7b7b8e5..1da5915 100644 --- a/tests/fixtures/DinoRegion.php +++ b/tests/fixtures/DinoRegion.php @@ -1,5 +1,12 @@ + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfonycasts\MicroMapper\Tests\fixtures; class DinoRegion diff --git a/tests/fixtures/DinoRegionDto.php b/tests/fixtures/DinoRegionDto.php index 559893c..111a43b 100644 --- a/tests/fixtures/DinoRegionDto.php +++ b/tests/fixtures/DinoRegionDto.php @@ -1,5 +1,12 @@ + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfonycasts\MicroMapper\Tests\fixtures; class DinoRegionDto diff --git a/tests/fixtures/DinoRegionToDtoMapper.php b/tests/fixtures/DinoRegionToDtoMapper.php index 549a234..ef19c1e 100644 --- a/tests/fixtures/DinoRegionToDtoMapper.php +++ b/tests/fixtures/DinoRegionToDtoMapper.php @@ -1,5 +1,12 @@ + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfonycasts\MicroMapper\Tests\fixtures; use Symfonycasts\MicroMapper\AsMapper; @@ -11,7 +18,6 @@ class DinoRegionToDtoMapper implements MapperInterface { public function __construct(private MicroMapperInterface $microMapper) { - } public function load(object $from, string $toClass, array $context): object @@ -24,8 +30,8 @@ public function load(object $from, string $toClass, array $context): object public function populate(object $from, object $to, array $context): object { - assert($from instanceof DinoRegion); - assert($to instanceof DinoRegionDto); + \assert($from instanceof DinoRegion); + \assert($to instanceof DinoRegionDto); $to->name = $from->name; $to->climate = $from->climate; diff --git a/tests/fixtures/Dinosaur.php b/tests/fixtures/Dinosaur.php index a0b399d..9b88220 100644 --- a/tests/fixtures/Dinosaur.php +++ b/tests/fixtures/Dinosaur.php @@ -1,5 +1,12 @@ + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfonycasts\MicroMapper\Tests\fixtures; class Dinosaur @@ -9,7 +16,6 @@ public function __construct( public ?string $genus = null, public ?string $species = null, public ?DinoRegion $region = null, - ) - { + ) { } } diff --git a/tests/fixtures/DinosaurDto.php b/tests/fixtures/DinosaurDto.php index 337e4b5..01eb5ec 100644 --- a/tests/fixtures/DinosaurDto.php +++ b/tests/fixtures/DinosaurDto.php @@ -1,5 +1,12 @@ + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfonycasts\MicroMapper\Tests\fixtures; class DinosaurDto diff --git a/tests/fixtures/DinosaurToDtoMapper.php b/tests/fixtures/DinosaurToDtoMapper.php index cba8270..f3fc235 100644 --- a/tests/fixtures/DinosaurToDtoMapper.php +++ b/tests/fixtures/DinosaurToDtoMapper.php @@ -1,5 +1,12 @@ + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfonycasts\MicroMapper\Tests\fixtures; use Symfonycasts\MicroMapper\AsMapper; @@ -11,7 +18,6 @@ class DinosaurToDtoMapper implements MapperInterface { public function __construct(private MicroMapperInterface $microMapper) { - } public function load(object $from, string $toClass, array $context): object @@ -24,8 +30,8 @@ public function load(object $from, string $toClass, array $context): object public function populate(object $from, object $to, array $context): object { - assert($from instanceof Dinosaur); - assert($to instanceof DinosaurDto); + \assert($from instanceof Dinosaur); + \assert($to instanceof DinosaurDto); $to->genus = $from->genus; $to->species = $from->species; diff --git a/tests/fixtures/MicroMapperTestKernel.php b/tests/fixtures/MicroMapperTestKernel.php index 9bdc737..c8f312a 100644 --- a/tests/fixtures/MicroMapperTestKernel.php +++ b/tests/fixtures/MicroMapperTestKernel.php @@ -1,5 +1,12 @@ + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Symfonycasts\MicroMapper\Tests\fixtures; use Symfony\Bundle\FrameworkBundle\FrameworkBundle;