forked from yii2tech/embedded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mapping.php
175 lines (160 loc) · 5.2 KB
/
Mapping.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
<?php
/**
* @link https://github.com/yii2tech
* @copyright Copyright (c) 2015 Yii2tech
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
*/
namespace yii2tech\embedded;
use ArrayObject;
use yii\base\InvalidParamException;
use yii\base\Object;
use Yii;
use yii\helpers\ArrayHelper;
/**
* Represents mapping between embedded object or object list and its container.
* It stores declaration of embedded policy and handles embedded value composition and extraction.
*
* @see ContainerTrait
*
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
class Mapping extends Object
{
/**
* @var string name of the container source field or property.
*/
public $source;
/**
* @var string|array target class name or array object configuration.
*/
public $target;
/**
* @var boolean whether list of objects should match the source value.
*/
public $multiple;
/**
* @var boolean whether to create empty object or list of objects, if the source field is null.
* If disabled [[getValue()]] will produce `null` value from null source.
*/
public $createFromNull = true;
/**
* @var boolean whether to set `null` for the owner [[source]] field, after the embedded value created.
* While enabled this saves memory usage, but also makes it impossible to use embedded and raw value at the same time.
*/
public $unsetSource = true;
/**
* @var mixed actual embedded value.
*/
private $_value = false;
/**
* Sets the embedded value.
* @param array|object|null $value actual value.
* @throws InvalidParamException on invalid argument
*/
public function setValue($value)
{
if (!is_null($value)) {
if ($this->multiple) {
if (is_array($value)) {
$arrayObject = new ArrayObject();
foreach ($value as $k => $v) {
$arrayObject[$k] = $v;
}
$value = $arrayObject;
} elseif (!($value instanceof \ArrayAccess)) {
throw new InvalidParamException("Value should either an array or a null, '" . gettype($value) . "' given.");
}
} else {
if (!is_object($value)) {
throw new InvalidParamException("Value should either an object or a null, '" . gettype($value) . "' given.");
}
}
}
$this->_value = $value;
}
/**
* Returns actual embedded value.
* @param object $owner owner object.
* @return object|object[]|null embedded value.
*/
public function getValue($owner)
{
if ($this->_value === false) {
$this->_value = $this->createValue($owner);
}
return $this->_value;
}
/**
* @param object $owner owner object
* @throws InvalidParamException on invalid source.
* @return array|null|object value.
*/
private function createValue($owner)
{
if (is_array($this->target)) {
$targetConfig = $this->target;
} else {
$targetConfig = ['class' => $this->target];
}
$sourceValue = $owner->{$this->source};
if ($this->createFromNull && $sourceValue === null) {
$sourceValue = [];
}
if ($this->multiple) {
$result = new ArrayObject();
foreach ($sourceValue as $key => $frame) {
if (!is_array($frame)) {
throw new InvalidParamException("Source value for the embedded should be an array.");
}
$result[$key] = Yii::createObject(array_merge($targetConfig, $frame));
}
} else {
if ($sourceValue === null) {
return null;
}
if (!is_array($sourceValue)) {
throw new InvalidParamException("Source value for the embedded should be an array.");
}
$result = Yii::createObject(array_merge($targetConfig, $sourceValue));
}
if ($this->unsetSource) {
$owner->{$this->source} = null;
}
return $result;
}
/**
* Extract embedded object(s) values as array.
* @param object $owner owner object
* @return array|null extracted values.
*/
public function extractValues($owner)
{
$embeddedValue = $this->getValue($owner);
if ($embeddedValue === null) {
$value = null;
} else {
if ($this->multiple) {
$value = [];
foreach ($embeddedValue as $key => $object) {
$value[$key] = $this->extractObjectValues($object);
}
} else {
$value = $this->extractObjectValues($embeddedValue);
}
}
return $value;
}
/**
* @param object $object
* @return array
*/
private function extractObjectValues($object)
{
$values = ArrayHelper::toArray($object, [], false);
if ($object instanceof ContainerInterface) {
$values = array_merge($values, $object->getEmbeddedValues());
}
return $values;
}
}