forked from janephp/automapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapperGeneratorMetadataFactory.php
60 lines (51 loc) · 2.34 KB
/
MapperGeneratorMetadataFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace Jane\AutoMapper;
use Jane\AutoMapper\Extractor\FromSourceMappingExtractor;
use Jane\AutoMapper\Extractor\FromTargetMappingExtractor;
use Jane\AutoMapper\Extractor\SourceTargetMappingExtractor;
/**
* Metadata factory, used to autoregistering new mapping without creating them.
*
* @author Joel Wurtz <[email protected]>
*/
final class MapperGeneratorMetadataFactory implements MapperGeneratorMetadataFactoryInterface
{
private $sourceTargetPropertiesMappingExtractor;
private $fromSourcePropertiesMappingExtractor;
private $fromTargetPropertiesMappingExtractor;
private $classPrefix;
private $attributeChecking;
private $dateTimeFormat;
public function __construct(
SourceTargetMappingExtractor $sourceTargetPropertiesMappingExtractor,
FromSourceMappingExtractor $fromSourcePropertiesMappingExtractor,
FromTargetMappingExtractor $fromTargetPropertiesMappingExtractor,
string $classPrefix = 'Mapper_',
bool $attributeChecking = true,
string $dateTimeFormat = \DateTime::RFC3339
) {
$this->sourceTargetPropertiesMappingExtractor = $sourceTargetPropertiesMappingExtractor;
$this->fromSourcePropertiesMappingExtractor = $fromSourcePropertiesMappingExtractor;
$this->fromTargetPropertiesMappingExtractor = $fromTargetPropertiesMappingExtractor;
$this->classPrefix = $classPrefix;
$this->attributeChecking = $attributeChecking;
$this->dateTimeFormat = $dateTimeFormat;
}
/**
* Create metadata for a source and target.
*/
public function create(MapperGeneratorMetadataRegistryInterface $autoMapperRegister, string $source, string $target): MapperGeneratorMetadataInterface
{
$extractor = $this->sourceTargetPropertiesMappingExtractor;
if ('array' === $source || 'stdClass' === $source) {
$extractor = $this->fromTargetPropertiesMappingExtractor;
}
if ('array' === $target || 'stdClass' === $target) {
$extractor = $this->fromSourcePropertiesMappingExtractor;
}
$mapperMetadata = new MapperMetadata($autoMapperRegister, $extractor, $source, $target, $this->classPrefix);
$mapperMetadata->setAttributeChecking($this->attributeChecking);
$mapperMetadata->setDateTimeFormat($this->dateTimeFormat);
return $mapperMetadata;
}
}