1
|
|
|
<?php namespace AdamWathan\Faktory\Factory; |
2
|
|
|
|
3
|
|
|
use AdamWathan\Faktory\Strategy\Build as BuildStrategy; |
4
|
|
|
use AdamWathan\Faktory\Strategy\Create as CreateStrategy; |
5
|
|
|
use AdamWathan\Faktory\Relationship\BelongsTo; |
6
|
|
|
use AdamWathan\Faktory\Relationship\HasMany; |
7
|
|
|
use AdamWathan\Faktory\Relationship\HasOne; |
8
|
|
|
|
9
|
|
|
class Factory |
10
|
|
|
{ |
11
|
|
|
protected $model; |
12
|
|
|
protected $attributes; |
13
|
|
|
protected $factory_repository; |
14
|
|
|
protected $sequence = 1; |
15
|
|
|
|
16
|
|
|
public function __construct($model, $factory_repository) |
17
|
|
|
{ |
18
|
|
|
$this->model = $model; |
19
|
|
|
$this->factory_repository = $factory_repository; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function make($model, $factory_repository) |
23
|
|
|
{ |
24
|
|
|
return new static($model, $factory_repository); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getModel() |
28
|
|
|
{ |
29
|
|
|
return $this->model; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function __set($key, $value) |
33
|
|
|
{ |
34
|
|
|
$this->setAttribute($key, $value); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function __get($key) |
38
|
|
|
{ |
39
|
|
|
return $this->getAttribute($key); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function getAttribute($key) |
43
|
|
|
{ |
44
|
|
|
if (is_callable($this->attributes[$key])) { |
45
|
|
|
return $this->attributes[$key]($this, $this->sequence); |
46
|
|
|
} |
47
|
|
|
return $this->attributes[$key]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function setAttributes($attributes) |
51
|
|
|
{ |
52
|
|
|
$this->attributes = $attributes; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function setAttribute($key, $value) |
56
|
|
|
{ |
57
|
|
|
$this->attributes[$key] = $value; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function build($override_attributes) |
61
|
|
|
{ |
62
|
|
|
$strategy = BuildStrategy::make($this->model, $this->sequence); |
63
|
|
|
return $this->newInstance($strategy, $override_attributes); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function create($override_attributes) |
67
|
|
|
{ |
68
|
|
|
$strategy = CreateStrategy::make($this->model, $this->sequence); |
69
|
|
|
return $this->newInstance($strategy, $override_attributes); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function newInstance($strategy, $override_attributes) |
73
|
|
|
{ |
74
|
|
|
$strategy->attributes($this->mergeAttributes($override_attributes)); |
75
|
|
|
$instance = $strategy->newInstance(); |
76
|
|
|
$this->sequence++; |
77
|
|
|
return $instance; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function mergeAttributes($override_attributes) |
81
|
|
|
{ |
82
|
|
|
if (is_callable($override_attributes)) { |
83
|
|
|
$override_attributes = $this->getOverridesFromClosure($override_attributes); |
84
|
|
|
} |
85
|
|
|
return array_merge($this->attributes, $override_attributes); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function getOverridesFromClosure($closure) |
89
|
|
|
{ |
90
|
|
|
$that = clone $this; |
91
|
|
|
$closure($that); |
92
|
|
|
return $that->attributes; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
public function buildMany($count, $override_attributes) |
96
|
|
|
{ |
97
|
|
|
$override_attributes = $this->expandAttributesForList($override_attributes, $count); |
98
|
|
|
return array_map(function ($i) use ($override_attributes) { |
99
|
|
|
return $this->build($override_attributes[$i]); |
100
|
|
|
}, range(0, $count - 1)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function expandAttributesForList($attributes, $count) |
104
|
|
|
{ |
105
|
|
|
return array_map(function ($i) use ($attributes) { |
106
|
|
|
return $this->extractAttributesForIndex($i, $attributes); |
107
|
|
|
}, range(0, $count - 1)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected function extractAttributesForIndex($i, $attributes) |
111
|
|
|
{ |
112
|
|
|
return array_map(function ($value) use ($i) { |
113
|
|
|
return is_array($value) ? $value[$i] : $value; |
114
|
|
|
}, $attributes); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
View Code Duplication |
public function createMany($count, $override_attributes) |
118
|
|
|
{ |
119
|
|
|
$override_attributes = $this->expandAttributesForList($override_attributes, $count); |
120
|
|
|
return array_map(function ($i) use ($override_attributes) { |
121
|
|
|
return $this->create($override_attributes[$i]); |
122
|
|
|
}, range(0, $count - 1)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function define($name, $definitionCallback) |
126
|
|
|
{ |
127
|
|
|
$callback = function ($f) use ($definitionCallback) { |
128
|
|
|
$f->setAttributes($this->attributes); |
129
|
|
|
$definitionCallback($f); |
130
|
|
|
}; |
131
|
|
|
$this->factory_repository->define($this->model, $name, $callback); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function belongsTo($name, $foreign_key = null, $attributes = []) |
135
|
|
|
{ |
136
|
|
|
$factory = $this->factory_repository->getFactory($name); |
137
|
|
|
return new BelongsTo($this->model, $factory, $foreign_key, $attributes); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function hasMany($name, $count, $foreign_key = null, $attributes = []) |
141
|
|
|
{ |
142
|
|
|
$factory = $this->factory_repository->getFactory($name); |
143
|
|
|
return new HasMany($this->model, $factory, $count, $foreign_key, $attributes); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function hasOne($name, $foreign_key = null, $attributes = []) |
147
|
|
|
{ |
148
|
|
|
$factory = $this->factory_repository->getFactory($name); |
149
|
|
|
return new HasOne($this->model, $factory, $foreign_key, $attributes); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|