1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2013-2014 eBay Enterprise, Inc. |
4
|
|
|
* |
5
|
|
|
* NOTICE OF LICENSE |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
8
|
|
|
* that is bundled with this package in the file LICENSE.md. |
9
|
|
|
* It is also available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* @copyright Copyright (c) 2013-2014 eBay Enterprise, Inc. (http://www.ebayenterprise.com/) |
13
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace eBayEnterprise\RetailOrderManagement\Payload; |
17
|
|
|
|
18
|
|
|
use eBayEnterprise\RetailOrderManagement\Payload\Exception\UnsupportedPayload; |
19
|
|
|
use eBayEnterprise\RetailOrderManagement\Payload\ConfigLocator; |
20
|
|
|
use Psr\Log\LoggerInterface; |
21
|
|
|
use Psr\Log\NullLogger; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Abstract factory implementation for building payload objects. |
25
|
|
|
*/ |
26
|
|
|
class PayloadFactory implements IPayloadFactory |
27
|
|
|
{ |
28
|
|
|
/** @var ILocator Maps a payload type to configuration used to construct the payload */ |
29
|
|
|
protected $locator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param ILocator |
33
|
|
|
*/ |
34
|
|
|
public function __construct(ILocator $locator = null) |
35
|
|
|
{ |
36
|
|
|
// If no custom payload locator is used, default to a locator using |
37
|
|
|
// the PayloadConfigMap. |
38
|
|
|
$this->locator = $locator ?: new ConfigMapLocator; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Build a payload using the payload config mappings. |
43
|
|
|
* @param string $type Concrete payload type |
44
|
|
|
* @param IPayloadMap |
45
|
|
|
* @param LoggerInterface |
46
|
|
|
* @param IPayload |
47
|
|
|
* @return IPayload |
48
|
|
|
* @throws UnsupportedPayload If the payload type is not configured |
49
|
|
|
*/ |
50
|
|
|
public function buildPayload($type, IPayloadMap $cascadedPayloadMap = null, IPayload $parentPayload = null, LoggerInterface $logger = null) |
51
|
|
|
{ |
52
|
|
|
if ($this->locator->hasPayloadConfig($type)) { |
53
|
|
|
$payloadConfig = $this->locator->getPayloadConfig($type); |
54
|
|
|
|
55
|
|
|
$validatorIteratorConfig = $payloadConfig['validatorIterator']; |
56
|
|
|
$validatorsConfig = $payloadConfig['validators']; |
57
|
|
|
$schemaValidatorConfig = $payloadConfig['schemaValidator']; |
58
|
|
|
$payloadMapConfig = $payloadConfig['childPayloads']['payloadMap']; |
59
|
|
|
$childPayloadsConfig = $payloadConfig['childPayloads']['types']; |
60
|
|
|
|
61
|
|
|
$validatorIterator = new $validatorIteratorConfig($this->buildValidators($validatorsConfig)); |
62
|
|
|
$schemaValidator = new $schemaValidatorConfig(); |
63
|
|
|
/** @var IPayloadMap $payloadMap */ |
64
|
|
|
$payloadMap = new $payloadMapConfig($childPayloadsConfig); |
65
|
|
|
if ($cascadedPayloadMap) { |
66
|
|
|
$payloadMap->merge($cascadedPayloadMap); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return new $type($validatorIterator, $schemaValidator, $payloadMap, $logger ?: new NullLogger(), $parentPayload); |
70
|
|
|
} |
71
|
|
|
throw new UnsupportedPayload("No configuration found for '$type'"); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Instantiate each validator and store it in an array |
76
|
|
|
* to be passed to IValidatorIterator |
77
|
|
|
* |
78
|
|
|
* @param $validators |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
protected function buildValidators($validators) |
82
|
|
|
{ |
83
|
|
|
return array_map( |
84
|
|
|
function ($validatorConfig) { |
85
|
|
|
return new $validatorConfig['validator']($validatorConfig['params']); |
86
|
|
|
}, |
87
|
|
|
$validators |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|