1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Orders\Factory; |
4
|
|
|
|
5
|
|
|
use Dynamic\Foxy\Orders\Model\OrderOption; |
6
|
|
|
use SilverStripe\Core\Config\Configurable; |
7
|
|
|
use SilverStripe\Core\Injector\Injectable; |
8
|
|
|
use SilverStripe\ORM\ArrayList; |
9
|
|
|
use SilverStripe\View\ArrayData; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class OrderOptionFactory |
13
|
|
|
* @package Dynamic\Foxy\Orders\Factory |
14
|
|
|
*/ |
15
|
|
|
class OrderOptionFactory |
16
|
|
|
{ |
17
|
|
|
use Configurable; |
18
|
|
|
use Injectable; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var |
22
|
|
|
*/ |
23
|
|
|
private $order_options; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ArrayData |
27
|
|
|
*/ |
28
|
|
|
private $foxy_product; |
29
|
|
|
|
30
|
|
|
public function __construct(ArrayData $foxyProduct = null) |
31
|
|
|
{ |
32
|
|
|
if ($foxyProduct instanceof ArrayData && $foxyProduct !== null) { |
33
|
|
|
$this->setFoxyProduct($foxyProduct); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $foxyProduct |
39
|
|
|
* @return $this |
40
|
|
|
*/ |
41
|
|
|
public function setFoxyProduct($foxyProduct) |
42
|
|
|
{ |
43
|
|
|
$this->foxy_product = $foxyProduct; |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return ArrayData |
50
|
|
|
*/ |
51
|
|
|
protected function getFoxyProduct() |
52
|
|
|
{ |
53
|
|
|
return $this->foxy_product; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return $this |
58
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
59
|
|
|
*/ |
60
|
|
|
protected function setOrderOptions() |
61
|
|
|
{ |
62
|
|
|
$options = ArrayList::create(); |
63
|
|
|
|
64
|
|
|
/** @var ArrayData $optionItem */ |
65
|
|
|
foreach ($this->getFoxyProduct()->transaction_detail_options as $optionItem) { |
66
|
|
|
$option = OrderOption::create(); |
67
|
|
|
|
68
|
|
|
foreach ($this->config()->get('option_mapping') as $foxyField => $ssField) { |
69
|
|
|
if ($optionItem->hasField($foxyField)) { |
70
|
|
|
$option->{$ssField} = $optionItem->getField($foxyField); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$option->write(); |
75
|
|
|
$options->push($option); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->order_options = $options; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return ArrayList |
85
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
86
|
|
|
*/ |
87
|
|
|
public function getOrderOptions() |
88
|
|
|
{ |
89
|
|
|
if (!$this->order_options instanceof ArrayList) { |
90
|
|
|
$this->setOrderOptions(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->order_options; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|