|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package Faker |
|
5
|
|
|
* @author Iurii Makukh <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2017, Iurii Makukh <[email protected]> |
|
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License 3.0 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace gplcart\modules\faker\models\generators; |
|
11
|
|
|
|
|
12
|
|
|
use gplcart\core\models\Page as PageModel, |
|
13
|
|
|
gplcart\core\models\Product as ProductModel, |
|
14
|
|
|
gplcart\core\models\Collection as CollectionModel, |
|
15
|
|
|
gplcart\core\models\CollectionItem as CollectionItemModel; |
|
16
|
|
|
use gplcart\modules\faker\models\Generator as FakerModuleGenerator; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Manages basic behaviors and data related to collection items |
|
20
|
|
|
*/ |
|
21
|
|
|
class CollectionItem extends FakerModuleGenerator |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Product model class instance |
|
26
|
|
|
* @var \gplcart\core\models\Product $product |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $product; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Page model class instance |
|
32
|
|
|
* @var \gplcart\core\models\Page $page |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $page; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Collection model class instance |
|
38
|
|
|
* @var \gplcart\core\models\Collection $collection |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $collection; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Collection item model class instance |
|
44
|
|
|
* @var \gplcart\core\models\CollectionItem $collection_item |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $collection_item; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param CollectionModel $collection |
|
50
|
|
|
* @param CollectionItemModel $collection_item |
|
51
|
|
|
* @param ProductModel $product |
|
52
|
|
|
* @param PageModel $page |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(CollectionModel $collection, |
|
55
|
|
|
CollectionItemModel $collection_item, ProductModel $product, PageModel $page) |
|
56
|
|
|
{ |
|
57
|
|
|
parent::__construct(); |
|
58
|
|
|
|
|
59
|
|
|
$this->page = $page; |
|
60
|
|
|
$this->product = $product; |
|
61
|
|
|
$this->collection = $collection; |
|
62
|
|
|
$this->collection_item = $collection_item; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns the generator name |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getName() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->translation->text('Collection item'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Generate a single collection |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
public function create() |
|
79
|
|
|
{ |
|
80
|
|
|
$collection = $this->getCollection(); |
|
81
|
|
|
|
|
82
|
|
|
if (empty($collection)) { |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$data = array( |
|
87
|
|
|
'status' => $this->faker->boolean(), |
|
88
|
|
|
'value' => $this->getEntityId($collection), |
|
89
|
|
|
'collection_id' => $collection['collection_id'], |
|
90
|
|
|
'weight' => $this->faker->numberBetween(0, 20) |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
return (bool) $this->collection_item->add($data); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns a random entity ID |
|
98
|
|
|
* @staticvar array|null $items |
|
99
|
|
|
* @param array $collection |
|
100
|
|
|
* @return integer |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function getEntityId(array $collection) |
|
103
|
|
|
{ |
|
104
|
|
|
static $items = array(); |
|
105
|
|
|
if (!isset($items[$collection['type']])) { |
|
106
|
|
|
$options = array('limit' => array(0, 100)); |
|
107
|
|
|
switch ($collection['type']) { |
|
108
|
|
|
case 'page': |
|
109
|
|
|
$items[$collection['type']] = $this->page->getList($options); |
|
110
|
|
|
break; |
|
111
|
|
|
case 'product': |
|
112
|
|
|
$items[$collection['type']] = $this->product->getList($options); |
|
113
|
|
|
break; |
|
114
|
|
|
case 'file': |
|
115
|
|
|
$items[$collection['type']] = $this->file->getList($options); |
|
116
|
|
|
break; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if (empty($items[$collection['type']])) { |
|
121
|
|
|
return 0; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return (int) array_rand($items[$collection['type']]); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Returns a random collection |
|
129
|
|
|
* @staticvar array|null $collections |
|
130
|
|
|
* @return array |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function getCollection() |
|
133
|
|
|
{ |
|
134
|
|
|
static $collections = null; |
|
135
|
|
|
if (!isset($collections)) { |
|
136
|
|
|
$collections = $this->collection->getList(array('limit' => array(0, 100))); |
|
137
|
|
|
} |
|
138
|
|
|
return $collections[array_rand($collections)]; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|