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
<?php
/**
* @copyright Vertex. All rights reserved. https://www.vertexinc.com/
* @author Mediotype Development <diveinto@mediotype.com>
*/
namespace Vertex\Mapper;
/**
* Retrieve an instance of a mapper
*
* @api
*/
class MapperFactory
{
/**
* List of api-specific mapping implementations for given interfaces
*
* Keys in the outer array should be the interface that would be mapped. Their value should be an associative array
* where the key is the API level and the value is a mapper valid for that API level
*
* @var array
*/
private $config;
/**
* List of object instances
*
* Keys in the array are the class name with the value being the instance
*
* @var array
*/
private $instances;
/**
* @param array|null $config
*/
public function __construct(array $config = null)
{
if ($config === null) {
$config = [
'Vertex\Mapper\AuthenticatorInterface' => [
'60' => 'Vertex\Mapper\Api60\Authenticator',
'70' => 'Vertex\Mapper\Api60\Authenticator',
],
'Vertex\Data\AddressInterface' => [
'60' => 'Vertex\Mapper\Api60\AddressMapper',
'70' => 'Vertex\Mapper\Api60\AddressMapper',
],
'Vertex\Data\CustomerInterface' => [
'60' => 'Vertex\Mapper\Api60\CustomerMapper',
'70' => 'Vertex\Mapper\Api70\CustomerMapper',
],
'Vertex\Data\JurisdictionInterface' => [
'60' => 'Vertex\Mapper\Api60\JurisdictionMapper',
'70' => 'Vertex\Mapper\Api60\JurisdictionMapper',
],
'Vertex\Data\LineItemInterface' => [
'60' => 'Vertex\Mapper\Api60\LineItemMapper',
'70' => 'Vertex\Mapper\Api70\LineItemMapper',
],
'Vertex\Data\LoginInterface' => [
'60' => 'Vertex\Mapper\Api60\LoginMapper',
'70' => 'Vertex\Mapper\Api60\LoginMapper',
],
'Vertex\Data\SellerInterface' => [
'60' => 'Vertex\Mapper\Api60\SellerMapper',
'70' => 'Vertex\Mapper\Api60\SellerMapper',
],
'Vertex\Data\TaxAreaLookupResultInterface' => [
'60' => 'Vertex\Mapper\Api60\TaxAreaLookupResultMapper',
'70' => 'Vertex\Mapper\Api60\TaxAreaLookupResultMapper',
],
'Vertex\Data\TaxInterface' => [
'60' => 'Vertex\Mapper\Api60\TaxMapper',
'70' => 'Vertex\Mapper\Api60\TaxMapper',
],
'Vertex\Data\TaxRegistrationInterface' => [
'60' => 'Vertex\Mapper\Api60\TaxRegistrationMapper',
'70' => 'Vertex\Mapper\Api70\TaxRegistrationMapper',
],
'Vertex\Services\Invoice\RequestInterface' => [
'60' => 'Vertex\Mapper\Api60\InvoiceRequestMapper',
'70' => 'Vertex\Mapper\Api70\InvoiceRequestMapper',
],
'Vertex\Services\Invoice\ResponseInterface' => [
'60' => 'Vertex\Mapper\Api60\InvoiceResponseMapper',
'70' => 'Vertex\Mapper\Api70\InvoiceResponseMapper',
],
'Vertex\Services\TaxAreaLookup\RequestInterface' => [
'60' => 'Vertex\Mapper\Api60\TaxAreaLookupRequestMapper',
'70' => 'Vertex\Mapper\Api60\TaxAreaLookupRequestMapper',
],
'Vertex\Services\TaxAreaLookup\ResponseInterface' => [
'60' => 'Vertex\Mapper\Api60\TaxAreaLookupResponseMapper',
'70' => 'Vertex\Mapper\Api60\TaxAreaLookupResponseMapper',
],
'Vertex\Services\Quote\RequestInterface' => [
'60' => 'Vertex\Mapper\Api60\QuoteRequestMapper',
'70' => 'Vertex\Mapper\Api70\QuoteRequestMapper',
],
'Vertex\Services\Quote\ResponseInterface' => [
'60' => 'Vertex\Mapper\Api60\QuoteResponseMapper',
'70' => 'Vertex\Mapper\Api70\QuoteResponseMapper',
],
];
}
$this->config = $config;
}
/**
* Create an instance of an API-specific implementation of a mapper for the given interface
*
* @param string $class An interface you want an API-specific mapper for
* @param string $apiLevel The API level you need the mapper for
* @return mixed
*/
public function createForClass($class, $apiLevel)
{
$className = $this->getClassName($class, $apiLevel);
return new $className;
}
/**
* Retrieve the API-specific implementation of a mapper for the given interface
*
* @param string $class An interface you want an API-specific mapper for
* @param string $apiLevel The API level you need the mapper for
* @return mixed
*/
public function getForClass($class, $apiLevel)
{
$className = $this->getClassName($class, $apiLevel);
if (isset($this->instances[$className])) {
return $this->instances[$className];
}
$instance = $this->createForClass($class, $apiLevel);
$this->instances[$className] = $instance;
return $instance;
}
/**
* Retrieve the class name of the API-specific implementation of a mapper for the given interface
*
* @param string $class
* @param string $apiLevel
* @return string
*/
private function getClassName($class, $apiLevel)
{
if (!isset($this->config[$class])) {
throw new \InvalidArgumentException('No configured mappers for ' . $class);
}
if (!isset($this->config[$class][$apiLevel])) {
throw new \InvalidArgumentException('No configured mapper for ' . $class . ' in API Level ' . $apiLevel);
}
return $this->config[$class][$apiLevel];
}
}