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; |
12
|
|
|
|
13
|
|
|
use Kuperwood\Eav\Enum\_ATTR; |
14
|
|
|
use Kuperwood\Eav\Traits\ContainerTrait; |
15
|
|
|
use Kuperwood\Eav\Value\ValueManager; |
16
|
|
|
|
17
|
|
|
class AttributeSetAction |
18
|
|
|
{ |
19
|
|
|
use ContainerTrait; |
20
|
|
|
|
21
|
3 |
|
public function initializeAttribute(array $record): Attribute |
22
|
|
|
{ |
23
|
3 |
|
$container = $this->getAttributeContainer(); |
24
|
3 |
|
$attribute = new Attribute(); |
25
|
|
|
// set default strategy if its empty |
26
|
3 |
|
if (key_exists(_ATTR::STRATEGY, $record) && empty($record[_ATTR::STRATEGY]) ) { |
27
|
1 |
|
$record[_ATTR::STRATEGY] = $attribute->getStrategy(); |
28
|
|
|
} |
29
|
3 |
|
$attribute->getBag()->setFields($record); |
30
|
3 |
|
$container->setAttribute($attribute); |
31
|
|
|
|
32
|
3 |
|
return $attribute; |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
public function initializeStrategy(Attribute $attribute): Strategy |
36
|
|
|
{ |
37
|
1 |
|
$container = $this->getAttributeContainer(); |
38
|
1 |
|
$className = $attribute->getStrategy(); |
39
|
1 |
|
$strategy = new $className(); |
40
|
1 |
|
$container->setStrategy($strategy); |
41
|
|
|
|
42
|
1 |
|
return $strategy; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public function initializeValueManager(): ValueManager |
46
|
|
|
{ |
47
|
1 |
|
$container = $this->getAttributeContainer(); |
48
|
1 |
|
$container->makeValueManager(); |
49
|
1 |
|
$container->makeValueAction(); |
50
|
1 |
|
$container->makeValueValidator(); |
51
|
1 |
|
$valueManager = $container->getValueManager(); |
52
|
1 |
|
$attribute = $container->getAttribute(); |
53
|
1 |
|
$entity = $container->getAttributeSet()->getEntity(); |
54
|
1 |
|
$bag = $entity->getBag(); |
55
|
1 |
|
$name = $attribute->getName(); |
56
|
1 |
|
if ($bag->hasField($name)) { |
57
|
1 |
|
$valueManager->setRuntime($bag->getField($name)); |
58
|
|
|
} |
59
|
1 |
|
$strategy = $container->getStrategy(); |
60
|
1 |
|
$strategy->find(); |
61
|
|
|
|
62
|
1 |
|
return $valueManager; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
public function initialize(array $attributeRecord): self |
66
|
|
|
{ |
67
|
2 |
|
$attribute = $this->initializeAttribute($attributeRecord); |
68
|
2 |
|
$this->initializeStrategy($attribute); |
69
|
2 |
|
$this->initializeValueManager(); |
70
|
|
|
|
71
|
2 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|