|
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\City as CityModel; |
|
13
|
|
|
use gplcart\core\models\CountryState as CountryStateModel; |
|
14
|
|
|
use gplcart\modules\faker\models\Generator as FakerModuleGenerator; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Manages basic behaviors and data related to cities |
|
18
|
|
|
*/ |
|
19
|
|
|
class City extends FakerModuleGenerator |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* City model class instance |
|
24
|
|
|
* @var \gplcart\core\models\City $city |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $city; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* State model class instance |
|
30
|
|
|
* @var \gplcart\core\models\CountryState $state |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $state; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param CityModel $city |
|
36
|
|
|
* @param CountryStateModel $state |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(CityModel $city, CountryStateModel $state) |
|
39
|
|
|
{ |
|
40
|
|
|
parent::__construct(); |
|
41
|
|
|
|
|
42
|
|
|
$this->city = $city; |
|
43
|
|
|
$this->state = $state; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns the generator name |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getName() |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->translation->text('City'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Generate a single city |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
|
|
public function create() |
|
60
|
|
|
{ |
|
61
|
|
|
$state = $this->getState(); |
|
62
|
|
|
|
|
63
|
|
|
if (empty($state)) { |
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$data = array( |
|
68
|
|
|
'name' => $this->faker->city(), |
|
69
|
|
|
'country' => $state['country'], |
|
70
|
|
|
'zone_id' => $state['zone_id'], |
|
71
|
|
|
'state_id' => $state['state_id'], |
|
72
|
|
|
'status' => $this->faker->boolean(), |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
return (bool) $this->city->add($data); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Returns a random state |
|
80
|
|
|
* @staticvar array $states |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function getState() |
|
84
|
|
|
{ |
|
85
|
|
|
static $states = null; |
|
86
|
|
|
if (!isset($states)) { |
|
87
|
|
|
$states = $this->state->getList(array('limit' => array(0, 300))); |
|
88
|
|
|
} |
|
89
|
|
|
return $states[array_rand($states)]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|