-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathEmbedsManyBehavior.php
87 lines (74 loc) · 2.06 KB
/
EmbedsManyBehavior.php
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
<?php
namespace consultnn\embedded;
use yii\helpers\Html;
use Yii;
use yii\validators\Validator;
/**
* Class EmbedsManyBehavior
* @property Storage $storage
* @package common\behaviors
*/
class EmbedsManyBehavior extends AbstractEmbeddedBehavior
{
public $initEmptyScenarios = [];
#[\ReturnTypeWillChange]
public function getFormName($index)
{
if ($this->setFormName) {
return Html::getInputName($this->owner, $this->fakeAttribute."[{$index}]");
}
return null;
}
/**
* @inheritdoc
*/
#[\ReturnTypeWillChange]
protected function setAttributes($attributes, $safeOnly = true)
{
$this->storage->removeAll();
if (empty($attributes)) {
return;
}
foreach ($attributes as $modelAttributes) {
$model = $this->createEmbedded(
$modelAttributes,
$safeOnly,
['formName' => $this->getFormName($this->storage->getNextIndex())]
);
if ($this->saveEmpty || !$model->isEmpty()) {
$this->storage[] = $model;
}
}
}
/**
* @inheritdoc
*/
#[\ReturnTypeWillChange]
protected function getAttributes()
{
return $this->storage->attributes;
}
/**
* @return Storage
*/
#[\ReturnTypeWillChange]
public function getStorage()
{
if (empty($this->_storage)) {
$this->_storage = new Storage();
$attributes = (array)$this->owner->{$this->attribute};
if (empty($attributes) && in_array($this->owner->scenario, $this->initEmptyScenarios)) {
$attributes[] = [];
}
foreach ($attributes as $modelAttributes) {
$model = $this->createEmbedded(
$modelAttributes,
false,
['formName' => $this->getFormName($this->_storage->getNextIndex())]
);
$this->_storage[] = $model;
}
}
return $this->_storage;
}
}