Skip to content

Commit

Permalink
php-cs-fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverryan committed Aug 18, 2023
1 parent aa8e093 commit a10fc4f
Show file tree
Hide file tree
Showing 20 changed files with 161 additions and 36 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions src/AsMapper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

/*
* This file is part of the SymfonyCasts MicroMapper package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfonycasts\MicroMapper;

/**
Expand All @@ -15,8 +22,7 @@ class AsMapper
public function __construct(
private string $from,
private string $to,
)
{
) {
}

public function getFrom(): string
Expand Down
9 changes: 8 additions & 1 deletion src/Bundle/DependencyInjection/MicroMapperCompilerPass.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

/*
* This file is part of the SymfonyCasts MicroMapper package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* 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;
Expand All @@ -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)),
]);
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/Bundle/DependencyInjection/MicroMapperExtension.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

/*
* This file is part of the SymfonyCasts MicroMapper package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* 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;
Expand Down
9 changes: 8 additions & 1 deletion src/Bundle/config/services.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<?php

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
/*
* This file is part of the SymfonyCasts MicroMapper package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* 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 {
Expand Down
10 changes: 8 additions & 2 deletions src/MapperConfig.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

/*
* This file is part of the SymfonyCasts MicroMapper package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfonycasts\MicroMapper;

/**
Expand All @@ -11,8 +18,7 @@ public function __construct(
private string $fromClass,
private string $toClass,
private \Closure $mapper
)
{
) {
}

public function supports(object $fromObject, string $targetClass): bool
Expand Down
7 changes: 7 additions & 0 deletions src/MapperInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

/*
* This file is part of the SymfonyCasts MicroMapper package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfonycasts\MicroMapper;

/**
Expand Down
24 changes: 14 additions & 10 deletions src/MicroMapper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

/*
* This file is part of the SymfonyCasts MicroMapper package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfonycasts\MicroMapper;

/**
Expand All @@ -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');
Expand All @@ -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)) {
Expand All @@ -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));
}
}
7 changes: 7 additions & 0 deletions src/MicroMapperInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

/*
* This file is part of the SymfonyCasts MicroMapper package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfonycasts\MicroMapper;

/**
Expand Down
7 changes: 7 additions & 0 deletions src/SymfonycastsMicroMapperBundle.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

/*
* This file is part of the SymfonyCasts MicroMapper package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* 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;
Expand Down
9 changes: 8 additions & 1 deletion tests/IntegrationTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

/*
* This file is part of the SymfonyCasts MicroMapper package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* 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;
Expand All @@ -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);
Expand Down
11 changes: 9 additions & 2 deletions tests/MapperConfigTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

/*
* This file is part of the SymfonyCasts MicroMapper package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* 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;
Expand All @@ -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));
Expand All @@ -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());
Expand Down
11 changes: 9 additions & 2 deletions tests/MicroMapperTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

/*
* This file is part of the SymfonyCasts MicroMapper package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* 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;
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/DinoRegion.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

/*
* This file is part of the SymfonyCasts MicroMapper package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* 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
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/DinoRegionDto.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

/*
* This file is part of the SymfonyCasts MicroMapper package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* 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
Expand Down
12 changes: 9 additions & 3 deletions tests/fixtures/DinoRegionToDtoMapper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

/*
* This file is part of the SymfonyCasts MicroMapper package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* 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;
Expand All @@ -11,7 +18,6 @@ class DinoRegionToDtoMapper implements MapperInterface
{
public function __construct(private MicroMapperInterface $microMapper)
{

}

public function load(object $from, string $toClass, array $context): object
Expand All @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions tests/fixtures/Dinosaur.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

/*
* This file is part of the SymfonyCasts MicroMapper package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* 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
Expand All @@ -9,7 +16,6 @@ public function __construct(
public ?string $genus = null,
public ?string $species = null,
public ?DinoRegion $region = null,
)
{
) {
}
}
Loading

0 comments on commit a10fc4f

Please sign in to comment.