Skip to content

Commit

Permalink
PHP 8.2 stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Korbeil committed Sep 12, 2023
1 parent f3563ed commit fff6e0b
Show file tree
Hide file tree
Showing 155 changed files with 613 additions and 744 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'remove_inheritdoc' => false,
],
'nullable_type_declaration_for_default_null_value' => false,
'declare_strict_types' => true,

// Can be removed once PHP requirement is upgraded
'get_class_to_class_keyword' => false,
Expand Down
10 changes: 3 additions & 7 deletions src/Attribute/MapToContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@

namespace AutoMapper\Attribute;

#[\Attribute(\Attribute::TARGET_PARAMETER)] final class MapToContext
#[\Attribute(\Attribute::TARGET_PARAMETER)]
final class MapToContext
{
public function __construct(
private string $contextName,
public readonly string $contextName,
) {
}

public function getContextName(): string
{
return $this->contextName;
}
}
91 changes: 31 additions & 60 deletions src/AutoMapper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace AutoMapper;

use AutoMapper\Exception\NoMappingFoundException;
Expand Down Expand Up @@ -40,38 +42,23 @@
class AutoMapper implements AutoMapperInterface, AutoMapperRegistryInterface, MapperGeneratorMetadataRegistryInterface
{
/** @var MapperGeneratorMetadataInterface[] */
private $metadata = [];
private array $metadata = [];

/** @var GeneratedMapper[] */
private $mapperRegistry = [];

/** @var ClassLoaderInterface */
private $classLoader;

/** @var MapperGeneratorMetadataFactoryInterface|null */
private $mapperConfigurationFactory;

/** @var ChainTransformerFactory */
private $chainTransformerFactory;
private array $mapperRegistry = [];

public function __construct(ClassLoaderInterface $classLoader, ChainTransformerFactory $chainTransformerFactory, MapperGeneratorMetadataFactoryInterface $mapperConfigurationFactory = null)
{
$this->classLoader = $classLoader;
$this->mapperConfigurationFactory = $mapperConfigurationFactory;
$this->chainTransformerFactory = $chainTransformerFactory;
public function __construct(
private readonly ClassLoaderInterface $classLoader,
private readonly ChainTransformerFactory $chainTransformerFactory,
private readonly ?MapperGeneratorMetadataFactoryInterface $mapperConfigurationFactory = null
) {
}

/**
* {@inheritdoc}
*/
public function register(MapperGeneratorMetadataInterface $metadata): void
public function register(MapperGeneratorMetadataInterface $configuration): void
{
$this->metadata[$metadata->getSource()][$metadata->getTarget()] = $metadata;
$this->metadata[$configuration->getSource()][$configuration->getTarget()] = $configuration;
}

/**
* {@inheritdoc}
*/
public function getMapper(string $source, string $target): MapperInterface
{
$metadata = $this->getMetadata($source, $target);
Expand Down Expand Up @@ -100,60 +87,50 @@ public function getMapper(string $source, string $target): MapperInterface
return $this->mapperRegistry[$className];
}

/**
* {@inheritdoc}
*/
public function hasMapper(string $source, string $target): bool
{
return null !== $this->getMetadata($source, $target);
}

/**
* {@inheritdoc}
*/
public function map($sourceData, $targetData, array $context = [])
public function map(null|array|object $source, string|array|object $target, array $context = []): null|array|object
{
$source = null;
$target = null;
$sourceType = $targetType = null;

if (null === $sourceData) {
if (null === $source) {
return null;
}

if (\is_object($sourceData)) {
$source = \get_class($sourceData);
} elseif (\is_array($sourceData)) {
$source = 'array';
if (\is_object($source)) {
$sourceType = \get_class($source);
} elseif (\is_array($source)) {
$sourceType = 'array';
}

if (null === $source) {
if (null === $sourceType) {
throw new NoMappingFoundException('Cannot map this value, source is neither an object or an array.');
}

if (\is_object($targetData)) {
$target = \get_class($targetData);
$context[MapperContext::TARGET_TO_POPULATE] = $targetData;
} elseif (\is_array($targetData)) {
$target = 'array';
$context[MapperContext::TARGET_TO_POPULATE] = $targetData;
} elseif (\is_string($targetData)) {
$target = $targetData;
if (\is_object($target)) {
$targetType = \get_class($target);
$context[MapperContext::TARGET_TO_POPULATE] = $target;
} elseif (\is_array($target)) {
$targetType = 'array';
$context[MapperContext::TARGET_TO_POPULATE] = $target;
} elseif (\is_string($target)) {
$targetType = $target;
}

if (null === $target) {
if (null === $targetType) {
throw new NoMappingFoundException('Cannot map this value, target is neither an object or an array.');
}

if ('array' === $source && 'array' === $target) {
if ('array' === $sourceType && 'array' === $targetType) {
throw new NoMappingFoundException('Cannot map this value, both source and target are array.');
}

return $this->getMapper($source, $target)->map($sourceData, $context);
return $this->getMapper($sourceType, $targetType)->map($source, $context);
}

/**
* {@inheritdoc}
*/
public function getMetadata(string $source, string $target): ?MapperGeneratorMetadataInterface
{
if (!isset($this->metadata[$source][$target])) {
Expand All @@ -167,27 +144,21 @@ public function getMetadata(string $source, string $target): ?MapperGeneratorMet
return $this->metadata[$source][$target];
}

/**
* {@inheritdoc}
*/
public function bindTransformerFactory(TransformerFactoryInterface $transformerFactory): void
{
if (!$this->chainTransformerFactory->hasTransformerFactory($transformerFactory)) {
$this->chainTransformerFactory->addTransformerFactory($transformerFactory);
}
}

/**
* Create an automapper.
*/
public static function create(
bool $private = true,
ClassLoaderInterface $loader = null,
AdvancedNameConverterInterface $nameConverter = null,
string $classPrefix = 'Mapper_',
bool $attributeChecking = true,
bool $autoRegister = true,
string $dateTimeFormat = \DateTime::RFC3339,
string $dateTimeFormat = \DateTimeInterface::RFC3339,
bool $allowReadOnlyTargetToPopulate = false
): self {
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
Expand Down
8 changes: 5 additions & 3 deletions src/AutoMapperInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace AutoMapper;

/**
Expand All @@ -12,11 +14,11 @@ interface AutoMapperInterface
/**
* Maps data from a source to a target.
*
* @param array|object $source Any data object, which may be an object or an array
* @param array|object|null $source Any data object, which may be an object or an array
* @param string|array|object $target To which type of data, or data, the source should be mapped
* @param array $context Mapper context
*
* @return array|object The mapped object
* @return array|object|null The mapped object
*/
public function map($source, $target, array $context = []);
public function map(null|array|object $source, string|array|object $target, array $context = []): null|array|object;
}
2 changes: 2 additions & 0 deletions src/AutoMapperRegistryInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace AutoMapper;

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Exception/CircularReferenceException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony package.
*
Expand All @@ -14,6 +16,6 @@
/**
* @author Joel Wurtz <[email protected]>
*/
class CircularReferenceException extends RuntimeException
final class CircularReferenceException extends RuntimeException
{
}
4 changes: 3 additions & 1 deletion src/Exception/CompileException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony package.
*
Expand All @@ -14,6 +16,6 @@
/**
* @author Joel Wurtz <[email protected]>
*/
class CompileException extends RuntimeException
final class CompileException extends RuntimeException
{
}
4 changes: 3 additions & 1 deletion src/Exception/InvalidMappingException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony package.
*
Expand All @@ -14,6 +16,6 @@
/**
* @author Joel Wurtz <[email protected]>
*/
class InvalidMappingException extends RuntimeException
final class InvalidMappingException extends RuntimeException
{
}
4 changes: 3 additions & 1 deletion src/Exception/NoMappingFoundException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony package.
*
Expand All @@ -14,6 +16,6 @@
/**
* @author Joel Wurtz <[email protected]>
*/
class NoMappingFoundException extends RuntimeException
final class NoMappingFoundException extends RuntimeException
{
}
6 changes: 4 additions & 2 deletions src/Exception/ReadOnlyTargetException.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Exception;

use AutoMapper\MapperContext;

/**
* @author Baptiste Leduc <[email protected]>
*/
class ReadOnlyTargetException extends RuntimeException
final class ReadOnlyTargetException extends RuntimeException
{
public function __construct(int $code = 0, ?Throwable $previous = null)
public function __construct(int $code = 0, ?\Throwable $previous = null)
{
parent::__construct(sprintf('Cannot use readonly class as an object to populate. You can opt-out this behavior by using the context "%s"', MapperContext::ALLOW_READONLY_TARGET_TO_POPULATE), $code, $previous);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Exception;

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

declare(strict_types=1);

namespace AutoMapper\Extractor;

use AutoMapper\Exception\InvalidMappingException;
Expand All @@ -23,18 +25,17 @@ final class FromSourceMappingExtractor extends MappingExtractor
{
private const ALLOWED_TARGETS = ['array', \stdClass::class];

private $nameConverter;

public function __construct(PropertyInfoExtractorInterface $propertyInfoExtractor, PropertyReadInfoExtractorInterface $readInfoExtractor, PropertyWriteInfoExtractorInterface $writeInfoExtractor, TransformerFactoryInterface $transformerFactory, ClassMetadataFactoryInterface $classMetadataFactory = null, AdvancedNameConverterInterface $nameConverter = null)
{
public function __construct(
PropertyInfoExtractorInterface $propertyInfoExtractor,
PropertyReadInfoExtractorInterface $readInfoExtractor,
PropertyWriteInfoExtractorInterface $writeInfoExtractor,
TransformerFactoryInterface $transformerFactory,
ClassMetadataFactoryInterface $classMetadataFactory = null,
private readonly ?AdvancedNameConverterInterface $nameConverter = null,
) {
parent::__construct($propertyInfoExtractor, $readInfoExtractor, $writeInfoExtractor, $transformerFactory, $classMetadataFactory);

$this->nameConverter = $nameConverter;
}

/**
* {@inheritdoc}
*/
public function getPropertiesMapping(MapperMetadataInterface $mapperMetadata): array
{
$sourceProperties = $this->propertyInfoExtractor->getProperties($mapperMetadata->getSource());
Expand Down Expand Up @@ -123,9 +124,6 @@ private function transformType(string $target, Type $type = null): ?Type
);
}

/**
* {@inheritdoc}
*/
public function getWriteMutator(string $source, string $target, string $property, array $context = []): WriteMutator
{
if (null !== $this->nameConverter) {
Expand Down
Loading

0 comments on commit fff6e0b

Please sign in to comment.