-
-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathModelFactory.php
More file actions
147 lines (117 loc) · 3.69 KB
/
Copy pathModelFactory.php
File metadata and controls
147 lines (117 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace Tempest\Framework\Testing;
use DateTime as PHPDateTime;
use Tempest\Database\PrimaryKey;
use Tempest\DateTime\DateTime;
use Tempest\Reflection\PropertyReflector;
use function Tempest\Mapper\map;
use function Tempest\Reflection\reflect;
use function Tempest\Support\arr;
/** @template TModelClass */
final class ModelFactory
{
private array $fields = [];
public function __construct(
/** @var class-string<TModelClass> The model class to create an instance of. */
private readonly string $modelClass,
) {}
/**
* Make multiple instances of the model class.
*
* @param int|array $items The number of instances to make, or an array with field values used as a sequence to generate multiple instances.
*
* @return ModelFactoryCollection<TModelClass>
*/
public function times(int|array $items): ModelFactoryCollection
{
return new ModelFactoryCollection($this, $items);
}
/**
* Set up values that should be used for specific fields when creating a model instance
*
* @param mixed ...$fields If another instance of a ModelFactory is passed, it will be used to create the value for that property.
*
* @return self<TModelClass>
*/
public function with(mixed ...$fields): self
{
return clone($this, [
'fields' => [
...$this->fields,
...$fields,
],
]);
}
/**
* Make an instance of the model class.
*
* @return TModelClass
*/
public function make()
{
$fields = $this->fields;
foreach ($fields as $key => $value) {
if (! $value instanceof ModelFactory) {
continue;
}
$fields[$key] = $value->make();
}
$model = map($fields)->to($this->modelClass);
foreach (reflect($model)->getPublicProperties() as $property) {
if ($property->isInitialized($model)) {
continue;
}
if ($property->hasDefaultValue()) {
continue;
}
$value = $this->generateValue($property);
if ($value === null && $property->isNullable()) {
$property->setValue($model, null);
continue;
}
if ($value === null) {
continue;
}
$property->setValue($model, $value);
}
return $model;
}
/**
* Make an instance of the model class and save it to the database.
*
* @return TModelClass
*/
public function save()
{
$model = $this->make();
$model->save();
return $model;
}
private function generateValue(PropertyReflector $property): mixed
{
$type = $property->getType();
if ($type->matches(PrimaryKey::class)) {
return null;
}
if ($type->matches(DateTime::class)) {
return DateTime::now()->plusHours(random_int(-24, 24));
}
if ($type->matches(PHPDateTime::class)) {
return DateTime::now()->plusHours(random_int(-24, 24))->toNativeDateTime();
}
if ($type->isEnum()) {
$cases = arr($type->getName()::cases());
return $cases->random();
}
if ($type->isClass()) {
return new self($type->getName())->make();
}
return match ($type->getName()) {
'string' => arr(['Lorem', 'Ipsum', 'Dolor', 'Sit', 'Amet'])->random(),
'int' => random_int(1, 100),
'float' => random_int(100, 1000) / 10,
'bool' => arr([true, false])->random(),
default => null,
};
}
}