1 <?php
2 3 4
5 namespace Team3\PayU\Order\Transformer\UserOrder\Strategy\ShippingMethod;
6
7 use Team3\PayU\Order\Model\ShippingMethods\ShippingMethod;
8 use Team3\PayU\Order\Model\ShippingMethods\ShippingMethodInterface;
9 use Team3\PayU\Order\Transformer\UserOrder\TransformerProperties;
10 use Team3\PayU\Order\Transformer\UserOrder\TransformerPropertiesRegExp;
11 use Team3\PayU\PropertyExtractor\ExtractorInterface;
12 use Team3\PayU\PropertyExtractor\ExtractorResult;
13
14 class SingleShippingMethodTransformer
15 {
16 17 18
19 private $extractor;
20
21 22 23
24 public function __construct(
25 ExtractorInterface $extractor
26 ) {
27 $this->extractor = $extractor;
28 }
29
30 31 32 33 34
35 public function transform($usersShippingMethod)
36 {
37 $shippingMethod = new ShippingMethod();
38
39 foreach ($this->extractor->extract($usersShippingMethod) as $extractedResult) {
40 if ($this->supports($extractedResult->getPropertyName())) {
41 $this->copyValue($shippingMethod, $extractedResult);
42 }
43 }
44
45 return $shippingMethod;
46 }
47
48 49 50 51 52
53 protected function supports($propertyName)
54 {
55 return 1 === preg_match(
56 TransformerPropertiesRegExp::SHIPPING_METHOD_REGEXP,
57 $propertyName
58 );
59 }
60
61 62 63 64
65 protected function copyValue(
66 ShippingMethodInterface $shippingMethod,
67 ExtractorResult $extractorResult
68 ) {
69 switch ($extractorResult->getPropertyName()) {
70 case TransformerProperties::SHIPPING_METHOD_NAME:
71 $shippingMethod->setName($extractorResult->getValue());
72 break;
73 case TransformerProperties::SHIPPING_METHOD_PRICE:
74 $shippingMethod->setPrice($extractorResult->getValue());
75 break;
76 case TransformerProperties::SHIPPING_METHOD_COUNTRY:
77 $shippingMethod->setCountry($extractorResult->getValue());
78 break;
79 default:
80 }
81 }
82 }
83