This repository has been archived by the owner on Feb 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayModel.php
205 lines (180 loc) · 5.23 KB
/
ArrayModel.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
* @link https://github.com/gromver/yii2-models.git#readme
* @copyright Copyright (c) Gayazov Roman, 2014
* @license https://github.com/gromver/yii2-models/blob/master/LICENSE
* @package yii2-models
* @version 1.0.0
*/
namespace gromver\models;
use gromver\models\fields\BaseField;
use yii\base\ArrayAccessTrait;
use yii\base\Object;
use yii\validators\Validator;
/**
* Class ArrayModel
* @package yii2-models
* @author Gayazov Roman <[email protected]>
*/
class ArrayModel extends BaseModel implements \Countable, ObjectModelInterface
{
use ArrayAccessTrait;
/**
* @var array
*/
private $_fieldConfig;
/**
* @var \gromver\models\fields\BaseField[]
*/
private $data = [];
/**
* Данная модель работает в связке с ObjectModel
* @var ObjectModel
*/
private $_objectModel;
/**
* @param ObjectModel
* @param array $fieldConfig
* @param array $config
* @internal param object|string $object
* @internal param array $attributes
*/
public function __construct(ObjectModel $model, array $fieldConfig, $config = [])
{
$this->_objectModel = $model;
$this->_fieldConfig = $fieldConfig;
Object::__construct($config);
}
/**
* @inheritdoc
*/
public function __get($name)
{
if (array_key_exists($name, $this->data)) {
return $this->data[$name]->getValue();
} else {
return parent::__get($name);
}
}
/**
* @inheritdoc
*/
public function __set($name, $value)
{
if (array_key_exists($name, $this->data)) {
$this->data[$name]->setValue($value);
} else {
parent::__set($name, $value);
}
}
/**
* @inheritdoc
*/
public function __isset($name)
{
if (array_key_exists($name, $this->data)) {
return isset($this->data[$name]);
} else {
return parent::__isset($name);
}
}
/**
* @inheritdoc
*/
public function __unset($name)
{
if (array_key_exists($name, $this->data)) {
unset($this->data[$name]);
} else {
parent::__unset($name);
}
}
/**
* Adds a validation rule to this model.
* You can also directly manipulate [[validators]] to add or remove validation rules.
* This method provides a shortcut.
* @param string|array $attributes the attribute(s) to be validated by the rule
* @param mixed $validator the validator for the rule.This can be a built-in validator name,
* a method name of the model class, an anonymous function, or a validator class name.
* @param array $options the options (name-value pairs) to be applied to the validator
* @return static the model itself
*/
public function addRule($attributes, $validator, $options = [])
{
$validators = $this->getValidators();
$validators->append(Validator::createValidator($validator, $this, (array) $attributes, $options));
return $this;
}
public function setAttributes($values, $safeOnly = true)
{
$this->data = [];
$this->getValidators()->exchangeArray([]);
if (is_array($values)) {
foreach ($values as $value) {
$this[] = $value;
}
}
}
/**
* Пляски с бубнами для того чтоб валидация нармально отрабатывала атрибуты с числовыми именами
* @inheritdoc
*/
public function attributes()
{
return array_map(function($value){return (string)$value;}, array_keys($this->data));
}
/**
* Пляски с бубнами для того чтоб валидация нармально отрабатывала атрибуты с числовыми именами
* @inheritdoc
*/
public function activeAttributes()
{
return array_map(function($value){return (string)$value;}, parent::activeAttributes());
}
public function modelFields()
{
return $this->data;
}
/**
* @return BaseField
*/
public function createField()
{
return BaseField::createField($this->_fieldConfig);
}
/**
* @inheritdoc
*/
public function offsetGet($offset)
{
return isset($this->data[$offset]) ? $this->data[$offset]->getValue() : null;
}
/**
* @inheritdoc
*/
public function offsetSet($offset, $item)
{
if ($offset == null) {
$offset = $this->count();
}
if ($this->offsetExists($offset)) {
$this->data[$offset]->setValue($item);
} else {
$field = BaseField::createField($this->_fieldConfig)->link($this, $offset);
if ($item !== null) $field->setValue($item);
$this->data[$offset] = $field;
$rules = $field->rules();
foreach ($rules as $rule) {
$this->addRule((string)$rule[0], $rule[1], array_slice($rule, 2));
}
}
}
// ObjectModelInterface
/**
* @inheritdoc
*/
public function getObjectModel()
{
return $this->_objectModel;
}
}