1 <?php
2 3 4
5 namespace Team3\PayU\Order\Transformer\UserOrder\Strategy\ShippingMethod;
6
7 use Team3\PayU\Order\Model\OrderInterface;
8 use Team3\PayU\Order\Transformer\UserOrder\TransformerProperties;
9 use Team3\PayU\PropertyExtractor\ExtractorException;
10 use Team3\PayU\PropertyExtractor\ExtractorResult;
11 use Team3\PayU\Order\Transformer\UserOrder\Strategy\UserOrderTransformerStrategyInterface;
12
13 class ShippingMethodCollectionTransformer implements UserOrderTransformerStrategyInterface
14 {
15 16 17
18 private $singleShippingMethodTransformer;
19
20 21 22
23 public function __construct(
24 SingleShippingMethodTransformer $singleShippingMethodTransformer
25 ) {
26 $this->singleShippingMethodTransformer = $singleShippingMethodTransformer;
27 }
28
29 30 31
32 public function transform(
33 OrderInterface $order,
34 ExtractorResult $extractorResult
35 ) {
36
37 $shippingMethodCollection = $extractorResult->getValue();
38 $this->checkCollection($shippingMethodCollection);
39
40 foreach ($shippingMethodCollection as $usersShippingMethod) {
41 $order
42 ->getShippingMethodCollection()
43 ->addShippingMethod(
44 $this
45 ->singleShippingMethodTransformer
46 ->transform(
47 $usersShippingMethod
48 )
49 );
50 }
51 }
52
53 54 55
56 public function supports($propertyName)
57 {
58 return TransformerProperties::SHIPPING_METHOD_COLLECTION === $propertyName;
59 }
60
61 62 63 64 65
66 protected function checkCollection($collection)
67 {
68 if (!is_array($collection)
69 && !$collection instanceof \Traversable) {
70 throw new ExtractorException(sprintf(
71 'Array or object which implements Traversable was expected, but %s was given',
72 gettype($collection)
73 ));
74 }
75 }
76 }
77