-
Notifications
You must be signed in to change notification settings - Fork 1
/
MapBuilder.php
170 lines (150 loc) · 4.34 KB
/
MapBuilder.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* This file is part of the opportus/object-mapper project.
*
* Copyright (c) 2018-2022 Clément Cazaud <[email protected]>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
namespace Opportus\ObjectMapper\Map;
use Opportus\ObjectMapper\PathFinder\DynamicSourceToStaticTargetPathFinder;
use Opportus\ObjectMapper\PathFinder\StaticSourceToDynamicTargetPathFinder;
use Opportus\ObjectMapper\PathFinder\PathFinderCollection;
use Opportus\ObjectMapper\PathFinder\PathFinderInterface;
use Opportus\ObjectMapper\PathFinder\StaticPathFinder;
use Opportus\ObjectMapper\Route\RouteBuilderInterface;
use Opportus\ObjectMapper\Route\RouteCollection;
use Opportus\ObjectMapper\Route\RouteInterface;
/**
* The map builder.
*
* @package Opportus\ObjectMapper\Map
* @author Clément Cazaud <[email protected]>
* @license https://github.com/opportus/object-mapper/blob/master/LICENSE MIT
*/
class MapBuilder implements MapBuilderInterface
{
/**
* @var RouteBuilderInterface $routeBuilder
*/
private $routeBuilder;
/**
* @var RouteCollection $routes
*/
private $routes;
/**
* @var PathFinderCollection $pathFinders
*/
private $pathFinders;
/**
* Constructs the map builder.
*
* @param RouteBuilderInterface $routeBuilder
* @param null|RouteCollection $routes
* @param null|PathFinderCollection $pathFinders
*/
public function __construct(
RouteBuilderInterface $routeBuilder,
?RouteCollection $routes = null,
?PathFinderCollection $pathFinders = null
) {
$this->routeBuilder = $routeBuilder;
$this->routes = $routes ?? new RouteCollection();
$this->pathFinders = $pathFinders ?? new PathFinderCollection();
}
/**
* {@inheritdoc}
*/
public function getRouteBuilder(): RouteBuilderInterface
{
return $this->routeBuilder->setMapBuilder($this);
}
/**
* {@inheritdoc}
*/
public function addRoute(RouteInterface $route): MapBuilderInterface
{
$routes = $this->routes->toArray();
$routes[] = $route;
return new self(
$this->routeBuilder,
new RouteCollection($routes),
$this->pathFinders
);
}
/**
* {@inheritdoc}
*/
public function addRoutes(RouteCollection $routes): MapBuilderInterface
{
$routes = $routes->toArray() + $this->routes->toArray();
return new self(
$this->routeBuilder,
new RouteCollection($routes),
$this->pathFinders
);
}
/**
* {@inheritdoc}
*/
public function addPathFinder(
PathFinderInterface $pathFinder,
?int $priority = null
): MapBuilderInterface {
$pathFinders = $this->pathFinders->toArray();
if (null === $priority) {
$pathFinders[] = $pathFinder;
} else {
$pathFinders[$priority] = $pathFinder;
}
return new self(
$this->routeBuilder,
$this->routes,
new PathFinderCollection($pathFinders)
);
}
/**
* {@inheritdoc}
*/
public function addStaticPathFinder(
?int $priority = null,
bool $recursiveMode = true
): MapBuilderInterface {
return $this->addPathFinder(
new StaticPathFinder($this->routeBuilder, $recursiveMode),
$priority
);
}
/**
* {@inheritdoc}
*/
public function addStaticSourceToDynamicTargetPathFinder(
?int $priority = null,
bool $recursiveMode = true
): MapBuilderInterface {
return $this->addPathFinder(
new StaticSourceToDynamicTargetPathFinder($this->routeBuilder, $recursiveMode),
$priority
);
}
/**
* {@inheritdoc}
*/
public function addDynamicSourceToStaticTargetPathFinder(
?int $priority = null,
bool $recursiveMode = true
): MapBuilderInterface {
return $this->addPathFinder(
new DynamicSourceToStaticTargetPathFinder($this->routeBuilder, $recursiveMode),
$priority
);
}
/**
* {@inheritdoc}
*/
public function getMap(): MapInterface
{
return new Map($this->pathFinders, $this->routes);
}
}