1 <?php
 2  3  4 
 5 namespace Team3\PayU\Order\Transformer\UserOrder\Strategy\Product;
 6 
 7 use Team3\PayU\Order\Model\OrderInterface;
 8 use Team3\PayU\Order\Transformer\UserOrder\TransformerProperties;
 9 use Team3\PayU\PropertyExtractor\ExtractorResult;
10 use Team3\PayU\Order\Transformer\UserOrder\Strategy\UserOrderTransformerStrategyInterface;
11 use Team3\PayU\Order\Transformer\UserOrder\UserOrderTransformerException;
12 
13 class ProductCollectionTransformer implements UserOrderTransformerStrategyInterface
14 {
15     16 17 
18     protected $singleProductTransformer;
19 
20     21 22 
23     public function __construct(
24         SingleProductTransformer $singleProductTransformer
25     ) {
26         $this->singleProductTransformer = $singleProductTransformer;
27     }
28 
29     30 31 
32     public function transform(
33         OrderInterface $order,
34         ExtractorResult $extractorResult
35     ) {
36         $usersProductCollection = $extractorResult->getValue();
37         $this->checkProductCollection($usersProductCollection);
38 
39         foreach ($usersProductCollection as $userProduct) {
40             $order
41                 ->getProductCollection()
42                 ->addProduct(
43                     $this
44                         ->singleProductTransformer
45                         ->transform($userProduct)
46                 );
47         }
48 
49         return $order;
50     }
51 
52     53 54 
55     public function supports($propertyName)
56     {
57         return TransformerProperties::PRODUCT_COLLECTION === $propertyName;
58     }
59 
60     61 62 63 64 65 66 67 68 
69     private function checkProductCollection(
70         $productCollection
71     ) {
72         if (!is_array($productCollection)
73             && !$productCollection instanceof \Traversable) {
74             throw new UserOrderTransformerException(sprintf(
75                 'Value should be an array or object which implements \Traversable, but it returns %s',
76                 gettype($productCollection)
77             ));
78         }
79     }
80 }
81