1 <?php
2 3 4
5 namespace Team3\PayU\Order\Transformer\UserOrder;
6
7 use Team3\PayU\Order\Model\Order;
8 use Team3\PayU\Order\Model\OrderInterface;
9 use Team3\PayU\PropertyExtractor\ExtractorInterface;
10 use Team3\PayU\PropertyExtractor\ExtractorResult;
11 use Team3\PayU\Order\Transformer\UserOrder\Strategy\UserOrderTransformerStrategyInterface;
12
13 class UserOrderTransformer implements UserOrderTransformerInterface
14 {
15 16 17
18 protected $strategies;
19
20 21 22
23 protected $extractor;
24
25 26 27
28 public function __construct(ExtractorInterface $extractor)
29 {
30 $this->extractor = $extractor;
31 $this->strategies = [];
32 }
33
34 35 36
37 public function transform(OrderInterface $order, $userOrder)
38 {
39 foreach ($this->getExtractedResults($userOrder) as $extractionResult) {
40 foreach ($this->strategies as $strategy) {
41 if ($strategy->supports($extractionResult->getPropertyName())) {
42 $strategy->transform(
43 $order,
44 $extractionResult
45 );
46 }
47 }
48 }
49
50 return $order;
51 }
52
53 54 55
56 public function addStrategy(
57 UserOrderTransformerStrategyInterface $userOrderTransformerStrategy
58 ) {
59 $this->strategies[] = $userOrderTransformerStrategy;
60 }
61
62 63 64 65 66
67 protected function getExtractedResults($userOrder)
68 {
69 return $this
70 ->extractor
71 ->extract($userOrder);
72 }
73 }
74