1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the eav package. |
4
|
|
|
* |
5
|
|
|
* @author Alex Kuperwood <[email protected]> |
6
|
|
|
* @copyright 2025 Alex Kuperwood |
7
|
|
|
* @license https://opensource.org/license/mit The MIT License |
8
|
|
|
*/ |
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Kuperwood\Eav\Factory; |
12
|
|
|
|
13
|
|
|
use Kuperwood\Eav\Enum\_ATTR; |
14
|
|
|
use Kuperwood\Eav\Enum\_DOMAIN; |
15
|
|
|
use Kuperwood\Eav\Enum\_ENTITY; |
16
|
|
|
use Kuperwood\Eav\Enum\_GROUP; |
17
|
|
|
use Kuperwood\Eav\Enum\_PIVOT; |
18
|
|
|
use Kuperwood\Eav\Enum\_SET; |
19
|
|
|
use Kuperwood\Eav\Enum\ATTR_TYPE; |
20
|
|
|
use Kuperwood\Eav\Model\AttributeGroupModel; |
21
|
|
|
use Kuperwood\Eav\Model\AttributeModel; |
22
|
|
|
use Kuperwood\Eav\Model\AttributeSetModel; |
23
|
|
|
use Kuperwood\Eav\Model\DomainModel; |
24
|
|
|
use Kuperwood\Eav\Model\EntityModel; |
25
|
|
|
use Kuperwood\Eav\Model\PivotModel; |
26
|
|
|
use Kuperwood\Eav\Result\Result; |
27
|
|
|
use Faker\Generator; |
28
|
|
|
|
29
|
|
|
class EavFactory |
30
|
|
|
{ |
31
|
|
|
private Generator $faker; |
32
|
|
|
|
33
|
1 |
|
public function __construct() |
34
|
|
|
{ |
35
|
1 |
|
$this->faker = \Faker\Factory::create(); |
36
|
|
|
} |
37
|
|
|
|
38
|
3 |
|
public function createDomain(array $data = []): int |
39
|
|
|
{ |
40
|
3 |
|
$defaultData = [ |
41
|
3 |
|
_DOMAIN::NAME => $this->faker->word(), |
42
|
3 |
|
]; |
43
|
3 |
|
$input = array_merge($defaultData, $data); |
44
|
3 |
|
$model = new DomainModel(); |
45
|
3 |
|
return $model->create($input); |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
public function createEntity(?int $domainKey = null, ?int $setKey = null): int |
49
|
|
|
{ |
50
|
2 |
|
if (is_null($domainKey)) |
51
|
|
|
{ |
52
|
1 |
|
$domainKey = $this->createDomain(); |
53
|
|
|
} |
54
|
2 |
|
if (is_null($setKey)) |
55
|
|
|
{ |
56
|
1 |
|
$setKey = $this->createAttributeSet($domainKey); |
57
|
|
|
} |
58
|
2 |
|
$model = new EntityModel(); |
59
|
2 |
|
return $model->create([ |
60
|
2 |
|
_ENTITY::DOMAIN_ID => $domainKey, |
61
|
2 |
|
_ENTITY::ATTR_SET_ID => $setKey |
62
|
2 |
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
public function createAttributeSet(?int $domainKey = null, array $data = []): int |
66
|
|
|
{ |
67
|
3 |
|
if (is_null($domainKey)) { |
68
|
1 |
|
$domainKey = $this->createDomain(); |
69
|
|
|
} |
70
|
3 |
|
$defaultData = [ |
71
|
3 |
|
_SET::NAME => $this->faker->word(), |
72
|
3 |
|
]; |
73
|
3 |
|
$input = array_merge($defaultData, $data); |
74
|
3 |
|
$model = new AttributeSetModel(); |
75
|
3 |
|
return $model->create([ |
76
|
3 |
|
_SET::DOMAIN_ID => $domainKey, |
77
|
3 |
|
_SET::NAME => $input[_SET::NAME] |
78
|
3 |
|
]); |
79
|
|
|
} |
80
|
|
|
|
81
|
3 |
|
public function createGroup(?int $setKey = null, array $data = []): int |
82
|
|
|
{ |
83
|
3 |
|
if (is_null($setKey)) { |
84
|
1 |
|
$setKey = $this->createAttributeSet(); |
85
|
|
|
} |
86
|
3 |
|
$defaultData = [ |
87
|
3 |
|
_GROUP::SET_ID => $setKey, |
88
|
3 |
|
_GROUP::NAME => $this->faker->word(), |
89
|
3 |
|
]; |
90
|
3 |
|
$input = array_merge($defaultData, $data); |
91
|
3 |
|
$model = new AttributeGroupModel(); |
92
|
|
|
|
93
|
3 |
|
return $model->create([ |
94
|
3 |
|
_GROUP::SET_ID => $setKey, |
95
|
3 |
|
_GROUP::NAME => $input[_SET::NAME] |
96
|
3 |
|
]); |
97
|
|
|
} |
98
|
|
|
|
99
|
3 |
|
public function createAttribute(?int $domainKey = null, array $data = []): int |
100
|
|
|
{ |
101
|
3 |
|
if (is_null($domainKey)) { |
102
|
1 |
|
$domainKey = $this->createDomain(); |
103
|
|
|
} |
104
|
3 |
|
$defaultData = [ |
105
|
3 |
|
_ATTR::DOMAIN_ID => $domainKey, |
106
|
3 |
|
_ATTR::NAME => $this->faker->slug(2), |
107
|
3 |
|
_ATTR::TYPE => _ATTR::bag(_ATTR::TYPE), |
108
|
3 |
|
_ATTR::STRATEGY => _ATTR::bag(_ATTR::STRATEGY), |
109
|
3 |
|
_ATTR::SOURCE => _ATTR::bag(_ATTR::SOURCE), |
110
|
3 |
|
_ATTR::DEFAULT_VALUE => _ATTR::bag(_ATTR::DEFAULT_VALUE), |
111
|
3 |
|
_ATTR::DESCRIPTION => _ATTR::bag(_ATTR::DESCRIPTION), |
112
|
3 |
|
]; |
113
|
3 |
|
$input = array_merge($defaultData, $data); |
114
|
3 |
|
$model = new AttributeModel(); |
115
|
3 |
|
return $model->create([ |
116
|
3 |
|
_ATTR::DOMAIN_ID => $domainKey, |
117
|
3 |
|
_ATTR::NAME => $input[_ATTR::NAME], |
118
|
3 |
|
_ATTR::TYPE => $input[_ATTR::TYPE], |
119
|
3 |
|
_ATTR::STRATEGY => $input[_ATTR::STRATEGY], |
120
|
3 |
|
_ATTR::SOURCE => $input[_ATTR::SOURCE], |
121
|
3 |
|
_ATTR::DEFAULT_VALUE => $input[_ATTR::DEFAULT_VALUE], |
122
|
3 |
|
_ATTR::DESCRIPTION => $input[_ATTR::DESCRIPTION], |
123
|
3 |
|
]); |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
public function createPivot(int $domainKey, int $setKey, int $groupKey, int $attributeKey): int |
127
|
|
|
{ |
128
|
1 |
|
$model = new PivotModel(); |
129
|
1 |
|
return $model->create([ |
130
|
1 |
|
_PIVOT::DOMAIN_ID => $domainKey, |
131
|
1 |
|
_PIVOT::SET_ID => $setKey, |
132
|
1 |
|
_PIVOT::GROUP_ID => $groupKey, |
133
|
1 |
|
_PIVOT::ATTR_ID => $attributeKey |
134
|
1 |
|
]); |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
public function createEavEntity(array $config, int $domainKey, int $setKey): Result |
138
|
|
|
{ |
139
|
|
|
|
140
|
1 |
|
$result = new Result(); |
141
|
1 |
|
$result->created(); |
142
|
1 |
|
$factory = new EntityFactory(); |
143
|
1 |
|
$result->setData($factory->create($config, $domainKey, $setKey)); |
144
|
|
|
|
145
|
1 |
|
return $result; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|