|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yarak\DB; |
|
4
|
|
|
|
|
5
|
|
|
use Faker\Generator; |
|
6
|
|
|
use Yarak\Config\Config; |
|
7
|
|
|
use Yarak\Helpers\Filesystem; |
|
8
|
|
|
use Phalcon\Mvc\User\Component; |
|
9
|
|
|
|
|
10
|
|
|
class ModelFactory extends Component |
|
11
|
|
|
{ |
|
12
|
|
|
use Filesystem; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Faker instance. |
|
16
|
|
|
* |
|
17
|
|
|
* @var Generator |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $faker; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Array of user defined factory definitions. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $definitions; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Construct. |
|
30
|
|
|
* |
|
31
|
|
|
* @param Generator $faker |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(Generator $faker) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->faker = $faker; |
|
36
|
|
|
|
|
37
|
|
|
$this->load(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Make instances of class, overriding default values with attributes. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $class |
|
44
|
|
|
* @param array $attributes |
|
45
|
|
|
* @param int $times |
|
46
|
|
|
* |
|
47
|
|
|
* @return Phalcon\Mvc\Model|array |
|
48
|
|
|
*/ |
|
49
|
|
|
public function make($class, array $attributes = [], $times = null) |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->forClass($class)->times($times)->make($attributes); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Make instances of class using name. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $class |
|
58
|
|
|
* @param string $name |
|
59
|
|
|
* @param array $attributes |
|
60
|
|
|
* @param int $times |
|
61
|
|
|
* |
|
62
|
|
|
* @return Phalcon\Mvc\Model|array |
|
63
|
|
|
*/ |
|
64
|
|
|
public function makeAs($class, $name, array $attributes = [], $times = null) |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->forClass($class, $name)->times($times)->make($attributes); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Create instances of class, overriding default values with attributes. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $class |
|
73
|
|
|
* @param array $attributes |
|
74
|
|
|
* @param int $times |
|
75
|
|
|
* |
|
76
|
|
|
* @return Phalcon\Mvc\Model|array |
|
77
|
|
|
*/ |
|
78
|
|
|
public function create($class, array $attributes = [], $times = null) |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->forClass($class)->times($times)->create($attributes); |
|
81
|
|
|
} |
|
82
|
|
|
/** |
|
83
|
|
|
* Create instances of class using name. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $class |
|
86
|
|
|
* @param string $name |
|
87
|
|
|
* @param array $attributes |
|
88
|
|
|
* @param int $times |
|
89
|
|
|
* |
|
90
|
|
|
* @return Phalcon\Mvc\Model|array |
|
91
|
|
|
*/ |
|
92
|
|
|
public function createAs($class, $name, array $attributes = [], $times = null) |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->forClass($class, $name)->times($times)->create($attributes); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get instance of factory builder with set attributes. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $class |
|
101
|
|
|
* @param string $name |
|
102
|
|
|
* |
|
103
|
|
|
* @return ModelFactoryBuilder |
|
104
|
|
|
*/ |
|
105
|
|
|
public function forClass($class, $name = 'default') |
|
106
|
|
|
{ |
|
107
|
|
|
return new ModelFactoryBuilder( |
|
108
|
|
|
$class, |
|
109
|
|
|
$name, |
|
110
|
|
|
$this->definitions, |
|
111
|
|
|
$this->faker |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Load factory definitions onto object. |
|
117
|
|
|
* |
|
118
|
|
|
* @return this |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function load() |
|
121
|
|
|
{ |
|
122
|
|
|
$factory = $this; |
|
123
|
|
|
|
|
124
|
|
|
$config = Config::getInstance(); |
|
125
|
|
|
|
|
126
|
|
|
$path = $config->getFactoryDirectory(); |
|
127
|
|
|
|
|
128
|
|
|
$this->makeDirectoryStructure([ |
|
129
|
|
|
$config->getDatabaseDirectory(), |
|
130
|
|
|
$path |
|
131
|
|
|
]); |
|
132
|
|
|
|
|
133
|
|
|
$dir = new \DirectoryIterator($path); |
|
134
|
|
|
|
|
135
|
|
|
foreach ($dir as $fileinfo) { |
|
136
|
|
|
if (!$fileinfo->isDot()) { |
|
137
|
|
|
require $fileinfo->getRealPath(); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
if (empty($this->definitions)) { |
|
142
|
|
|
throw new \Exception('No factory definitions found.'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $factory; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Define a class with a given set of attributes. |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $class |
|
152
|
|
|
* @param callable $attributes |
|
153
|
|
|
* @param string $name |
|
154
|
|
|
* |
|
155
|
|
|
* @return this |
|
156
|
|
|
*/ |
|
157
|
|
|
public function define($class, callable $attributes, $name = 'default') |
|
158
|
|
|
{ |
|
159
|
|
|
$this->definitions[$class][$name] = $attributes; |
|
160
|
|
|
|
|
161
|
|
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|