|
1
|
|
|
<?php namespace Bedard\Shop\Classes; |
|
2
|
|
|
|
|
3
|
|
|
use Faker; |
|
4
|
|
|
use Faker\Provider\Lorem; |
|
5
|
|
|
use Model; |
|
6
|
|
|
|
|
7
|
|
|
class Factory |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Create a model and save it to the database. |
|
11
|
|
|
* |
|
12
|
|
|
* @param Model $model Model to create |
|
13
|
|
|
* @param array $omit Data to fill model with |
|
14
|
|
|
* @return Model |
|
15
|
|
|
*/ |
|
16
|
|
|
public static function create(Model $model, array $data = [], array $omit = []) |
|
17
|
|
|
{ |
|
18
|
|
|
$model = self::fill($model, $data, $omit); |
|
19
|
|
|
$model->save(); |
|
20
|
|
|
|
|
21
|
|
|
return $model; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Create a model and fill it with data. |
|
26
|
|
|
* |
|
27
|
|
|
* @param Model $model Model to fill |
|
28
|
|
|
* @param array $data Data to fill the model with |
|
29
|
|
|
* @return Model |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function fill(Model $model, array $data = [], array $omit = []) |
|
32
|
|
|
{ |
|
33
|
|
|
$seed = []; |
|
34
|
|
|
|
|
35
|
|
|
switch (get_class($model)) { |
|
36
|
|
|
case 'Bedard\Shop\Models\Cart': $seed = self::getCartData($data); break; |
|
|
|
|
|
|
37
|
|
|
case 'Bedard\Shop\Models\CartItem': $seed = self::getCartItemData($data); break; |
|
|
|
|
|
|
38
|
|
|
case 'Bedard\Shop\Models\Category': $seed = self::getCategoryData($data); break; |
|
|
|
|
|
|
39
|
|
|
case 'Bedard\Shop\Models\Inventory': $seed = self::getInventoryData($data); break; |
|
|
|
|
|
|
40
|
|
|
case 'Bedard\Shop\Models\Option': $seed = self::getOptionData($data); break; |
|
|
|
|
|
|
41
|
|
|
case 'Bedard\Shop\Models\Product': $seed = self::getProductData($data); break; |
|
|
|
|
|
|
42
|
|
|
case 'Bedard\Shop\Models\Status': $seed = self::getStatusData($data); break; |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$model->fill(array_merge($seed, $data)); |
|
46
|
|
|
|
|
47
|
|
|
foreach ($omit as $key) { |
|
48
|
|
|
unset($model->attributes[$key]); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $model; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Cart. |
|
56
|
|
|
* |
|
57
|
|
|
* @param array $data |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function getCartData(array $data = []) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
return [ |
|
63
|
|
|
// |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* CartItem. |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $data |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function getCartItemData(array $data = []) |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
|
|
return [ |
|
76
|
|
|
'cart_id' => 0, |
|
77
|
|
|
'inventory_id' => 0, |
|
78
|
|
|
'product_id' => 0, |
|
79
|
|
|
]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Category. |
|
84
|
|
|
* |
|
85
|
|
|
* @param array $data |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
|
|
public static function getCategoryData(array $data = []) |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
$faker = Faker\Factory::create(); |
|
91
|
|
|
|
|
92
|
|
|
return [ |
|
93
|
|
|
'description_html' => '<p>'.Lorem::paragraph().'</p>', |
|
94
|
|
|
'name' => $faker->words(2, true), |
|
95
|
|
|
'slug' => $faker->slug, |
|
96
|
|
|
]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public static function getInventoryData(array $data = []) |
|
|
|
|
|
|
100
|
|
|
{ |
|
101
|
|
|
$faker = Faker\Factory::create(); |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
return [ |
|
104
|
|
|
'sku' => null, |
|
105
|
|
|
'quantity' => rand(0, 10), |
|
106
|
|
|
]; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Option. |
|
111
|
|
|
* |
|
112
|
|
|
* @param array $data |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
public static function getOptionData(array $data = []) |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
|
|
$faker = Faker\Factory::create(); |
|
118
|
|
|
|
|
119
|
|
|
return [ |
|
120
|
|
|
'name' => $faker->words(2, true), |
|
121
|
|
|
'placeholder' => $faker->words(3, true), |
|
122
|
|
|
'sort_order' => 0, |
|
123
|
|
|
'value_data' => [ |
|
124
|
|
|
['_key' => 1, 'id' => null, 'name' => 'a', 'sort_order' => 0], |
|
125
|
|
|
], |
|
126
|
|
|
]; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Category. |
|
131
|
|
|
* |
|
132
|
|
|
* @param array $data |
|
133
|
|
|
* @return array |
|
134
|
|
|
*/ |
|
135
|
|
|
public static function getProductData(array $data = []) |
|
|
|
|
|
|
136
|
|
|
{ |
|
137
|
|
|
$faker = Faker\Factory::create(); |
|
138
|
|
|
|
|
139
|
|
|
return [ |
|
140
|
|
|
'base_price' => rand(1, 100) + (rand(0, 100) / 100), |
|
141
|
|
|
'description_html' => '<p>'.Lorem::paragraph().'</p>', |
|
142
|
|
|
'name' => $faker->words(2, true), |
|
143
|
|
|
'slug' => $faker->slug, |
|
144
|
|
|
]; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Status. |
|
149
|
|
|
* |
|
150
|
|
|
* @param array $data |
|
151
|
|
|
* @return array |
|
152
|
|
|
*/ |
|
153
|
|
|
public static function getStatusData(array $data = []) |
|
|
|
|
|
|
154
|
|
|
{ |
|
155
|
|
|
$faker = Faker\Factory::create(); |
|
156
|
|
|
|
|
157
|
|
|
return [ |
|
158
|
|
|
'is_abandoned' => false, |
|
159
|
|
|
'is_default' => false, |
|
160
|
|
|
'is_reducing' => false, |
|
161
|
|
|
'name' => $faker->words(2, true), |
|
162
|
|
|
]; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.