Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
objectfield.php
1
<?php
9
namespace
Bitrix\Main\ORM\Fields
;
10
16
class
ObjectField
extends
ScalarField
17
{
19
protected
$objectClasses
= [];
20
22
protected
$encodeFunction
;
23
25
protected
$decodeFunction
;
26
35
public
function
__construct
(
$name
, $parameters = [])
36
{
37
$this->
addSaveDataModifier
([$this,
'encode'
]);
38
$this->
addFetchDataModifier
([$this,
'decode'
]);
39
40
parent::__construct(
$name
, $parameters);
41
}
42
46
public
function
getObjectClasses
()
47
{
48
return
$this->objectClasses
;
49
}
50
56
public
function
configureObjectClass
($objectClass)
57
{
58
$classes = is_array($objectClass) ? $objectClass : [$objectClass];
59
60
foreach
($classes as $class)
61
{
62
if
(substr($class, 0, 1) !==
'\\'
)
63
{
64
$class =
'\\'
.$class;
65
}
66
67
$this->objectClasses[] = $class;
68
}
69
70
return
$this;
71
}
72
80
public
function
configureSerializeCallback
($callback)
81
{
82
$this->encodeFunction = $callback;
83
84
return
$this;
85
}
86
94
public
function
configureUnserializeCallback
($callback)
95
{
96
$this->decodeFunction = $callback;
97
98
return
$this;
99
}
100
106
public
function
encode
($value)
107
{
108
$callback =
$this->encodeFunction
;
109
return
$callback($value);
110
}
111
117
public
function
decode
($value)
118
{
119
$callback =
$this->decodeFunction
;
120
return
$callback($value);
121
}
122
127
public
function
cast
($value)
128
{
129
// try to check type
130
if
(!empty($this->objectClasses))
131
{
132
$foundClass =
false
;
133
134
foreach
($this->objectClasses as $objectType)
135
{
136
if
($value instanceof $objectType)
137
{
138
$foundClass =
true
;
139
break
;
140
}
141
}
142
143
if
(!$foundClass)
144
{
145
trigger_error(sprintf(
146
'Invalid value type `%s` for `%s` field'
,
147
gettype($value) ===
'object'
? get_class($value) : gettype($value),
148
$this->entity->getFullName().
':'
.
$this->name
149
), E_USER_WARNING
150
);
151
}
152
}
153
154
return
$value;
155
}
156
160
public
function
convertValueFromDb
($value)
161
{
162
return
$this->
getConnection
()->getSqlHelper()->convertFromDbString($value);
163
}
164
168
public
function
convertValueToDb
($value)
169
{
170
return
$value ===
null
&& $this->is_nullable
171
? $value
172
: $this->
getConnection
()->getSqlHelper()->convertToDbString($value);
173
}
174
178
public
function
getGetterTypeHint
()
179
{
180
$type =
'mixed'
;
181
182
if
(!empty($this->objectClasses))
183
{
184
$types =
$this->objectClasses
;
185
186
if
($this->is_nullable)
187
{
188
$types[] =
'null'
;
189
}
190
191
$type = join(
'|'
, $types);
192
}
193
194
return
$type;
195
}
196
200
public
function
getSetterTypeHint
()
201
{
202
$type =
'mixed'
;
203
204
if
(!empty($this->objectClasses))
205
{
206
$types =
$this->objectClasses
;
207
208
if
($this->is_nullable)
209
{
210
$types[] =
'null'
;
211
}
212
213
$type = join(
'|'
, $types);
214
}
215
216
return
$type;
217
}
218
}
Bitrix\Main\ORM\Fields\Field\$name
$name
Definition
field.php:27
Bitrix\Main\ORM\Fields\Field\getConnection
getConnection()
Definition
field.php:626
Bitrix\Main\ORM\Fields\Field\addFetchDataModifier
addFetchDataModifier($modifier)
Definition
field.php:344
Bitrix\Main\ORM\Fields\Field\addSaveDataModifier
addSaveDataModifier($modifier)
Definition
field.php:420
Bitrix\Main\ORM\Fields\ObjectField
Definition
objectfield.php:17
Bitrix\Main\ORM\Fields\ObjectField\configureObjectClass
configureObjectClass($objectClass)
Definition
objectfield.php:56
Bitrix\Main\ORM\Fields\ObjectField\decode
decode($value)
Definition
objectfield.php:117
Bitrix\Main\ORM\Fields\ObjectField\getSetterTypeHint
getSetterTypeHint()
Definition
objectfield.php:200
Bitrix\Main\ORM\Fields\ObjectField\$decodeFunction
$decodeFunction
Definition
objectfield.php:25
Bitrix\Main\ORM\Fields\ObjectField\cast
cast($value)
Definition
objectfield.php:127
Bitrix\Main\ORM\Fields\ObjectField\configureUnserializeCallback
configureUnserializeCallback($callback)
Definition
objectfield.php:94
Bitrix\Main\ORM\Fields\ObjectField\$encodeFunction
$encodeFunction
Definition
objectfield.php:22
Bitrix\Main\ORM\Fields\ObjectField\getGetterTypeHint
getGetterTypeHint()
Definition
objectfield.php:178
Bitrix\Main\ORM\Fields\ObjectField\getObjectClasses
getObjectClasses()
Definition
objectfield.php:46
Bitrix\Main\ORM\Fields\ObjectField\$objectClasses
$objectClasses
Definition
objectfield.php:19
Bitrix\Main\ORM\Fields\ObjectField\convertValueFromDb
convertValueFromDb($value)
Definition
objectfield.php:160
Bitrix\Main\ORM\Fields\ObjectField\__construct
__construct($name, $parameters=[])
Definition
objectfield.php:35
Bitrix\Main\ORM\Fields\ObjectField\encode
encode($value)
Definition
objectfield.php:106
Bitrix\Main\ORM\Fields\ObjectField\convertValueToDb
convertValueToDb($value)
Definition
objectfield.php:168
Bitrix\Main\ORM\Fields\ObjectField\configureSerializeCallback
configureSerializeCallback($callback)
Definition
objectfield.php:80
Bitrix\Main\ORM\Fields\ScalarField
Definition
scalarfield.php:19
Bitrix\Main\ORM\Fields
Definition
arrayfield.php:9
modules
main
lib
orm
fields
objectfield.php
Создано системой
1.10.0