-
Notifications
You must be signed in to change notification settings - Fork 1
/
Map.php
98 lines (82 loc) · 2.65 KB
/
Map.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?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\Map;
use Exception;
use Opportus\ObjectMapper\Exception\InvalidOperationException;
use Opportus\ObjectMapper\PathFinder\PathFinderCollection;
use Opportus\ObjectMapper\Route\RouteCollection;
use Opportus\ObjectMapper\SourceInterface;
use Opportus\ObjectMapper\TargetInterface;
/**
* The map.
*
* @package Opportus\ObjectMapper\Map
* @author Clément Cazaud <clement.cazaud@gmail.com>
* @license https://github.com/opportus/object-mapper/blob/master/LICENSE MIT
*/
class Map implements MapInterface
{
/**
* @var PathFinderCollection $pathFinders
*/
private $pathFinders;
/**
* @var RouteCollection $routes
*/
private $routes;
/**
* Constructs the map.
*
* @param null|PathFinderCollection $pathFinders
* @param null|RouteCollection $routes
*/
public function __construct(
?PathFinderCollection $pathFinders = null,
?RouteCollection $routes = null
) {
$this->pathFinders = $pathFinders ?? new PathFinderCollection();
$this->routes = $routes ?? new RouteCollection();
}
/**
* {@inheritdoc}
*/
public function getRoutes(SourceInterface $source, TargetInterface $target): RouteCollection
{
$routes = [];
foreach ($this->pathFinders as $pathFinder) {
try {
$pathFinderRoutes = $pathFinder->getRoutes($source, $target);
} catch (Exception $exception) {
throw new InvalidOperationException('', 0, $exception);
}
foreach ($pathFinderRoutes as $pathFinderRoute) {
$routes[] = $pathFinderRoute;
}
}
foreach ($this->routes as $mapRoute) {
$sourceFqn = $mapRoute->getSourcePoint()->getSourceFqn();
$targetFqn = $mapRoute->getTargetPoint()->getTargetFqn();
if ($sourceFqn !== $source->getFqn() || $targetFqn !== $target->getFqn()) {
continue;
}
$routes[] = $mapRoute;
}
$routesToKeep = [];
foreach ($routes as $key => $route) {
$routesToKeep[$route->getTargetPoint()->getFqn()] = $key;
}
foreach ($routes as $key => $route) {
if (false === \in_array($key, $routesToKeep)) {
unset($routes[$key]);
}
}
return new RouteCollection($routes);
}
}