-
Notifications
You must be signed in to change notification settings - Fork 1
/
ObjectMapper.php
71 lines (61 loc) · 1.79 KB
/
ObjectMapper.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
61
62
63
64
65
66
67
68
69
70
71
<?php
/**
* This file is part of the opportus/object-mapper project.
*
* Copyright (c) 2018-2022 Clément Cazaud <clement.cazaud@gmail.com>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
namespace Opportus\ObjectMapper;
use Opportus\ObjectMapper\Exception\InvalidArgumentException;
use Opportus\ObjectMapper\Exception\InvalidOperationException;
use Opportus\ObjectMapper\Map\MapBuilderInterface;
use Opportus\ObjectMapper\Map\MapInterface;
/**
* The object mapper.
*
* @package Opportus\ObjectMapper
* @author Clément Cazaud <clement.cazaud@gmail.com>
* @license https://github.com/opportus/object-mapper/blob/master/LICENSE MIT
*/
class ObjectMapper implements ObjectMapperInterface
{
use ObjectMapperTrait;
/**
* @var MapBuilderInterface $mapBuilder
*/
private $mapBuilder;
/**
* Constructs the object mapper.
*
* @param MapBuilderInterface $mapBuilder
*/
public function __construct(MapBuilderInterface $mapBuilder)
{
$this->mapBuilder = $mapBuilder;
}
/**
* {@inheritdoc}
*/
public function map(
object $source,
$target,
?MapInterface $map = null
): ?object {
if (!$source instanceof SourceInterface) {
$source = new Source($source);
}
if (!$target instanceof TargetInterface) {
try {
$target = new Target($target);
} catch (InvalidArgumentException $exception) {
throw new InvalidArgumentException(2, '', 0, $exception);
}
}
$map = $map ?? $this->mapBuilder
->addStaticPathFinder()
->getMap();
return $this->mapSourceToTarget($source, $target, $map);
}
}